// ************************************************************************
// Cache lifecycle methods
// ************************************************************************
public void constructCache(DefaultSolverScope solverScope) {
InnerScoreDirector scoreDirector = solverScope.getScoreDirector();
GenuineVariableDescriptor variableDescriptor = valueSelector.getVariableDescriptor();
Class<?> entityClass = variableDescriptor.getEntityDescriptor().getEntityClass();
long valueSize = valueSelector.getSize();
// Fail-fast when anchorTrailingChainList.size() could ever be too big
if (valueSize > (long) Integer.MAX_VALUE) {
throw new IllegalStateException("The selector (" + this
+ ") has a valueSelector (" + valueSelector
+ ") with valueSize (" + valueSize
+ ") which is higher than Integer.MAX_VALUE.");
}
// Temporary LinkedList to avoid using a bad initialCapacity
List<Object> anchorList = new LinkedList<Object>();
for (Object value : valueSelector) {
if (!entityClass.isAssignableFrom(value.getClass())) {
anchorList.add(value);
}
}
anchorTrailingChainList = new ArrayList<SubChain>(anchorList.size());
int anchorChainInitialCapacity = ((int) valueSize / anchorList.size()) + 1;
for (Object anchor : anchorList) {
List<Object> anchorChain = new ArrayList<Object>(anchorChainInitialCapacity);
Object trailingEntity = scoreDirector.getTrailingEntity(variableDescriptor, anchor);
while (trailingEntity != null) {
anchorChain.add(trailingEntity);
trailingEntity = scoreDirector.getTrailingEntity(variableDescriptor, trailingEntity);
}
if (anchorChain.size() >= minimumSubChainSize) {
anchorTrailingChainList.add(new SubChain(anchorChain));
}
}