diff --git a/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java b/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java index f273994..9c24a19 100644 --- a/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java +++ b/hofund-core/src/main/java/dev/logchange/hofund/connection/HofundConnectionsTable.java @@ -2,9 +2,9 @@ import dev.logchange.hofund.AsciiTable; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; public class HofundConnectionsTable { @@ -13,15 +13,10 @@ public class HofundConnectionsTable { private final List connections; public HofundConnectionsTable(List connectionsProviders) { - List connections = new ArrayList<>(); - - for (HofundConnectionsProvider connectionsProvider : connectionsProviders) { - connections.addAll(connectionsProvider.getConnections()); - } - - connections.sort((d1, d2) -> d2.getType().compareTo(d1.getType())); - - this.connections = connections; + this.connections = connectionsProviders.stream() + .flatMap(t -> t.getConnections().stream()) + .sorted((d1, d2) -> d2.getType().compareTo(d1.getType())) + .collect(Collectors.toList()); } public String print() { diff --git a/hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java b/hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java index ff34840..2c06067 100644 --- a/hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java +++ b/hofund-core/src/test/java/dev/logchange/hofund/connection/HofundConnectionsTableTest.java @@ -33,6 +33,34 @@ void testSortByType() { assertEquals(expected, result); } + @Test + void testSortByTypeWithTwoProviders() { + // given: + TastableHofundConnectionsProvider provider1 = new TastableHofundConnectionsProvider(); + TastableHofundConnectionsProvider provider2 = new TastableHofundConnectionsProvider(); + + HofundConnectionsTable table = new HofundConnectionsTable(Arrays.asList(provider1, provider2)); + String expected = + "+----------+---------+--------+------+\n" + + "| TYPE | NAME | STATUS | URL |\n" + + "+----------+---------+--------+------+\n" + + "| HTTP | target1 | UP | fake |\n" + + "| HTTP | target3 | UP | fake |\n" + + "| HTTP | target1 | UP | fake |\n" + + "| HTTP | target3 | UP | fake |\n" + + "| DATABASE | target2 | UP | fake |\n" + + "| DATABASE | target4 | UP | fake |\n" + + "| DATABASE | target2 | UP | fake |\n" + + "| DATABASE | target4 | UP | fake |\n" + + "+----------+---------+--------+------+\n"; + + // when: + String result = table.print(); + + // then: + assertEquals(expected, result); + } + private static class TastableHofundConnectionsProvider implements HofundConnectionsProvider { public List getConnections() {