Skip to content

Commit

Permalink
Issue #20: ITableSource provides Objects instead of Strings.
Browse files Browse the repository at this point in the history
ITableSource is not backward compatible.
It provides iteration of Object cells instead of String cells.
  • Loading branch information
Michal Jaron committed Mar 13, 2023
1 parent 6562150 commit e46bb3b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 79 deletions.
12 changes: 6 additions & 6 deletions src/main/java/pl/mjaron/etudes/table/BeanTableSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Iterable<String> getHeaders() {
}

@Override
public Iterator<Iterable<String>> iterator() {
public Iterator<Iterable<Object>> iterator() {
return new RowsIterator<>(tClass, tFields, values);
}

Expand All @@ -84,7 +84,7 @@ public Iterator<Iterable<String>> iterator() {
*
* @param <BeanT> Type of the object.
*/
private static class RowsIterator<BeanT> implements Iterator<Iterable<String>> {
private static class RowsIterator<BeanT> implements Iterator<Iterable<Object>> {

final Iterator<BeanT> beanIterator;
final private Class<BeanT> tClass;
Expand All @@ -107,11 +107,11 @@ public boolean hasNext() {
* @return Next table row.
*/
@Override
public Iterable<String> next() {
public Iterable<Object> next() {
final BeanT bean = beanIterator.next();
List<String> stringSeries = new ArrayList<>(tFields.length);
Obj.visitFieldValues(bean, this.tClass, this.tFields, (name, value) -> stringSeries.add(Str.orEmpty(value)));
return stringSeries;
List<Object> record = new ArrayList<>(tFields.length);
Obj.visitFieldValues(bean, this.tClass, this.tFields, (name, value) -> record.add(value));
return record;
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/pl/mjaron/etudes/table/ITableSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Abstract source which provides string-based data usable for printing table.
*/
public interface ITableSource extends Iterable<Iterable<String>> {
public interface ITableSource extends Iterable<Iterable<Object>> {

/**
* @return Width of table.
Expand All @@ -40,7 +40,7 @@ public interface ITableSource extends Iterable<Iterable<String>> {
* @return Iterator which allows to read all rows sequentially.
*/
@Override
Iterator<Iterable<String>> iterator();
Iterator<Iterable<Object>> iterator();

default boolean hasHeaders() {
return getHeaders() != null;
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/pl/mjaron/etudes/table/ListTableSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package pl.mjaron.etudes.table;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

Expand All @@ -43,16 +44,20 @@ public Iterable<String> getHeaders() {
}

@Override
public Iterator<Iterable<String>> iterator() {
public Iterator<Iterable<Object>> iterator() {
return new ArrayIterator<>(array);
}

private static class ArrayIterator<T> implements Iterator<Iterable<String>> {
private final List<List<T>> array;
/**
* Provides rows of array table source.
*
* @param <T> Type of table cell.
* @since 0.2.0
*/
private static class ArrayIterator<T> implements Iterator<Iterable<Object>> {
private final Iterator<List<T>> it;

ArrayIterator(final List<List<T>> array) {
this.array = array;
this.it = array.iterator();
}

Expand All @@ -62,8 +67,8 @@ public boolean hasNext() {
}

@Override
public Iterable<String> next() {
return StringSeries.from(it.next());
public Iterable<Object> next() {
return Collections.unmodifiableList(it.next());
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/pl/mjaron/etudes/table/ManipulatingTableSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Iterable<String> getHeaders() {
}

@Override
public Iterator<Iterable<String>> iterator() {
public Iterator<Iterable<Object>> iterator() {
return new ManipulatingTableSourceIterator(underlyingSource, columnOrder);
}

Expand All @@ -68,21 +68,21 @@ public boolean hasHeaders() {
}
}

class ManipulatingTableSourceRow implements Iterable<String> {
class ManipulatingTableSourceRow implements Iterable<Object> {

private final ArrayList<Integer> columnOrder;

private final IRandomIterator<String> underlyingRowRandomIterator;
private final IRandomIterator<Object> underlyingRowRandomIterator;

public ManipulatingTableSourceRow(ArrayList<Integer> columnOrder, Iterable<String> underlyingRow) {
public ManipulatingTableSourceRow(ArrayList<Integer> columnOrder, Iterable<Object> underlyingRow) {
this.columnOrder = columnOrder;
this.underlyingRowRandomIterator = IRandomIterator.from(underlyingRow);
}

@NotNull
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
public Iterator<Object> iterator() {
return new Iterator<Object>() {

int currentColumnIndex = -1;

Expand All @@ -92,7 +92,7 @@ public boolean hasNext() {
}

@Override
public String next() {
public Object next() {
++currentColumnIndex;
underlyingRowRandomIterator.setPosition(columnOrder.get(currentColumnIndex));
return underlyingRowRandomIterator.getCurrent();
Expand All @@ -101,10 +101,10 @@ public String next() {
}
}

class ManipulatingTableSourceIterator implements Iterator<Iterable<String>> {
class ManipulatingTableSourceIterator implements Iterator<Iterable<Object>> {

private final ArrayList<Integer> columnOrder;
private final Iterator<Iterable<String>> underlyingIterator;
private final Iterator<Iterable<Object>> underlyingIterator;

public ManipulatingTableSourceIterator(ITableSource underlyingSource, ArrayList<Integer> columnOrder) {
this.columnOrder = columnOrder;
Expand All @@ -127,7 +127,7 @@ public boolean hasNext() {
* @return Next row.
*/
@Override
public Iterable<String> next() {
public Iterable<Object> next() {
return new ManipulatingTableSourceRow(columnOrder, underlyingIterator.next());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@

import java.util.Iterator;

public abstract class SeriesIteratorFactory {
public abstract class ObjectSeriesIteratorFactory {

private SeriesIteratorFactory() {
}

public static Iterator<String> from(final byte[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final byte[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -36,14 +33,14 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}

public static Iterator<String> from(final short[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final short[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -52,14 +49,14 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}

public static Iterator<String> from(final int[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final int[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -68,14 +65,14 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}

public static Iterator<String> from(final long[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final long[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -84,14 +81,14 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}

public static Iterator<String> from(final float[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final float[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -100,14 +97,14 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}

public static Iterator<String> from(final double[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final double[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -116,14 +113,14 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}

public static Iterator<String> from(final boolean[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final boolean[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -132,14 +129,14 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}

public static Iterator<String> from(final char[] src) {
return new Iterator<String>() {
public static Iterator<Object> from(final char[] src) {
return new Iterator<Object>() {
private int idx = 0;

@Override
Expand All @@ -148,8 +145,8 @@ public boolean hasNext() {
}

@Override
public String next() {
return "" + src[idx++];
public Object next() {
return src[idx++];
}
};
}
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/pl/mjaron/etudes/table/RenderOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@

package pl.mjaron.etudes.table;

import pl.mjaron.etudes.Exc;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public abstract class RenderOperation {

public static void execute(final RenderContext context) {
Expand All @@ -48,11 +42,11 @@ public static void execute(final RenderContext context) {
runtime.setHeaderState(false);
}

for (final Iterable<String> row : source) {
for (final Iterable<Object> row : source) {
writer.beginRow();
runtime.resetColumn();
for (final String cell : row) {
writer.writeCell(runtime.getEscaper().escape(cell));
for (final Object cell : row) {
writer.writeCell(runtime.getEscaper().escape(cell.toString())); // @todo Use value converter instead of direct toString().
runtime.nextColumn();
}
writer.endRow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public abstract class TableColumnsWidthDetector {
* @param widths array of maximum column widths.
* @param series Single row (record) of data. Used to check the cell width.
*/
private static void applyRow(final int[] widths, final Iterable<String> series, final IEscaper escaper) {
private static void applyRow(final int[] widths, final Iterable<?> series, final IEscaper escaper) {
int i = 0;
for (final String entry : series) {
for (final Object entry : series) {
final int oldEntryWidth = widths[i];
final int newEntryWidth = Integer.max(oldEntryWidth, escaper.escape(entry).length());
final int newEntryWidth = Integer.max(oldEntryWidth, escaper.escape(entry.toString()).length()); // @todo Use value converter instead of direct toString().
widths[i] = newEntryWidth;
++i;
}
Expand All @@ -55,7 +55,7 @@ public static int[] compute(@NotNull final ITableSource source, @NotNull final I
if (source.hasHeaders()) {
applyRow(widths, source.getHeaders(), escaper);
}
for (final Iterable<String> row : source) {
for (final Iterable<Object> row : source) {
applyRow(widths, row, escaper);
}
return widths;
Expand Down
Loading

0 comments on commit e46bb3b

Please sign in to comment.