Skip to content

Commit 1ccfb2d

Browse files
committed
fix: Tree.getAllLeaves() only yielding one value
1 parent 822e7fc commit 1ccfb2d

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.coley</groupId>
88
<artifactId>extra-collections</artifactId>
9-
<version>1.5.0</version>
9+
<version>1.5.1</version>
1010

1111
<name>Extra Collections</name>
1212
<description>Extra useful collection types and utilities</description>

src/main/java/software/coley/collections/tree/NavigableTreeImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javax.annotation.Nonnull;
66
import java.util.NavigableMap;
7+
import java.util.Objects;
78
import java.util.TreeMap;
89
import java.util.function.Supplier;
910

@@ -141,4 +142,25 @@ private NavigableTree<K, V> from(NavigableMap<K, Tree<K, V>> subMap) {
141142
subTree.putAll(subMap);
142143
return subTree;
143144
}
145+
146+
@Override
147+
public boolean equals(Object o) {
148+
if (this == o) return true;
149+
if (!(o instanceof Tree) || !super.equals(o)) return false;
150+
151+
Tree<?, ?> that = (Tree<?, ?>) o;
152+
return Objects.equals(value, that.getValue());
153+
}
154+
155+
@Override
156+
public int hashCode() {
157+
int result = super.hashCode();
158+
result = 31 * result + (value != null ? value.hashCode() : 0);
159+
return result;
160+
}
161+
162+
@Override
163+
public String toString() {
164+
return "NavigableTreeImpl{" + value + '}';
165+
}
144166
}

src/main/java/software/coley/collections/tree/SortedTreeImpl.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import software.coley.collections.delegate.DelegatingSortedMap;
44

55
import javax.annotation.Nonnull;
6+
import java.util.Objects;
67
import java.util.SortedMap;
78
import java.util.TreeMap;
89
import java.util.function.Supplier;
@@ -117,4 +118,25 @@ private SortedTree<K, V> from(SortedMap<K, Tree<K, V>> subMap) {
117118
subTree.putAll(subMap);
118119
return subTree;
119120
}
121+
122+
@Override
123+
public boolean equals(Object o) {
124+
if (this == o) return true;
125+
if (!(o instanceof Tree) || !super.equals(o)) return false;
126+
127+
Tree<?, ?> that = (Tree<?, ?>) o;
128+
return Objects.equals(value, that.getValue());
129+
}
130+
131+
@Override
132+
public int hashCode() {
133+
int result = super.hashCode();
134+
result = 31 * result + value.hashCode();
135+
return result;
136+
}
137+
138+
@Override
139+
public String toString() {
140+
return "SortedTreeImpl{" + value + '}';
141+
}
120142
}

src/test/java/software/coley/collections/TreeTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.ArrayList;
77
import java.util.List;
8+
import java.util.Set;
89

910
import static org.junit.jupiter.api.Assertions.assertEquals;
1011

@@ -131,4 +132,21 @@ public void test_navigable_descending() {
131132
assertEquals("keyA", descending.lastKey());
132133
assertEquals(Lists.reversed(keys), new ArrayList<>(descending.keySet()));
133134
}
135+
136+
@Test
137+
public void test_tree_gets_all_leaves() {
138+
SortedTree<String, String> tree = new SortedTreeImpl<>("root");
139+
// Populate original tree
140+
tree.putTree("key1", "value1");
141+
Tree<String, String> sub1 = tree.get("key1");
142+
sub1.putTree("key1-a", "value1a");
143+
sub1.putTree("key1-b", "value1b");
144+
tree.putTree("key2", "value2");
145+
tree.putTree("key3", "value3");
146+
tree.putTree("key4", "value4");
147+
tree.putTree("key5", "value5");
148+
// There are 6 leaves, as key1 has two children
149+
Set<Tree<String, String>> foo = tree.getAllLeaves();
150+
assertEquals(6, foo.size());
151+
}
134152
}

0 commit comments

Comments
 (0)