import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
public class Bina {
public static class FormattedRowsSupplier implements Iterable<List<List<String>>> {
private final FormattedTables tables;
private FormattedRowsSupplier(FormattedTables tables) {
this.tables = tables;
}
@NotNull
@Override
public Iterator<List<List<String>>> iterator() {
return new Iterator<>() {
private int currentIndex = 0;
@Override
public boolean hasNext() {
return currentIndex < tables.tables().size();
}
@Override
public List<List<String>> next() {
List<List<String>> result = new ArrayList<>();
for (FormattedRow fr : tables.tables().get(currentIndex).rows()) {
result.add(
List.of(
fr.at(),
fr.asset(),
fr.type(),
fr.free(),
fr.locked(),
fr.sum(),
fr.free_usd(),
fr.locked_usd(),
fr.sum1_usd(),
fr.sum2_usd()));
}
currentIndex++;
return result;
}
};
}
}
private record FormattedTables(List<FormattedTable> tables) {}
private record FormattedTable(List<FormattedRow> rows) {}
private record FormattedRow(
String at,
String asset,
String type,
String free,
String locked,
String sum,
String free_usd,
String locked_usd,
String sum1_usd,
String sum2_usd) {}
private enum Type {
base,
normal,
hide,
ignore,
sum
}
private record Tables(List<Table> tables) {
void sort() {
tables().forEach(Table::sort);
tables().sort(Comparator.comparing(t -> t.rows().get(0).at()));
}
}
private record Table(List<Row> rows) {
void sort() {
rows().sort(Comparator.comparing(Row::at));
rows().sort(Comparator.comparingDouble(r -> r.doubleValues()[sum1]));
}
}
private record Row(Date at, String asset, Type type, double[] doubleValues) {}
private static final int sum1 = 8;
private static Tables splitLargeTable(Table table) {
table.sort();
return new Tables(
table.rows().stream()
.collect(
Collectors.groupingBy(
Row::at, LinkedHashMap::new, Collectors.toCollection(ArrayList::new)))
.values()
.stream()
.map(Table::new)
.collect(Collectors.toCollection(ArrayList::new)));
}
private static Tables getAllData() throws Exception {
List<List<Object>> data = MDB.getBina();
Tables tables =
splitLargeTable(
new Table(
data.stream()
.map(
row ->
new Row(
new Date(((Timestamp) row.get(0)).getTime()),
(String) row.get(1),
Type.valueOf((String) row.get(2)),
IntStream.range(3, 10)
.mapToDouble(i -> (double) row.get(i))
.toArray()))
.collect(Collectors.toCollection(ArrayList::new))));
tables.sort();
tables
.tables()
.forEach(
table ->
table
.rows()
.add(
new Row(
table.rows().get(0).at(),
"---",
Type.sum,
IntStream.range(0, sum1)
.mapToDouble(
i ->
table.rows().stream()
.mapToDouble(r -> r.doubleValues()[i])
.sum())
.toArray())));
return tables;
}
public static FormattedRowsSupplier getAllDataUnmodifiable() throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
String formatter = "%.2f";
return new FormattedRowsSupplier(
new FormattedTables(
getAllData().tables().stream()
.map(
table ->
new FormattedTable(
table.rows().stream()
.map(
row ->
new FormattedRow(
df.format(row.at()),
row.asset(),
row.type().name(),
String.format(formatter, row.doubleValues()[0]),
String.format(formatter, row.doubleValues()[1]),
String.format(formatter, row.doubleValues()[2]),
String.format(formatter, row.doubleValues()[3]),
String.format(formatter, row.doubleValues()[4]),
String.format(formatter, row.doubleValues()[5]),
String.format(formatter, row.doubleValues()[6])))
.toList()))
.toList()));
}