FastLevelIndexWalker implements LevelIndexWalker<T> { @Override public List<T> find() { LevelNode<T> node = find(index.getRoot(), 0); return node != null ? node.getLeafList() : null; } FastLevelIndexWalker(LevelIndex<T> index, String... levelValues); @Override List<T> find();  }
@Test public void shouldFavourConcreteValuesOverDefaultWhenLookingForValue() { LevelIndex<Integer> levelIndex = levelIndex().withLevelCount(1).build(); levelIndex.add(new String[]{"*"}, 11); levelIndex.add(new String[]{"A"}, 42); FastLevelIndexWalker<Integer> walker = new FastLevelIndexWalker<Integer>(levelIndex, "A"); List<Integer> node = walker.find(); assertThat(node).containsExactly(42); }
@Test public void shouldFallBackToDefaultValueIfNoneOtherFound() { LevelIndex<Integer> levelIndex = levelIndex().withLevelCount(1).build(); levelIndex.add(new String[]{"*"}, 42); levelIndex.add(new String[]{"A"}, 11); FastLevelIndexWalker<Integer> walker = new FastLevelIndexWalker<Integer>(levelIndex, "B"); List<Integer> node = walker.find(); assertThat(node).containsExactly(42); }
@Test public void shouldReturnNullIfNothingFound() { LevelIndex<Integer> levelIndex = levelIndex().withLevelCount(1).build(); levelIndex.add(new String[]{"A"}, 10); FastLevelIndexWalker<Integer> walker = new FastLevelIndexWalker<Integer>(levelIndex, "B"); List<Integer> node = walker.find(); assertThat(node).isNull(); }
@Test(dataProvider = "findNodeSearchSet") public void shouldFindNodeFromTestSet(String[] levelValues, int expectedValue) { LevelIndex<Integer> levelIndex = levelIndex().withLevelCount(3).build(); levelIndex.add(new String[]{"A", "B", "C"}, 1); levelIndex.add(new String[]{"A", "B", "*"}, 9); levelIndex.add(new String[]{"A", "E", "D"}, 11); levelIndex.add(new String[]{"A", "*", "D"}, 12); levelIndex.add(new String[]{"A", "*", "*"}, 13); levelIndex.add(new String[]{"*", "Z", "Z"}, 21); levelIndex.add(new String[]{"*", "Z", "*"}, 22); levelIndex.add(new String[]{"*", "*", "*"}, 99); FastLevelIndexWalker<Integer> walker = new FastLevelIndexWalker<Integer>(levelIndex, levelValues); List<Integer> node = walker.find(); assertThat(node).containsExactly(expectedValue); }