|
| 1 | +package tudelft.ti2806.pl3.data.wrapper.util; |
| 2 | + |
| 3 | +import tudelft.ti2806.pl3.data.wrapper.CombineWrapper; |
| 4 | +import tudelft.ti2806.pl3.data.wrapper.SingleWrapper; |
| 5 | +import tudelft.ti2806.pl3.data.wrapper.Wrapper; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public final class CombineWrapUtil { |
| 15 | + private CombineWrapUtil(){ |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Wraps a list into a new layer and reconnects the new layer. |
| 20 | + * |
| 21 | + * @param nonCombinedNodes |
| 22 | + * the nodes that are not combined |
| 23 | + * @param combinedNodes |
| 24 | + * the nodes that are combined, and are already of the new layer |
| 25 | + * @return a list containing a new layer over the previous layer |
| 26 | + */ |
| 27 | + public static List<Wrapper> wrapAndReconnect(List<Wrapper> nonCombinedNodes, List<CombineWrapper> combinedNodes) { |
| 28 | + Map<Wrapper, Wrapper> map = wrapList(nonCombinedNodes, combinedNodes); |
| 29 | + reconnectLayer(nonCombinedNodes, combinedNodes, map); |
| 30 | + List<Wrapper> list = new ArrayList<>(new HashSet<>(map.values())); |
| 31 | + Collections.sort(list); |
| 32 | + return list; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Reconnects the given layer, using the connections from the previous layer and applying them to the new layer. |
| 37 | + * |
| 38 | + * @param nonCombinedNodes |
| 39 | + * the nodes that are not combined |
| 40 | + * @param combinedNodes |
| 41 | + * the nodes that are combined, and are already of the new layer |
| 42 | + * @param map |
| 43 | + * a map mapping all nodes from the previous layer to the new layer |
| 44 | + */ |
| 45 | + public static void reconnectLayer(List<Wrapper> nonCombinedNodes, List<CombineWrapper> combinedNodes, |
| 46 | + Map<Wrapper, Wrapper> map) { |
| 47 | + reconnectNonCombinedNodes(nonCombinedNodes, map); |
| 48 | + reconnectCombinedNodes(combinedNodes, map); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Reconnects the nodes which where not combined to the given layer. |
| 53 | + * |
| 54 | + * @param nonCombinedNodes |
| 55 | + * the nodes that are not combined |
| 56 | + * @param map |
| 57 | + * a map mapping all nodes from the previous layer to the new layer |
| 58 | + */ |
| 59 | + private static void reconnectNonCombinedNodes(List<Wrapper> nonCombinedNodes, Map<Wrapper, Wrapper> map) { |
| 60 | + for (Wrapper node : nonCombinedNodes) { |
| 61 | + Wrapper newWrapper = map.get(node); |
| 62 | + node.getIncoming().stream().filter(in -> !newWrapper.getIncoming().contains(map.get(in))) |
| 63 | + .forEach(in -> newWrapper.getIncoming().add(map.get(in))); |
| 64 | + node.getOutgoing().stream().filter(out -> !newWrapper.getOutgoing().contains(map.get(out))) |
| 65 | + .forEach(out -> newWrapper.getOutgoing().add(map.get(out))); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * Reconnects the nodes which where combined to the given layer. |
| 72 | + * |
| 73 | + * @param combinedNodes |
| 74 | + * the nodes that are combined, and are already of the new layer |
| 75 | + * @param map |
| 76 | + * a map mapping all nodes from the previous layer to the new layer |
| 77 | + */ |
| 78 | + private static void reconnectCombinedNodes(List<CombineWrapper> combinedNodes, Map<Wrapper, Wrapper> map) { |
| 79 | + for (CombineWrapper comNode : combinedNodes) { |
| 80 | + comNode.getFirst().getIncoming().stream().filter(in -> !comNode.getIncoming() |
| 81 | + .contains(map.get(in))) .forEach(in -> comNode.getIncoming().add(map.get(in))); |
| 82 | + comNode.getLast().getOutgoing().stream().filter(out -> !comNode.getOutgoing() |
| 83 | + .contains(map.get(out))) .forEach(out -> comNode.getOutgoing() |
| 84 | + .add(map.get(out))); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Creates a new layer from the given nodes and creates a map mapping all nodes from the previous layer |
| 90 | + * to the new layer. |
| 91 | + * |
| 92 | + * @param nonCombinedNodes |
| 93 | + * the nodes that are not combined |
| 94 | + * @param combinedNodes |
| 95 | + * the nodes that are combined, and are already of the new layer |
| 96 | + * @return a map mapping all nodes from the previous layer to the new layer |
| 97 | + */ |
| 98 | + private static Map<Wrapper, Wrapper> wrapList(List<Wrapper> nonCombinedNodes, |
| 99 | + List<CombineWrapper> combinedNodes) { |
| 100 | + Map<Wrapper, Wrapper> map = new HashMap<>(); |
| 101 | + for (Wrapper node : nonCombinedNodes) { |
| 102 | + SingleWrapper newWrapper = new SingleWrapper(node); |
| 103 | + map.put(node, newWrapper); |
| 104 | + } |
| 105 | + for (CombineWrapper verNode : combinedNodes) { |
| 106 | + for (Wrapper node : verNode.getNodeList()) { |
| 107 | + map.put(node, verNode); |
| 108 | + } |
| 109 | + } |
| 110 | + return map; |
| 111 | + } |
| 112 | +} |
0 commit comments