Last active
April 22, 2020 21:00
-
-
Save stefanofago73/67fed1c917aac103cd8cb1379a89f4d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static it.fago.experiments.EderChallenge.Operation.from; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Optional; | |
public class EderChallenge { | |
public static void main(String[] args) { | |
original(); | |
maybeFunctional(); | |
} | |
private static void original() { | |
String table = "table1"; | |
String previousTable = "tableX"; | |
Map<String, List<Field<?>>> childrenPerTable = new HashMap<>(); | |
childrenPerTable.put("table0", new ArrayList<>()); | |
List<Field<?>> children = childrenPerTable.get(table); | |
if (children == null) { | |
childrenPerTable.put(previousTable = table, children = new ArrayList<>()); | |
} | |
System.out.printf("\n%s\n%s\n%s\n",table,previousTable,children); | |
} | |
private static void maybeFunctional() { | |
String table = "table1"; | |
String previousTable = "tableX"; | |
Map<String, List<Field<?>>> childrenPerTable = new HashMap<>(); | |
childrenPerTable.put("table0", new ArrayList<>()); | |
Operation newChildrenOp = | |
from(table, previousTable, childrenPerTable) | |
.newChildren(); | |
System.out.printf("\n%s\n%s\n%s\n",newChildrenOp.table(),newChildrenOp.previousTable(),newChildrenOp.children()); | |
} | |
/////////////////////////////////////////////////////////// | |
// | |
// | |
// | |
/////////////////////////////////////////////////////////// | |
public static class Operation { | |
private final String table; | |
private final String previousTable; | |
private final Map<String, List<Field<?>>> childrenTable; | |
private Operation(String table, String previousTable, Map<String, List<Field<?>>> childrenTable) { | |
this.table = table; | |
this.previousTable = previousTable; | |
this.childrenTable = childrenTable; | |
} | |
public final List<Field<?>> children() { | |
return this.childrenTable.get(table); | |
} | |
public final String table() { | |
return table; | |
} | |
public final String previousTable() { | |
return previousTable; | |
} | |
public static final Operation from(Operation operation) { | |
Map<String, List<Field<?>>> result = new HashMap<>(operation.childrenTable); | |
result.put(operation.table, new ArrayList<>()); | |
return new Operation(operation.table, operation.table, result); | |
} | |
public static final Operation from(String table, String previousTable, | |
Map<String, List<Field<?>>> childrenTable) { | |
return new Operation(table, previousTable, childrenTable); | |
} | |
public final Operation newChildren() { | |
return Optional | |
.ofNullable(children()) | |
.map(kids -> this) | |
.orElseGet(() -> from(this)); | |
} | |
}// END | |
}// END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment