
fr.ird.observe.dto.report.DataMatrix Maven / Gradle / Ivy
package fr.ird.observe.dto.report;
/*-
* #%L
* ObServe Toolkit :: Dto
* %%
* Copyright (C) 2017 - 2021 Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
import fr.ird.observe.dto.ObserveDto;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.Serializable;
import java.util.StringJoiner;
/**
* Une matrice de données
*
* @author Tony Chemit - [email protected]
* @since 1.4
*/
public class DataMatrix implements ObserveDto {
private static final Logger log = LogManager.getLogger(DataMatrix.class);
protected Object[][] data;
protected int width;
protected int height;
protected int x;
protected int y;
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Object[][] getData() {
return data;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void createData() {
data = new Object[height][width];
}
public void setData(Object[][] data) {
this.data = data;
}
public void copyData(DataMatrix incoming) {
int x = incoming.getX();
int y = incoming.getY();
int height = incoming.getHeight();
int width = incoming.getWidth();
log.debug(String.format("copying incoming matrix (dim: %s, location: %s)", incoming.getDimension(), incoming.getLocation()));
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Serializable value = incoming.getValue(i, j);
setValue(x + i, y + j, value);
}
}
}
public DataMatrixDimension getDimension() {
return new DataMatrixDimension(width, height);
}
public void setDimension(DataMatrixDimension dim) {
height = dim.getHeight();
width = dim.getWidth();
}
public DataMatrixPoint getLocation() {
return new DataMatrixPoint(x, y);
}
public void setLocation(DataMatrixPoint location) {
x = location.getX();
y = location.getY();
}
public Serializable getValue(int x, int y) {
return data == null ? null : (Serializable) data[y][x];
}
public void setValue(int x, int y, Object data) {
String cellData = data == null ? null : String.valueOf(data);
log.debug(String.format("Put data [%d,%d] = %s", x, y, cellData));
this.data[y][x] = cellData;
}
public static DataMatrixDimension getDimension(DataMatrix... datas) {
int width = 0;
int height = 0;
for (DataMatrix request : datas) {
int nWidth = request.getX() + request.getWidth();
int nHeight = request.getY() + request.getHeight();
if (nWidth > width) {
width = nWidth;
}
if (nHeight > height) {
height = nHeight;
}
}
return new DataMatrixDimension(width, height);
}
public static DataMatrix merge(DataMatrix... incomings) {
return merge(-1, -1, incomings);
}
public static DataMatrix merge(int rows,
int columns,
DataMatrix... incomings) {
DataMatrixDimension dimension = getDimension(incomings);
log.debug(String.format("Merge dimension : %s", dimension));
if (rows != -1) {
int height = dimension.getHeight();
// on verifie que récupère bien le bon count de lignes
if (rows != height) {
log.warn(String.format("No matching rows number : should have %d, but was %d", rows, height));
}
}
if (columns != -1) {
int width = dimension.getWidth();
// on verifie que récupère bien le bon count de colonnes
if (columns != width) {
log.warn(String.format("No matching columns number : should have %d, but was %d", columns, width));
}
}
DataMatrix result = new DataMatrix();
result.setDimension(dimension);
result.createData();
for (DataMatrix incoming : incomings) {
result.copyData(incoming);
}
return result;
}
@Override
public String toString() {
return new StringJoiner(", ", DataMatrix.class.getSimpleName() + "[", "]")
.add("dimension=" + getDimension())
.add("location=" + getLocation())
.toString();
}
public String getClipboardContent(boolean copyRowHeaders,
boolean copyColumnHeaders,
boolean escapeCells, char sep) {
if (getWidth() <= 0 || getHeight() <= 0) {
return "";
}
StringBuilder buffer = new StringBuilder();
char eol = '\n';
for (int y = copyColumnHeaders ? 0 : 1, rows = getHeight(); y < rows; y++) {
Serializable value;
// nouvell ligne
int x = copyRowHeaders ? 0 : 1;
for (int columns = getWidth() - 1; x < columns; x++) {
// sur chaque cellule (sauf la dernière)
value = getValue(x, y);
if (escapeCells) {
value = "\"" + value + "\"";
}
buffer.append(value).append(sep);
}
// dernière cellule
value = getValue(x, y);
if (escapeCells) {
value = "\"" + value + "\"";
}
buffer.append(value);
// fin de ligne
buffer.append(eol);
}
return buffer.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy