com.box.sdk.LRUCache Maven / Gradle / Ivy
package com.box.sdk;
import java.util.Iterator;
import java.util.LinkedHashSet;
class LRUCache {
static final int MAX_SIZE = 512;
private final LinkedHashSet linkedHashSet;
public LRUCache() {
this.linkedHashSet = new LinkedHashSet(MAX_SIZE);
}
boolean add(E item) {
boolean newItem = !this.linkedHashSet.remove(item);
this.linkedHashSet.add(item);
if (this.linkedHashSet.size() >= MAX_SIZE) {
Iterator it = this.linkedHashSet.iterator();
it.next();
it.remove();
}
return newItem;
}
}