Example usage for com.google.common.collect ForwardingSet ForwardingSet

List of usage examples for com.google.common.collect ForwardingSet ForwardingSet

Introduction

In this page you can find the example usage for com.google.common.collect ForwardingSet ForwardingSet.

Prototype

protected ForwardingSet() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:org.jclouds.aws.ec2.compute.suppliers.AWSEC2ImageSupplier.java

@SuppressWarnings("unchecked")
@Override/*w  w w .  ja  v a  2 s . c o m*/
public Set<? extends Image> get() {
    String amiQuery = queries.get(PROPERTY_EC2_AMI_QUERY);
    String ccAmiQuery = queries.get(PROPERTY_EC2_CC_AMI_QUERY);

    ListenableFuture<Iterable<Image>> normalImages = images(regions.get(), amiQuery, PROPERTY_EC2_AMI_QUERY);
    ImmutableSet<Image> clusterImages;
    try {
        clusterImages = ImmutableSet
                .copyOf(images(clusterRegions, ccAmiQuery, PROPERTY_EC2_CC_AMI_QUERY).get());
    } catch (Exception e) {
        logger.warn(e, "Error parsing images in query %s", ccAmiQuery);
        throw Throwables.propagate(e);
    }
    Iterables.addAll(clusterComputeIds, transform(clusterImages, new Function<Image, String>() {

        @Override
        public String apply(Image arg0) {
            return arg0.getId();
        }

    }));
    Iterable<? extends Image> parsedImages;
    try {
        parsedImages = ImmutableSet.copyOf(concat(clusterImages, normalImages.get()));
    } catch (Exception e) {
        logger.warn(e, "Error parsing images in query %s", amiQuery);
        throw Throwables.propagate(e);
    }

    final Map<RegionAndName, ? extends Image> imageMap = ImagesToRegionAndIdMap.imagesToMap(parsedImages);
    cache.get().invalidateAll();
    cache.get().asMap().putAll(Map.class.cast(imageMap));
    logger.debug("<< images(%d)", imageMap.size());

    // TODO Used to be mutable; was this assumed anywhere?
    return new ForwardingSet<Image>() {
        protected Set<Image> delegate() {
            return ImmutableSet.copyOf(cache.get().asMap().values());
        }
    };
}

From source file:org.apache.calcite.util.CompatibleGuava11.java

private static <E> Set<E> removeOnlySet(final Set<E> set) {
    return new ForwardingSet<E>() {
        @Override/* ww  w .  j  av  a 2s. com*/
        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();
        }
    };
}

From source file:com.github.benmanes.caffeine.guava.CaffeinatedGuavaCache.java

@Override
public ConcurrentMap<K, V> asMap() {
    return new ForwardingConcurrentMap<K, V>() {
        @Override/*from   w ww  .  j av  a 2s .c  om*/
        public boolean containsKey(Object key) {
            return (key != null) && delegate().containsKey(key);
        }

        @Override
        public boolean containsValue(Object value) {
            return (value != null) && delegate().containsValue(value);
        }

        @Override
        public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
            delegate().replaceAll(function);
        }

        @Override
        public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
            return delegate().computeIfAbsent(key, mappingFunction);
        }

        @Override
        public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
            return delegate().computeIfPresent(key, remappingFunction);
        }

        @Override
        public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
            return delegate().compute(key, remappingFunction);
        }

        @Override
        public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
            return delegate().merge(key, value, remappingFunction);
        }

        @Override
        public Set<K> keySet() {
            return new ForwardingSet<K>() {
                @Override
                public boolean removeIf(Predicate<? super K> filter) {
                    return delegate().removeIf(filter);
                }

                @Override
                public boolean remove(Object o) {
                    return (o != null) && delegate().remove(o);
                }

                @Override
                protected Set<K> delegate() {
                    return cache.asMap().keySet();
                }
            };
        }

        @Override
        public Collection<V> values() {
            return new ForwardingCollection<V>() {
                @Override
                public boolean removeIf(Predicate<? super V> filter) {
                    return delegate().removeIf(filter);
                }

                @Override
                public boolean remove(Object o) {
                    return (o != null) && delegate().remove(o);
                }

                @Override
                protected Collection<V> delegate() {
                    return cache.asMap().values();
                }
            };
        }

        @Override
        public Set<Entry<K, V>> entrySet() {
            return new ForwardingSet<Entry<K, V>>() {
                @Override
                public boolean add(Entry<K, V> entry) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean addAll(Collection<? extends Entry<K, V>> entry) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean removeIf(Predicate<? super Entry<K, V>> filter) {
                    return delegate().removeIf(filter);
                }

                @Override
                public Iterator<Entry<K, V>> iterator() {
                    Iterator<Entry<K, V>> iterator = delegate().iterator();
                    return new ForwardingIterator<Entry<K, V>>() {
                        @Override
                        public Entry<K, V> next() {
                            Entry<K, V> entry = delegate().next();
                            return new ForwardingMapEntry<K, V>() {
                                @Override
                                public V setValue(V value) {
                                    throw new UnsupportedOperationException();
                                }

                                @Override
                                protected Entry<K, V> delegate() {
                                    return entry;
                                }
                            };
                        }

                        @Override
                        protected Iterator<Entry<K, V>> delegate() {
                            return iterator;
                        }
                    };
                }

                @Override
                protected Set<Entry<K, V>> delegate() {
                    return cache.asMap().entrySet();
                }
            };
        }

        @Override
        protected ConcurrentMap<K, V> delegate() {
            return cache.asMap();
        }
    };
}

