Skip to content

Instantly share code, notes, and snippets.

@evacchi
Last active August 29, 2015 14:15
Show Gist options
  • Save evacchi/36e6059cc59a26a8c228 to your computer and use it in GitHub Desktop.
Save evacchi/36e6059cc59a26a8c228 to your computer and use it in GitHub Desktop.
A proxy to Vaadin's Containers that caches items in a Map<Object,SoftReference<Item>>
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.filter.UnsupportedFilterException;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.WeakHashMap;
/**
* Created by evacchi on 19/02/15.
*/
public class CachingContainerProxy<T extends Container.Indexed
& Container.Filterable
& Container.Sortable
& Container.ItemSetChangeNotifier>
implements Container.Indexed,
Container.Filterable,
Container.Sortable,
Container.ItemSetChangeNotifier {
private final T delegate;
private final WeakHashMap<Object, SoftReference<Item>> itemCache = new WeakHashMap<>();
public CachingContainerProxy(T delegate) {
this.delegate = delegate;
}
public T getDelegate() {
return delegate;
}
@Override
public Object addItemAt(int i) throws UnsupportedOperationException {
return delegate.addItemAt(i);
}
@Override
public Item addItemAt(int i, Object o) throws UnsupportedOperationException {
return delegate.addItemAt(i, o);
}
@Override
public Object getIdByIndex(int i) {
return delegate.getIdByIndex(i);
}
@Override
public List<?> getItemIds(int i, int i1) {
return delegate.getItemIds(i, i1);
}
@Override
public int indexOfId(Object o) {
return delegate.indexOfId(o);
}
@Override
public Object addItemAfter(Object o) throws UnsupportedOperationException {
return delegate.addItemAfter(o);
}
@Override
public Item addItemAfter(Object o, Object o1) throws UnsupportedOperationException {
return delegate.addItemAfter(o, o1);
}
@Override
public Object firstItemId() {
return delegate.firstItemId();
}
@Override
public boolean isFirstId(Object o) {
return delegate.isFirstId(o);
}
@Override
public boolean isLastId(Object o) {
return delegate.isLastId(o);
}
@Override
public Object lastItemId() {
return delegate.lastItemId();
}
@Override
public Object nextItemId(Object o) {
return delegate.nextItemId(o);
}
@Override
public Object prevItemId(Object o) {
return delegate.prevItemId(o);
}
@Override
public boolean addContainerProperty(Object o, Class<?> aClass, Object o1) throws
UnsupportedOperationException {
return delegate.addContainerProperty(o, aClass, o1);
}
@Override
public Object addItem() throws UnsupportedOperationException {
return delegate.addItem();
}
@Override
public Item addItem(Object o) throws UnsupportedOperationException {
return delegate.addItem(o);
}
@Override
public boolean containsId(Object o) {
return delegate.containsId(o);
}
@Override
public Property getContainerProperty(Object o, Object o1) {
return delegate.getContainerProperty(o, o1);
}
@Override
public Collection<?> getContainerPropertyIds() {
return delegate.getContainerPropertyIds();
}
@Override
public Item getItem(Object o) {
if (!this.containsId(o)) return null;
SoftReference<Item> itemRef = itemCache.get(o);
if (itemRef == null || itemRef.get() == null) {
itemRef = new SoftReference<>(delegate.getItem(o));
itemCache.put(o, itemRef);
}
return itemRef.get();
}
@Override
public Collection<?> getItemIds() {
return delegate.getItemIds();
}
@Override
public Class<?> getType(Object o) {
return delegate.getType(o);
}
@Override
public boolean removeAllItems() throws UnsupportedOperationException {
return delegate.removeAllItems();
}
@Override
public boolean removeContainerProperty(Object o) throws UnsupportedOperationException {
return delegate.removeContainerProperty(o);
}
@Override
public boolean removeItem(Object o) throws UnsupportedOperationException {
itemCache.remove(o);
return delegate.removeItem(o);
}
@Override
public int size() {
return delegate.size();
}
@Override
public void addContainerFilter(Filter filter) throws UnsupportedFilterException {
delegate.addContainerFilter(filter);
}
@Override
public Collection<Filter> getContainerFilters() {
return delegate.getContainerFilters();
}
@Override
public void removeAllContainerFilters() {
delegate.removeAllContainerFilters();
}
@Override
public void removeContainerFilter(Filter filter) {
delegate.removeContainerFilter(filter);
}
@Override
public Collection<?> getSortableContainerPropertyIds() {
return delegate.getSortableContainerPropertyIds();
}
@Override
public void sort(Object[] objects, boolean[] booleans) {
delegate.sort(objects, booleans);
}
@Override
public void addItemSetChangeListener(ItemSetChangeListener itemSetChangeListener) {
delegate.addItemSetChangeListener(itemSetChangeListener);
}
@Override
@Deprecated
public void addListener(ItemSetChangeListener itemSetChangeListener) {
delegate.addListener(itemSetChangeListener);
}
@Override
public void removeItemSetChangeListener(ItemSetChangeListener itemSetChangeListener) {
delegate.removeItemSetChangeListener(itemSetChangeListener);
}
@Override
@Deprecated
public void removeListener(ItemSetChangeListener itemSetChangeListener) {
delegate.removeListener(itemSetChangeListener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment