static <K, V> Set<Entry<K, V>> transformEntries(final Set<Entry<K, V>> entries) { return new ForwardingSet<Map.Entry<K, V>>() { @Override protected Set<Entry<K, V>> delegate() { return entries; } @Override public Iterator<Entry<K, V>> iterator() { return UnmodifiableEntry.transformEntries(super.iterator()); } @Override public Object[] toArray() { return standardToArray(); } @Override public <T> T[] toArray(T[] array) { return standardToArray(array); } }; }
private static <E> Set<E> removeOnlySet(final Set<E> set) { return new ForwardingSet<E>() { @Override protected Set<E> delegate() { return set; } @Override public boolean add(E element) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection<? extends E> es) { throw new UnsupportedOperationException(); } }; }
public static <T> Set<T> newThreadLocalSet() { return new ForwardingSet<T>() { private final ThreadLocal<Set<T>> threadSet = new ThreadLocal<Set<T>>() { @Override protected Set<T> initialValue() { return Sets.newHashSet(); } }; @Override protected Set<T> delegate() { return threadSet.get(); } }; }