com.eg.agent.android.harvest.HarvestableCache Maven / Gradle / Ivy
package com.eg.agent.android.harvest;
import com.eg.agent.android.harvest.type.Harvestable;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CopyOnWriteArrayList;
public class HarvestableCache {
private static final int DEFAULT_CACHE_LIMIT = 1024;
private final Collection cache = getNewCache();
private int limit = 1024;
protected Collection getNewCache() {
return new CopyOnWriteArrayList();
}
public void add(Harvestable harvestable) {
if (harvestable != null && this.cache.size() < this.limit) {
this.cache.add(harvestable);
}
}
public boolean get(Object h) {
return this.cache.contains(h);
}
public Collection flush() {
if (this.cache.size() == 0) {
return Collections.emptyList();
}
Collection oldCache;
synchronized (this) {
oldCache = getNewCache();
oldCache.addAll(this.cache);
this.cache.clear();
}
return oldCache;
}
public int getSize() {
return this.cache.size();
}
public void setLimit(int limit) {
this.limit = limit;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy