private ValueSelector applyCaching(
     SelectionCacheType resolvedCacheType,
     SelectionOrder resolvedSelectionOrder,
     ValueSelector valueSelector) {
   if (resolvedCacheType.isCached()
       && resolvedCacheType.compareTo(valueSelector.getCacheType()) > 0) {
     if (!(valueSelector instanceof EntityIndependentValueSelector)) {
       throw new IllegalArgumentException(
           "The valueSelectorConfig ("
               + this
               + ") with resolvedSelectionOrder ("
               + resolvedSelectionOrder
               + ") needs to be based on a EntityIndependentValueSelector."
               + " Check your @"
               + ValueRange.class.getSimpleName()
               + " annotations.");
     }
     valueSelector =
         new CachingValueSelector(
             (EntityIndependentValueSelector) valueSelector,
             resolvedCacheType,
             resolvedSelectionOrder.toRandomSelectionBoolean());
   }
   return valueSelector;
 }
  public void runCacheType(SelectionCacheType cacheType, int timesCalled) {
    MoveSelector childMoveSelector =
        SelectorTestUtils.mockMoveSelector(
            DummyMove.class,
            new DummyMove("e1"),
            new DummyMove("e2"),
            new DummyMove("e3"),
            new DummyMove("e4"));

    SelectionFilter<DummyMove> filter =
        new SelectionFilter<DummyMove>() {
          public boolean accept(ScoreDirector scoreDirector, DummyMove move) {
            return !move.getCode().equals("e3");
          }
        };
    List<SelectionFilter> filterList = Arrays.<SelectionFilter>asList(filter);
    MoveSelector moveSelector = new FilteringMoveSelector(childMoveSelector, filterList);
    if (cacheType.isCached()) {
      moveSelector = new CachingMoveSelector(moveSelector, cacheType, false);
    }

    DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
    moveSelector.solvingStarted(solverScope);

    AbstractSolverPhaseScope phaseScopeA = mock(AbstractSolverPhaseScope.class);
    when(phaseScopeA.getSolverScope()).thenReturn(solverScope);
    moveSelector.phaseStarted(phaseScopeA);

    AbstractStepScope stepScopeA1 = mock(AbstractStepScope.class);
    when(stepScopeA1.getPhaseScope()).thenReturn(phaseScopeA);
    moveSelector.stepStarted(stepScopeA1);
    assertAllCodesOfMoveSelector(
        moveSelector, (cacheType.isNotCached() ? 4L : 3L), "e1", "e2", "e4");
    moveSelector.stepEnded(stepScopeA1);

    AbstractStepScope stepScopeA2 = mock(AbstractStepScope.class);
    when(stepScopeA2.getPhaseScope()).thenReturn(phaseScopeA);
    moveSelector.stepStarted(stepScopeA2);
    assertAllCodesOfMoveSelector(
        moveSelector, (cacheType.isNotCached() ? 4L : 3L), "e1", "e2", "e4");
    moveSelector.stepEnded(stepScopeA2);

    moveSelector.phaseEnded(phaseScopeA);

    AbstractSolverPhaseScope phaseScopeB = mock(AbstractSolverPhaseScope.class);
    when(phaseScopeB.getSolverScope()).thenReturn(solverScope);
    moveSelector.phaseStarted(phaseScopeB);

    AbstractStepScope stepScopeB1 = mock(AbstractStepScope.class);
    when(stepScopeB1.getPhaseScope()).thenReturn(phaseScopeB);
    moveSelector.stepStarted(stepScopeB1);
    assertAllCodesOfMoveSelector(
        moveSelector, (cacheType.isNotCached() ? 4L : 3L), "e1", "e2", "e4");
    moveSelector.stepEnded(stepScopeB1);

    AbstractStepScope stepScopeB2 = mock(AbstractStepScope.class);
    when(stepScopeB2.getPhaseScope()).thenReturn(phaseScopeB);
    moveSelector.stepStarted(stepScopeB2);
    assertAllCodesOfMoveSelector(
        moveSelector, (cacheType.isNotCached() ? 4L : 3L), "e1", "e2", "e4");
    moveSelector.stepEnded(stepScopeB2);

    AbstractStepScope stepScopeB3 = mock(AbstractStepScope.class);
    when(stepScopeB3.getPhaseScope()).thenReturn(phaseScopeB);
    moveSelector.stepStarted(stepScopeB3);
    assertAllCodesOfMoveSelector(
        moveSelector, (cacheType.isNotCached() ? 4L : 3L), "e1", "e2", "e4");
    moveSelector.stepEnded(stepScopeB3);

    moveSelector.phaseEnded(phaseScopeB);

    moveSelector.solvingEnded(solverScope);

    verifySolverPhaseLifecycle(childMoveSelector, 1, 2, 5);
    verify(childMoveSelector, times(timesCalled)).iterator();
    verify(childMoveSelector, times(timesCalled)).getSize();
  }