Skip to content

Commit

Permalink
Adding stream merger solution
Browse files Browse the repository at this point in the history
  • Loading branch information
josdem committed Apr 27, 2024
1 parent 418887a commit 005e2e6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.josdem.kata;

public class CustomStream {
public class CustomStream implements Comparable<CustomStream>{
private final int id;
public CustomStream(int id) {
this.id = id;
Expand All @@ -9,4 +9,9 @@ public CustomStream(int id) {
public int getId() {
return id;
}

@Override
public int compareTo(CustomStream that) {
return Integer.compare(this.id, that.id);
}
}
8 changes: 5 additions & 3 deletions stream-merger/src/main/java/com/josdem/kata/StreamMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public record StreamMerger(Set<CustomStream> streams) {

public List<Integer> mergeInto(CustomOutputStream stream) {
streams.forEach(it -> stream.storeValue(it.getId()));
return stream.getList();
public Set<CustomStream> mergeInto(CustomOutputStream stream) {
TreeSet<CustomStream> treeSet = new TreeSet<>(streams);
treeSet.forEach(item -> stream.storeValue(item.getId()));
return treeSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ void shouldGetOrderedStreamCollection() {
streams.add(cst3);

StreamMerger streamMerger = new StreamMerger(streams);
List<Integer> result = streamMerger.mergeInto(new CustomOutputStream());
Set<CustomStream> result = streamMerger.mergeInto(new CustomOutputStream());

assertEquals(3, result.size(), "should contain three streams");
assertEquals(0, result.get(0), "should have cst3 value");
assertEquals(1, result.get(1), "should have cst2 value");
assertEquals(2, result.get(2), "should have cst1 value");
assertEquals(cst3, result.toArray()[0], "should have cst3 value");
assertEquals(cst2, result.toArray()[1], "should have cst2 value");
assertEquals(cst1, result.toArray()[2], "should have cst1 value");
}
}

0 comments on commit 005e2e6

Please sign in to comment.