CachedGroupBy extends AbstractIndex<C> { public T get(G g) { return cachedAggregation.getOrDefault(g, emptyAggregation); } CachedGroupBy(Function<? super C, G> groupByExtractor, Collector<? super C, ?, T> collector); T get(G g);  }
@Test void get_whenMultipleValuesForGroup_thenReturnCorrectAggregation() { ImmutableSet<TestState> states = ImmutableSet.of( new TestState(Id.create(1), CoordinateLikeTestObject.ORIGIN, 1), new TestState(Id.create(2), CoordinateLikeTestObject.ORIGIN, 3), new TestState(Id.create(3), CoordinateLikeTestObject.ORIGIN, 5)); cache.addAll(states); assertThat(groupByAggregation.get(CoordinateLikeTestObject.ORIGIN)).isEqualTo(9); }
@Test void get_whenNoElementsForGroup_thenReturnEmptyAggregation() { assertThat(groupByAggregation.get(CoordinateLikeTestObject.ORIGIN)).isEqualTo(0); }
@Test void get_whenElementRemoved_thenElementRemovedFromAggregation() { TestState stateOne = new TestState(Id.create(1), CoordinateLikeTestObject.ORIGIN, 1); TestState stateTwo = new TestState(Id.create(2), CoordinateLikeTestObject.ORIGIN, 3); cache.addAll(ImmutableSet.of(stateOne, stateTwo)); cache.delete(stateTwo.getId()); assertThat(groupByAggregation.get(CoordinateLikeTestObject.ORIGIN)).isEqualTo(1); }
@Test void get_whenElementGroupValueChanges_thenElementRemovedFromAggregation() { TestState stateOne = new TestState(Id.create(1), CoordinateLikeTestObject.ORIGIN, 1); TestState stateTwo = new TestState(Id.create(2), CoordinateLikeTestObject.ORIGIN, 3); cache.addAll(ImmutableSet.of(stateOne, stateTwo)); TestState updatedStateTwo = new TestState(Id.create(2), CoordinateLikeTestObject.create(10, 10), 3); cache.update(stateTwo, updatedStateTwo); assertThat(groupByAggregation.get(CoordinateLikeTestObject.ORIGIN)).isEqualTo(1); }
@Test void get_whenMultipleGroups_thenEachGroupHasCorrectAggregation() { CoordinateLikeTestObject otherCoordinate = CoordinateLikeTestObject.create(1, 2); ImmutableSet<TestState> states = ImmutableSet.of( new TestState(Id.create(1), CoordinateLikeTestObject.ORIGIN, 1), new TestState(Id.create(2), CoordinateLikeTestObject.ORIGIN, 3), new TestState(Id.create(3), otherCoordinate, 5)); cache.addAll(states); assertThat(groupByAggregation.get(CoordinateLikeTestObject.ORIGIN)).isEqualTo(4); assertThat(groupByAggregation.get(otherCoordinate)).isEqualTo(5); }