private PoolableStore findUntriedLargestPoolableStore(Collection<PoolableStore> from, Collection<PoolableStore> tried) { PoolableStore largestPoolableStore = null; for (PoolableStore poolableStore : from) { if (alreadyTried(tried, poolableStore)) { continue; } if (largestPoolableStore == null || getSizeInBytes(poolableStore) > getSizeInBytes(largestPoolableStore)) { largestPoolableStore = poolableStore; } } return largestPoolableStore; }
/** * {@inheritDoc} */ public boolean freeSpace(Collection<PoolableStore> from, long bytes) { if (from == null || from.isEmpty()) { return false; } long remainingSizeInBytes = bytes; Collection<PoolableStore> tried = new ArrayList<PoolableStore>(); while (tried.size() != from.size()) { PoolableStore largestPoolableStore = findUntriedLargestPoolableStore(from, tried); long beforeEvictionSize = getSizeInBytes(largestPoolableStore); if (!evict(1, bytes, largestPoolableStore)) { tried.add(largestPoolableStore); continue; } long afterEvictionSize = getSizeInBytes(largestPoolableStore); remainingSizeInBytes -= (beforeEvictionSize - afterEvictionSize); if (remainingSizeInBytes <= 0L) { return true; } } return false; }