MCPcopy Index your code
hub / github.com/wyouflf/xUtils3 / trimToSize

Method trimToSize

xutils/src/main/java/org/xutils/cache/LruCache.java:164–188  ·  view source on GitHub ↗

Remove the eldest entries until the total of remaining entries is at or below the requested size. @param maxSize the maximum size of the cache before returning. May be -1 to evict even 0-sized elements.

(int maxSize)

Source from the content-addressed store, hash-verified

162 * to evict even 0-sized elements.
163 */
164 public void trimToSize(int maxSize) {
165 while (true) {
166 K key;
167 V value;
168 synchronized (this) {
169 if (size < 0 || (map.isEmpty() && size != 0)) {
170 throw new IllegalStateException(getClass().getName()
171 + ".sizeOf() is reporting inconsistent results!");
172 }
173
174 if (size <= maxSize || map.isEmpty()) {
175 break;
176 }
177
178 Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
179 key = toEvict.getKey();
180 value = toEvict.getValue();
181 map.remove(key);
182 size -= safeSizeOf(key, value);
183 evictionCount++;
184 }
185
186 entryRemoved(true, key, value, null);
187 }
188 }
189
190 /**
191 * Removes the entry for {@code key} if it exists.

Callers 4

resizeMethod · 0.95
getMethod · 0.95
putMethod · 0.95
evictAllMethod · 0.95

Calls 6

safeSizeOfMethod · 0.95
entryRemovedMethod · 0.95
isEmptyMethod · 0.80
getKeyMethod · 0.80
getNameMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected