Skip to content

Commit

Permalink
Merge pull request #53 from logchange/sorting
Browse files Browse the repository at this point in the history
Sorting connections tabel by type
  • Loading branch information
marwin1991 authored Nov 19, 2024
2 parents 3c577d4 + 6ccf281 commit 271d770
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -13,15 +13,10 @@ public class HofundConnectionsTable {
private final List<HofundConnection> connections;

public HofundConnectionsTable(List<HofundConnectionsProvider> connectionsProviders) {
List<HofundConnection> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HofundConnection> getConnections() {
Expand Down

0 comments on commit 271d770

Please sign in to comment.