From source file:com.koloboke.compile.KolobokeMapBackedMultiset.java

@Override
public final Set<E> elementSet() {
    final Set<E> mapKeySet = mapKeySet();
    return new ForwardingSet<E>() {
        @Override//  w  ww  .  j a  v  a2 s  . c o  m
        protected Set<E> delegate() {
            return mapKeySet;
        }

        @Override
        public Iterator<E> iterator() {
            final Iterator<Map.Entry<E, Integer>> mapEntrySetIterator = mapEntrySet().iterator();
            return new Iterator<E>() {
                Map.Entry<E, Integer> toRemove;

                @Override
                public boolean hasNext() {
                    return mapEntrySetIterator.hasNext();
                }

                @Override
                public E next() {
                    toRemove = mapEntrySetIterator.next();
                    return toRemove.getKey();
                }

                @Override
                public void remove() {
                    checkRemove(toRemove != null);
                    size -= toRemove.getValue();
                    mapEntrySetIterator.remove();
                    toRemove = null;
                }
            };
        }

        @SuppressFBWarnings("IA_AMBIGUOUS_INVOCATION_OF_INHERITED_OR_OUTER_METHOD")
        @Override
        public boolean removeAll(Collection<?> c) {
            int mc = modCount();
            Collection<?> collection = checkNotNull(c);
            checkNotNull(collection); // for GWT
            if (collection instanceof Multiset) {
                collection = ((Multiset<?>) collection).elementSet();
            }
            boolean modified = false;
            /*
             * AbstractSet.removeAll(List) has quadratic behavior if the list size
             * is just less than the set's size.  We augment the test by
             * assuming that sets have fast contains() performance, and other
             * collections don't.  See
             * http://code.google.com/p/guava-libraries/issues/detail?id=1013
             */
            if (collection instanceof Set && collection.size() > size()) {
                Iterator<E> removeFrom = iterator();
                Predicate<? super E> predicate = in((Collection<? extends E>) collection);
                checkNotNull(predicate);
                while (removeFrom.hasNext()) {
                    if (predicate.apply(removeFrom.next())) {
                        removeFrom.remove();
                        mc++;
                        modified = true;
                    }
                }
            } else {
                for (Object e : collection) {
                    if (remove(e)) {
                        mc++;
                        modified = true;
                    }
                }
            }
            if (mc != modCount())
                throw new ConcurrentModificationException();
            return modified;
        }

        @Override
        public boolean retainAll(Collection<?> collection) {
            int mc = modCount();
            Iterator<E> removeFrom = this.iterator();
            Predicate<? super E> predicate = not(in(collection));
            checkNotNull(predicate);
            boolean modified = false;
            while (removeFrom.hasNext()) {
                if (predicate.apply(removeFrom.next())) {
                    removeFrom.remove();
                    mc++;
                    modified = true;
                }
            }
            if (mc != modCount())
                throw new ConcurrentModificationException();
            return modified;
        }

        @Override
        public void clear() {
            KolobokeMapBackedMultiset.this.clear();
        }
    };
}