MemoizationListImpl extends LiveListBase<E> implements MemoizationList<E>, UnmodifiableByDefaultLiveList<E> { @Override public void force(int from, int to) { if(!isObservingInputs()) { throw new IllegalStateException( "Cannot force items when memoization is off." + " To turn memoization on, you have to be observing this" + " list or its memoizedItems."); } Lists.checkRange(from, to, size()); for(int i = from; i < to; ++i) { if(!sparseList.isPresent(i)) { E elem = source.get(i); if(sparseList.setIfAbsent(i, elem)) { int presentBefore = sparseList.getPresentCountBefore(i); memoizedItems.prepareNotifications(ProperLiveList.elemInsertion(presentBefore)); } } } memoizedItems.publishNotifications(); } MemoizationListImpl(ObservableList<E> source); @Override E get(int index); @Override void force(int from, int to); @Override int size(); @Override LiveList<E> memoizedItems(); @Override boolean isMemoized(int index); @Override Optional<E> getIfMemoized(int index); @Override int getMemoizedCountBefore(int position); @Override int getMemoizedCountAfter(int position); @Override int getMemoizedCount(); @Override void forget(int from, int to); @Override int indexOfMemoizedItem(int index); @Override IndexRange getMemoizedItemsRange();  }
@Test public void testForce() { LiveList<Integer> source = new LiveArrayList<>(0, 1, 2, 3, 4, 5, 6); MemoizationList<Integer> memoizing = source.memoize(); LiveList<Integer> memoized = memoizing.memoizedItems(); memoizing.pin(); memoizing.get(3); assertEquals(Collections.singletonList(3), memoized); Counter counter = new Counter(); memoized.observeChanges(ch -> { counter.inc(); assertEquals(2, ch.getModificationCount()); ListModification<?> mod1 = ch.getModifications().get(0); ListModification<?> mod2 = ch.getModifications().get(1); assertEquals(0, mod1.getFrom()); assertEquals(0, mod1.getRemovedSize()); assertEquals(Arrays.asList(1, 2), mod1.getAddedSubList()); assertEquals(3, mod2.getFrom()); assertEquals(0, mod2.getRemovedSize()); assertEquals(Arrays.asList(4, 5), mod2.getAddedSubList()); }); memoizing.force(1, 6); assertEquals(1, counter.get()); }
@Test(expected=IllegalStateException.class) public void testForceIsNotAllowedWhenUnobserved() { MemoizationList<Integer> list = new LiveArrayList<>(0, 1, 2, 3, 4, 5, 6).memoize(); list.force(2, 4); }