Skip to content

Commit

Permalink
fix: Tree.getAllLeaves() only yielding one value
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Sep 14, 2024
1 parent 822e7fc commit 2ff27c5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>software.coley</groupId>
<artifactId>extra-collections</artifactId>
<version>1.5.0</version>
<version>1.5.1</version>

<name>Extra Collections</name>
<description>Extra useful collection types and utilities</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.annotation.Nonnull;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Supplier;

Expand Down Expand Up @@ -141,4 +142,27 @@ private NavigableTree<K, V> from(NavigableMap<K, Tree<K, V>> subMap) {
subTree.putAll(subMap);
return subTree;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

NavigableTreeImpl<?, ?> that = (NavigableTreeImpl<?, ?>) o;

return Objects.equals(value, that.value);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "NavigableTreeImpl{" + value + '}';
}
}
23 changes: 23 additions & 0 deletions src/main/java/software/coley/collections/tree/SortedTreeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,27 @@ private SortedTree<K, V> from(SortedMap<K, Tree<K, V>> subMap) {
subTree.putAll(subMap);
return subTree;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

SortedTreeImpl<?, ?> that = (SortedTreeImpl<?, ?>) o;

return value.equals(that.value);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + value.hashCode();
return result;
}

@Override
public String toString() {
return "SortedTreeImpl{" + value + '}';
}
}
18 changes: 18 additions & 0 deletions src/test/java/software/coley/collections/TreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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

Expand Down Expand Up @@ -131,4 +132,21 @@ public void test_navigable_descending() {
assertEquals("keyA", descending.lastKey());
assertEquals(Lists.reversed(keys), new ArrayList<>(descending.keySet()));
}

@Test
public void test_tree_gets_all_leaves() {
SortedTree<String, String> tree = new SortedTreeImpl<>("root");
// Populate original tree
tree.putTree("key1", "value1");
Tree<String, String> sub1 = tree.get("key1");
sub1.putTree("key1-a", "value1a");
sub1.putTree("key1-b", "value1b");
tree.putTree("key2", "value2");
tree.putTree("key3", "value3");
tree.putTree("key4", "value4");
tree.putTree("key5", "value5");
// There are 6 leaves, as key1 has two children
Set<Tree<String, String>> foo = tree.getAllLeaves();
assertEquals(6, foo.size());
}
}

0 comments on commit 2ff27c5

Please sign in to comment.