Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.grid;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.hisp.grid.serializer.JacksonRowDataSerializer;
/**
* Implementation of the {@link Grid} interface using {@link ArrayList}. This implementation is
* annotated with {@code Jackson} annotations and hence can be rendered as JSON and XML using
* Jackson.
*/
public class ListGrid implements Grid {
private static final String CUMULATIVE_SUFFIX = "_cumulative";
/** The title of the grid. */
private String title;
/** The subtitle of the grid. */
private String subtitle;
/** The name of a potential corresponding table. */
private String table;
/** A List which represents the mn headers of the grid. */
private List headers;
/** A Map which can hold arbitrary meta-data. */
private Map metaData;
/**
* A two dimensional List which simulates a grid where the first list represents rows and the
* second represents columns.
*/
private List> grid;
/** Indicating the current row in the grid for writing data. */
private int currentRowWriteIndex = -1;
/** Represents a mapping between column names and the index of the column in the grid. */
private Map columnIndexMap = new HashMap<>();
/** Default constructor. */
public ListGrid() {
this.headers = new ArrayList<>();
this.metaData = new HashMap<>();
this.grid = new ArrayList<>();
}
/**
* @param metaData meta data.
*/
public ListGrid(Map metaData) {
this.headers = new ArrayList<>();
this.metaData = metaData;
this.grid = new ArrayList<>();
}
// ---------------------------------------------------------------------
// Public methods
// ---------------------------------------------------------------------
@Override
@JsonProperty
public String getTitle() {
return title;
}
@Override
public Grid setTitle(String title) {
this.title = title;
return this;
}
@Override
@JsonProperty
public String getSubtitle() {
return subtitle;
}
@Override
public Grid setSubtitle(String subtitle) {
this.subtitle = subtitle;
return this;
}
@Override
@JsonProperty
public String getTable() {
return table;
}
@Override
public Grid setTable(String table) {
this.table = table;
return this;
}
@Override
public Grid addHeader(GridHeader header) {
headers.add(header);
updateColumnIndexMap();
return this;
}
@Override
public Grid addHeader(String name) {
return addHeader(new GridHeader(name));
}
@Override
public Grid addHeader(int headerIndex, GridHeader header) {
headers.add(headerIndex, header);
updateColumnIndexMap();
return this;
}
@Override
public Grid addHeaders(int headerIndex, List gridHeaders) {
if (gridHeaders == null || gridHeaders.isEmpty()) {
return this;
}
for (int i = gridHeaders.size() - 1; i >= 0; i--) {
headers.add(headerIndex, gridHeaders.get(i));
}
updateColumnIndexMap();
return this;
}
@Override
public Grid addEmptyHeaders(int number) {
for (int i = 0; i < number; i++) {
headers.add(new GridHeader("", false, false));
}
updateColumnIndexMap();
return this;
}
@Override
@JsonProperty
public List getHeaders() {
return headers;
}
@Override
@JsonIgnore
public List getVisibleHeaders() {
List tempHeaders = new ArrayList<>();
for (GridHeader header : headers) {
if (!header.isHidden()) {
tempHeaders.add(header);
}
}
return tempHeaders;
}
@Override
@JsonIgnore
public int getIndexOfHeader(String name) {
return headers.indexOf(new GridHeader(name, null));
}
@Override
@JsonProperty
public int getHeight() {
return grid != null && grid.size() > 0 ? grid.size() : 0;
}
@Override
@JsonProperty
public int getWidth() {
verifyGridState();
return grid != null && grid.size() > 0 ? grid.get(0).size() : 0;
}
@Override
@JsonProperty
public Map getMetaData() {
return metaData;
}
@Override
public Grid setMetaData(Map metaData) {
this.metaData = metaData;
return this;
}
@Override
public Grid addMetaData(String key, Object value) {
this.metaData.put(key, value);
return this;
}
@Override
@JsonIgnore
public int getVisibleWidth() {
verifyGridState();
return grid != null && grid.size() > 0 ? getVisibleRows().get(0).size() : 0;
}
@Override
public Grid addRow() {
grid.add(new ArrayList<>());
currentRowWriteIndex++;
return this;
}
@Override
public Grid addRows(Grid grid) {
List> rows = grid.getRows();
for (List