Skip to content

Commit

Permalink
Merge pull request #17 from nRo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nRo authored Jan 7, 2019
2 parents 3918294 + a974973 commit 5c0f156
Show file tree
Hide file tree
Showing 26 changed files with 159 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Add this to you pom.xml
<dependency>
<groupId>de.unknownreality</groupId>
<artifactId>dataframe</artifactId>
<version>0.7.5.2</version>
<version>0.7.5.3</version>
</dependency>
...
</dependencies>
Expand Down Expand Up @@ -52,7 +52,7 @@ To build the library from sources:
<dependency>
<groupId>de.unknownreality</groupId>
<artifactId>dataframe</artifactId>
<version>0.7.5.2-SNAPSHOT</version>
<version>0.7.5.3-SNAPSHOT</version>
</dependency>
...
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.unknownreality</groupId>
<artifactId>dataframe</artifactId>
<version>0.7.5.3</version>
<version>0.7.5.3-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ public class ColumnPrintSettings {
private ValueFormatter headerFormatter;
private Integer width;
private Integer maxContentWidth;
private boolean autoWidth;

public boolean isAutoWidth() {
return autoWidth;
}

public void setAutoWidth(boolean autoWidth) {
this.autoWidth = autoWidth;
}

public ColumnPrintSettings(Object columnHeader){
this.columnHeader = columnHeader;
Expand Down
107 changes: 88 additions & 19 deletions src/main/java/de/unknownreality/dataframe/print/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.unknownreality.dataframe.common.Header;
import de.unknownreality.dataframe.common.Row;
import de.unknownreality.dataframe.common.header.BasicTypeHeader;
import de.unknownreality.dataframe.io.DataIterator;
import de.unknownreality.dataframe.io.DataWriter;
import de.unknownreality.dataframe.io.ReadFormat;

Expand Down Expand Up @@ -33,17 +34,20 @@ public class Printer extends DataWriter {
private String innerCrossConnection = "┼";
private int defaultColumnWidth = 12;
private int defaultMaxContentWidth = 10;
private int maxAutoWidth = 500;
private Map<Object, ColumnPrintSettings> columnSettings = new HashMap<>();
private ValueFormatter defaultValueFormatter = new DefaultValueFormatter();
private ValueFormatter defaultHeaderFormatter = (v, m) -> "#" + v.toString();
private ValueFormatter defaultNumberFormatter
= new DefaultNumberFormatter();


@Override
public void write(BufferedWriter writer, DataContainer<?, ?> dataContainer) {
try {
ColumnPrintSettings[] settings = new ColumnPrintSettings[dataContainer.getHeader().size()];
writeHeader(dataContainer, writer, settings);
ColumnPrintSettings[] settings = createPrintSettings(dataContainer);
ColumnWidth[] columnWidths = createColumnWidth(dataContainer, settings);
writeHeader(dataContainer, writer, settings, columnWidths);
StringBuilder contentLineSb = new StringBuilder();
StringBuilder topLineSb = new StringBuilder();
StringBuilder lastLineSb = new StringBuilder();
Expand All @@ -56,6 +60,7 @@ public void write(BufferedWriter writer, DataContainer<?, ?> dataContainer) {
last = !it.hasNext();
for (int i = 0; i < row.size(); i++) {
ColumnPrintSettings colSettings = settings[i];
ColumnWidth columnWidth = columnWidths[i];
if (colSettings.getValueFormatter() == null) {
colSettings.setValueFormatter(getDefaultValueFormatter(row.get(i)));
}
Expand All @@ -66,7 +71,7 @@ public void write(BufferedWriter writer, DataContainer<?, ?> dataContainer) {
lastLineSb.append(bottomLeftCorner);
}
}
String content = formatContent(colSettings, row, i);
String content = formatContent(colSettings, columnWidth, row, i);
contentLineSb.append(content);
for (int j = 0; j < content.length(); j++) {
topLineSb.append(innerHorizontalLine);
Expand Down Expand Up @@ -103,51 +108,110 @@ public void write(BufferedWriter writer, DataContainer<?, ?> dataContainer) {
writer.flush();

} catch (Exception e) {
throw new DataFrameRuntimeException("error printing data container",e);
throw new DataFrameRuntimeException("error printing data container", e);
}
}

private String formatContent(ColumnPrintSettings columnPrintSettings, Row<?, ?> row, int col) {
private ColumnWidth[] createColumnWidth(DataContainer<?, ?> dataContainer, ColumnPrintSettings[] columnPrintSetting) {
ColumnWidth[] widths = new ColumnWidth[dataContainer.getHeader().size()];
int[] autoWidthCols = new int[widths.length];
int[] max = new int[widths.length];

int autoWidthColsCount = 0;
for (int i = 0; i < widths.length; i++) {
ColumnPrintSettings settings = columnPrintSetting[i];
ColumnWidth columnWidth = new ColumnWidth();
if (!settings.isAutoWidth()) {
columnWidth.width = settings.getWidth();
columnWidth.contentWidth = settings.getMaxContentWidth();
} else {
max[i] = getContentLength(dataContainer.getHeader().get(i), settings);
autoWidthCols[autoWidthColsCount++] = i;
}
widths[i] = columnWidth;
}
if (autoWidthColsCount == 0) {
return widths;
}
int c;
for (Row<?, ?> row : dataContainer) {
for (int i = 0; i < autoWidthColsCount; i++) {
ColumnPrintSettings settings = columnPrintSetting[i];
c = autoWidthCols[i];
Object v = row.get(c);
int length = getContentLength(v, settings);
max[i] = Math.max(max[i], length);
}
}
for (int i = 0; i < autoWidthColsCount; i++) {
ColumnWidth columnWidth = widths[autoWidthCols[i]];
int m = Math.min(maxAutoWidth, max[i]);
columnWidth.contentWidth = m;
columnWidth.width = m + 1;
}
return widths;
}

private int getContentLength(Object v, ColumnPrintSettings printSettings) {
int length;
if (v == null) {
return 0;
} else if (Values.NA.isNA(v)) {
length = 2;
} else {
length = printSettings
.getValueFormatter().format(v, maxAutoWidth).length();
}
return length;
}

private String formatContent(ColumnPrintSettings columnPrintSettings, ColumnWidth columnWidth, Row<?, ?> row, int col) {
Object v = row.get(col);
String valueString;
if(v == null){
if (v == null) {
valueString = "";
}
else if(Values.NA.isNA(v)){
} else if (Values.NA.isNA(v)) {
valueString = "NA";
}
else{
} else {
valueString = columnPrintSettings
.getValueFormatter().format(v,
columnPrintSettings.getMaxContentWidth());
columnWidth.contentWidth);
}
String fmt = "%-" + columnPrintSettings.getWidth() + "." + columnPrintSettings.getMaxContentWidth() + "s";
String fmt = "%-" + columnWidth.width + "." + columnWidth.contentWidth + "s";
return String.format(fmt, valueString);
}

private String formatHeaderContent(Object header, ColumnPrintSettings columnPrintSettings) {
private String formatHeaderContent(Object header, ColumnPrintSettings columnPrintSettings, ColumnWidth columnWidth) {
String headerString = columnPrintSettings
.getHeaderFormatter().format(header,
columnPrintSettings.getMaxContentWidth());
columnWidth.contentWidth);

String fmt = "%-" + columnPrintSettings.getWidth() + "." + columnPrintSettings.getMaxContentWidth() + "s";
String fmt = "%-" + columnWidth.width + "." + columnWidth.contentWidth + "s";
return String.format(fmt, headerString);
}

protected ColumnPrintSettings[] createPrintSettings(DataContainer<?, ?> dataContainer) {
ColumnPrintSettings[] settings = new ColumnPrintSettings[dataContainer.getHeader().size()];
for (int i = 0; i < dataContainer.getHeader().size(); i++) {
ColumnPrintSettings columnSettings = getSettings(dataContainer, i);
settings[i] = columnSettings;
}
return settings;
}

protected void writeHeader(DataContainer<?, ?> dataContainer, BufferedWriter writer, ColumnPrintSettings[] settings) throws IOException {
protected void writeHeader(DataContainer<?, ?> dataContainer, BufferedWriter writer, ColumnPrintSettings[] settings, ColumnWidth[] columnWidths) throws IOException {
StringBuilder contentLineSb = new StringBuilder();
StringBuilder topLineSb = new StringBuilder();
topLineSb.append(topLeftCorner);
int cols = dataContainer.getHeader().size();
int colIdx = 0;
for (Object header : dataContainer.getHeader()) {
ColumnPrintSettings columnSettings = getSettings(dataContainer, colIdx);
settings[colIdx] = columnSettings;
ColumnWidth columnWidth = columnWidths[colIdx];
ColumnPrintSettings columnSettings = settings[colIdx];
if (contentLineSb.length() == 0) {
contentLineSb.append(leftLine);
}
String content = formatHeaderContent(header, columnSettings);
String content = formatHeaderContent(header, columnSettings, columnWidth);
contentLineSb.append(content);
for (int i = 0; i < content.length(); i++) {
topLineSb.append(topLine);
Expand Down Expand Up @@ -407,4 +471,9 @@ public List<DataFrameColumn> getMetaColumns(DataFrame dataFrame) {
public ReadFormat getReadFormat() {
return null;
}

private class ColumnWidth {
public int contentWidth;
public int width;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public PrinterBuilder withColumnWidth(Object header, int width) {
return this;
}

public PrinterBuilder withAutoWidth(Object header) {
ColumnPrintSettings settings = columnSettings.computeIfAbsent(
header, (h) -> new ColumnPrintSettings(header)
);
settings.setAutoWidth(true);
return this;
}

public PrinterBuilder withTTop(String tTop){
this.tTop = tTop;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.DataFrameException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.DataRow;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.DataFrameBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.csv.CSVReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@
* SOFTWARE.
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.DataFrameException;
import de.unknownreality.dataframe.DataFrameLoader;
import de.unknownreality.dataframe.DataRow;
import de.unknownreality.dataframe.column.DoubleColumn;
import de.unknownreality.dataframe.column.IntegerColumn;
import de.unknownreality.dataframe.column.StringColumn;
import de.unknownreality.dataframe.csv.CSVReader;
import de.unknownreality.dataframe.csv.CSVReaderBuilder;
import de.unknownreality.dataframe.filter.FilterPredicate;
import de.unknownreality.dataframe.meta.DataFrameMeta;
import de.unknownreality.dataframe.group.DataFrameGroupingTest;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
* SOFTWARE.
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.column.DoubleColumn;
import de.unknownreality.dataframe.column.IntegerColumn;
import de.unknownreality.dataframe.column.StringColumn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* SOFTWARE.
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.*;
import de.unknownreality.dataframe.column.BooleanColumn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* SOFTWARE.
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.DataFrameLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
*
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.io.FileFormat;
import org.junit.Assert;
import org.junit.Rule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.column.DoubleColumn;
import de.unknownreality.dataframe.column.FloatColumn;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe;

import de.unknownreality.dataframe.ColumnTypeMap;
import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.DataRow;
import de.unknownreality.dataframe.print.Printer;
import de.unknownreality.dataframe.print.PrinterBuilder;
import org.junit.Assert;
Expand Down Expand Up @@ -116,6 +113,33 @@ else if(j == 2 && "GermanyGermany".equals(c)){
}
}
}

@Test
public void testAutoWidth(){
Printer printer = PrinterBuilder.create()
.build();

DataFrame df = DataFrame.create()
.addStringColumn("testA")
.addDoubleColumn("testB");
df.append("abc",1.5d);
df.append("abcdefgh",1.5d);
df.append("abcdef",1.5d);
df.append("abcdefghijklmnopqrstuvwxyz",1.5d);
df.append("ab",1.5d);
StringWriter sw = new StringWriter();
df.write(sw, printer);
String[] lines = sw.toString().split("\\r?\\n");
Assert.assertEquals("│abcdefg... │1.50000000 │",lines[9]);

printer = PrinterBuilder.create()
.withAutoWidth("testA")
.build();
sw = new StringWriter();
df.write(sw, printer);
lines = sw.toString().split("\\r?\\n");
Assert.assertEquals("│abcdefghijklmnopqrstuvwxyz │1.50000000 │",lines[9]);
}
private void assertContent(String expected, String line, int width, int col){

String content = line.substring(1+col+ col*width,1+col+ (col+1)*width).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
*/

package de.unknownreality.dataframe.frame;
package de.unknownreality.dataframe.common;

import de.unknownreality.dataframe.DataFrame;
import de.unknownreality.dataframe.DataRow;
Expand Down
Loading

0 comments on commit 5c0f156

Please sign in to comment.