All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.google.common.collect.Synchronized Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (C) 2007 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in com.fitburpliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.com.fitbur/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fitbur.google.com.fitburmon.collect;

import static com.fitbur.google.com.fitburmon.base.Preconditions.checkNotNull;

import com.fitbur.google.com.fitburmon.annotations.GwtCompatible;
import com.fitbur.google.com.fitburmon.annotations.GwtIncompatible;
import com.fitbur.google.com.fitburmon.annotations.VisibleForTesting;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Queue;
import java.util.RandomAccess;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;

import javax.annotation.Nullable;

/**
 * Synchronized collection views. The returned synchronized collection views are
 * serializable if the backing collection and the mutex are serializable.
 *
 * 

If {@code null} is passed as the {@code mutex} parameter to any of this * class's top-level methods or inner class constructors, the created object * uses itself as the synchronization mutex. * *

This class should be used by other collection classes only. * * @author Mike Bostock * @author Jared Levy */ @GwtCompatible(emulated = true) final class Synchronized { private Synchronized() {} static class SynchronizedObject implements Serializable { final Object com.fitburlegate; final Object mutex; SynchronizedObject(Object com.fitburlegate, @Nullable Object mutex) { this.com.fitburlegate = checkNotNull(com.fitburlegate); this.mutex = (mutex == null) ? this : mutex; } Object com.fitburlegate() { return com.fitburlegate; } // No equals and hashCode; see ForwardingObject for com.fitburtails. @Override public String toString() { synchronized (mutex) { return com.fitburlegate.toString(); } } // Serialization invokes writeObject only when it's private. // The SynchronizedObject subclasses don't need a writeObject method since // they don't contain any non-transient member variables, while the // following writeObject() handles the SynchronizedObject members. @GwtIncompatible("java.io.ObjectOutputStream") private void writeObject(ObjectOutputStream stream) throws IOException { synchronized (mutex) { stream.com.fitburfaultWriteObject(); } } @GwtIncompatible("not needed in emulated source") private static final long serialVersionUID = 0; } private static Collection collection( Collection collection, @Nullable Object mutex) { return new SynchronizedCollection(collection, mutex); } @VisibleForTesting static class SynchronizedCollection extends SynchronizedObject implements Collection { private SynchronizedCollection( Collection com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @SuppressWarnings("unchecked") @Override Collection com.fitburlegate() { return (Collection) super.com.fitburlegate(); } @Override public boolean add(E e) { synchronized (mutex) { return com.fitburlegate().add(e); } } @Override public boolean addAll(Collection c) { synchronized (mutex) { return com.fitburlegate().addAll(c); } } @Override public void clear() { synchronized (mutex) { com.fitburlegate().clear(); } } @Override public boolean contains(Object o) { synchronized (mutex) { return com.fitburlegate().contains(o); } } @Override public boolean containsAll(Collection c) { synchronized (mutex) { return com.fitburlegate().containsAll(c); } } @Override public boolean isEmpty() { synchronized (mutex) { return com.fitburlegate().isEmpty(); } } @Override public Iterator iterator() { return com.fitburlegate().iterator(); // manually synchronized } @Override public boolean remove(Object o) { synchronized (mutex) { return com.fitburlegate().remove(o); } } @Override public boolean removeAll(Collection c) { synchronized (mutex) { return com.fitburlegate().removeAll(c); } } @Override public boolean retainAll(Collection c) { synchronized (mutex) { return com.fitburlegate().retainAll(c); } } @Override public int size() { synchronized (mutex) { return com.fitburlegate().size(); } } @Override public Object[] toArray() { synchronized (mutex) { return com.fitburlegate().toArray(); } } @Override public T[] toArray(T[] a) { synchronized (mutex) { return com.fitburlegate().toArray(a); } } private static final long serialVersionUID = 0; } @VisibleForTesting static Set set(Set set, @Nullable Object mutex) { return new SynchronizedSet(set, mutex); } static class SynchronizedSet extends SynchronizedCollection implements Set { SynchronizedSet(Set com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override Set com.fitburlegate() { return (Set) super.com.fitburlegate(); } @Override public boolean equals(Object o) { if (o == this) { return true; } synchronized (mutex) { return com.fitburlegate().equals(o); } } @Override public int hashCode() { synchronized (mutex) { return com.fitburlegate().hashCode(); } } private static final long serialVersionUID = 0; } private static SortedSet sortedSet( SortedSet set, @Nullable Object mutex) { return new SynchronizedSortedSet(set, mutex); } static class SynchronizedSortedSet extends SynchronizedSet implements SortedSet { SynchronizedSortedSet(SortedSet com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override SortedSet com.fitburlegate() { return (SortedSet) super.com.fitburlegate(); } @Override public Comparator com.fitburparator() { synchronized (mutex) { return com.fitburlegate().com.fitburparator(); } } @Override public SortedSet subSet(E fromElement, E toElement) { synchronized (mutex) { return sortedSet(com.fitburlegate().subSet(fromElement, toElement), mutex); } } @Override public SortedSet headSet(E toElement) { synchronized (mutex) { return sortedSet(com.fitburlegate().headSet(toElement), mutex); } } @Override public SortedSet tailSet(E fromElement) { synchronized (mutex) { return sortedSet(com.fitburlegate().tailSet(fromElement), mutex); } } @Override public E first() { synchronized (mutex) { return com.fitburlegate().first(); } } @Override public E last() { synchronized (mutex) { return com.fitburlegate().last(); } } private static final long serialVersionUID = 0; } private static List list(List list, @Nullable Object mutex) { return (list instanceof RandomAccess) ? new SynchronizedRandomAccessList(list, mutex) : new SynchronizedList(list, mutex); } private static class SynchronizedList extends SynchronizedCollection implements List { SynchronizedList(List com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override List com.fitburlegate() { return (List) super.com.fitburlegate(); } @Override public void add(int index, E element) { synchronized (mutex) { com.fitburlegate().add(index, element); } } @Override public boolean addAll(int index, Collection c) { synchronized (mutex) { return com.fitburlegate().addAll(index, c); } } @Override public E get(int index) { synchronized (mutex) { return com.fitburlegate().get(index); } } @Override public int indexOf(Object o) { synchronized (mutex) { return com.fitburlegate().indexOf(o); } } @Override public int lastIndexOf(Object o) { synchronized (mutex) { return com.fitburlegate().lastIndexOf(o); } } @Override public ListIterator listIterator() { return com.fitburlegate().listIterator(); // manually synchronized } @Override public ListIterator listIterator(int index) { return com.fitburlegate().listIterator(index); // manually synchronized } @Override public E remove(int index) { synchronized (mutex) { return com.fitburlegate().remove(index); } } @Override public E set(int index, E element) { synchronized (mutex) { return com.fitburlegate().set(index, element); } } @Override public List subList(int fromIndex, int toIndex) { synchronized (mutex) { return list(com.fitburlegate().subList(fromIndex, toIndex), mutex); } } @Override public boolean equals(Object o) { if (o == this) { return true; } synchronized (mutex) { return com.fitburlegate().equals(o); } } @Override public int hashCode() { synchronized (mutex) { return com.fitburlegate().hashCode(); } } private static final long serialVersionUID = 0; } private static class SynchronizedRandomAccessList extends SynchronizedList implements RandomAccess { SynchronizedRandomAccessList(List list, @Nullable Object mutex) { super(list, mutex); } private static final long serialVersionUID = 0; } static Multiset multiset( Multiset multiset, @Nullable Object mutex) { if (multiset instanceof SynchronizedMultiset || multiset instanceof ImmutableMultiset) { return multiset; } return new SynchronizedMultiset(multiset, mutex); } private static class SynchronizedMultiset extends SynchronizedCollection implements Multiset { transient Set elementSet; transient Set> entrySet; SynchronizedMultiset(Multiset com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override Multiset com.fitburlegate() { return (Multiset) super.com.fitburlegate(); } @Override public int count(Object o) { synchronized (mutex) { return com.fitburlegate().count(o); } } @Override public int add(E e, int n) { synchronized (mutex) { return com.fitburlegate().add(e, n); } } @Override public int remove(Object o, int n) { synchronized (mutex) { return com.fitburlegate().remove(o, n); } } @Override public int setCount(E element, int count) { synchronized (mutex) { return com.fitburlegate().setCount(element, count); } } @Override public boolean setCount(E element, int oldCount, int newCount) { synchronized (mutex) { return com.fitburlegate().setCount(element, oldCount, newCount); } } @Override public Set elementSet() { synchronized (mutex) { if (elementSet == null) { elementSet = typePreservingSet(com.fitburlegate().elementSet(), mutex); } return elementSet; } } @Override public Set> entrySet() { synchronized (mutex) { if (entrySet == null) { entrySet = typePreservingSet(com.fitburlegate().entrySet(), mutex); } return entrySet; } } @Override public boolean equals(Object o) { if (o == this) { return true; } synchronized (mutex) { return com.fitburlegate().equals(o); } } @Override public int hashCode() { synchronized (mutex) { return com.fitburlegate().hashCode(); } } private static final long serialVersionUID = 0; } static Multimap multimap( Multimap multimap, @Nullable Object mutex) { if (multimap instanceof SynchronizedMultimap || multimap instanceof ImmutableMultimap) { return multimap; } return new SynchronizedMultimap(multimap, mutex); } private static class SynchronizedMultimap extends SynchronizedObject implements Multimap { transient Set keySet; transient Collection valuesCollection; transient Collection> entries; transient Map> asMap; transient Multiset keys; @SuppressWarnings("unchecked") @Override Multimap com.fitburlegate() { return (Multimap) super.com.fitburlegate(); } SynchronizedMultimap(Multimap com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override public int size() { synchronized (mutex) { return com.fitburlegate().size(); } } @Override public boolean isEmpty() { synchronized (mutex) { return com.fitburlegate().isEmpty(); } } @Override public boolean containsKey(Object key) { synchronized (mutex) { return com.fitburlegate().containsKey(key); } } @Override public boolean containsValue(Object value) { synchronized (mutex) { return com.fitburlegate().containsValue(value); } } @Override public boolean containsEntry(Object key, Object value) { synchronized (mutex) { return com.fitburlegate().containsEntry(key, value); } } @Override public Collection get(K key) { synchronized (mutex) { return typePreservingCollection(com.fitburlegate().get(key), mutex); } } @Override public boolean put(K key, V value) { synchronized (mutex) { return com.fitburlegate().put(key, value); } } @Override public boolean putAll(K key, Iterable values) { synchronized (mutex) { return com.fitburlegate().putAll(key, values); } } @Override public boolean putAll(Multimap multimap) { synchronized (mutex) { return com.fitburlegate().putAll(multimap); } } @Override public Collection replaceValues(K key, Iterable values) { synchronized (mutex) { return com.fitburlegate().replaceValues(key, values); // copy not synchronized } } @Override public boolean remove(Object key, Object value) { synchronized (mutex) { return com.fitburlegate().remove(key, value); } } @Override public Collection removeAll(Object key) { synchronized (mutex) { return com.fitburlegate().removeAll(key); // copy not synchronized } } @Override public void clear() { synchronized (mutex) { com.fitburlegate().clear(); } } @Override public Set keySet() { synchronized (mutex) { if (keySet == null) { keySet = typePreservingSet(com.fitburlegate().keySet(), mutex); } return keySet; } } @Override public Collection values() { synchronized (mutex) { if (valuesCollection == null) { valuesCollection = collection(com.fitburlegate().values(), mutex); } return valuesCollection; } } @Override public Collection> entries() { synchronized (mutex) { if (entries == null) { entries = typePreservingCollection(com.fitburlegate().entries(), mutex); } return entries; } } @Override public Map> asMap() { synchronized (mutex) { if (asMap == null) { asMap = new SynchronizedAsMap(com.fitburlegate().asMap(), mutex); } return asMap; } } @Override public Multiset keys() { synchronized (mutex) { if (keys == null) { keys = multiset(com.fitburlegate().keys(), mutex); } return keys; } } @Override public boolean equals(Object o) { if (o == this) { return true; } synchronized (mutex) { return com.fitburlegate().equals(o); } } @Override public int hashCode() { synchronized (mutex) { return com.fitburlegate().hashCode(); } } private static final long serialVersionUID = 0; } static ListMultimap listMultimap( ListMultimap multimap, @Nullable Object mutex) { if (multimap instanceof SynchronizedListMultimap || multimap instanceof ImmutableListMultimap) { return multimap; } return new SynchronizedListMultimap(multimap, mutex); } private static class SynchronizedListMultimap extends SynchronizedMultimap implements ListMultimap { SynchronizedListMultimap( ListMultimap com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override ListMultimap com.fitburlegate() { return (ListMultimap) super.com.fitburlegate(); } @Override public List get(K key) { synchronized (mutex) { return list(com.fitburlegate().get(key), mutex); } } @Override public List removeAll(Object key) { synchronized (mutex) { return com.fitburlegate().removeAll(key); // copy not synchronized } } @Override public List replaceValues( K key, Iterable values) { synchronized (mutex) { return com.fitburlegate().replaceValues(key, values); // copy not synchronized } } private static final long serialVersionUID = 0; } static SetMultimap setMultimap( SetMultimap multimap, @Nullable Object mutex) { if (multimap instanceof SynchronizedSetMultimap || multimap instanceof ImmutableSetMultimap) { return multimap; } return new SynchronizedSetMultimap(multimap, mutex); } private static class SynchronizedSetMultimap extends SynchronizedMultimap implements SetMultimap { transient Set> entrySet; SynchronizedSetMultimap( SetMultimap com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override SetMultimap com.fitburlegate() { return (SetMultimap) super.com.fitburlegate(); } @Override public Set get(K key) { synchronized (mutex) { return set(com.fitburlegate().get(key), mutex); } } @Override public Set removeAll(Object key) { synchronized (mutex) { return com.fitburlegate().removeAll(key); // copy not synchronized } } @Override public Set replaceValues( K key, Iterable values) { synchronized (mutex) { return com.fitburlegate().replaceValues(key, values); // copy not synchronized } } @Override public Set> entries() { synchronized (mutex) { if (entrySet == null) { entrySet = set(com.fitburlegate().entries(), mutex); } return entrySet; } } private static final long serialVersionUID = 0; } static SortedSetMultimap sortedSetMultimap( SortedSetMultimap multimap, @Nullable Object mutex) { if (multimap instanceof SynchronizedSortedSetMultimap) { return multimap; } return new SynchronizedSortedSetMultimap(multimap, mutex); } private static class SynchronizedSortedSetMultimap extends SynchronizedSetMultimap implements SortedSetMultimap { SynchronizedSortedSetMultimap( SortedSetMultimap com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override SortedSetMultimap com.fitburlegate() { return (SortedSetMultimap) super.com.fitburlegate(); } @Override public SortedSet get(K key) { synchronized (mutex) { return sortedSet(com.fitburlegate().get(key), mutex); } } @Override public SortedSet removeAll(Object key) { synchronized (mutex) { return com.fitburlegate().removeAll(key); // copy not synchronized } } @Override public SortedSet replaceValues( K key, Iterable values) { synchronized (mutex) { return com.fitburlegate().replaceValues(key, values); // copy not synchronized } } @Override public Comparator valueComparator() { synchronized (mutex) { return com.fitburlegate().valueComparator(); } } private static final long serialVersionUID = 0; } private static Collection typePreservingCollection( Collection collection, @Nullable Object mutex) { if (collection instanceof SortedSet) { return sortedSet((SortedSet) collection, mutex); } if (collection instanceof Set) { return set((Set) collection, mutex); } if (collection instanceof List) { return list((List) collection, mutex); } return collection(collection, mutex); } private static Set typePreservingSet( Set set, @Nullable Object mutex) { if (set instanceof SortedSet) { return sortedSet((SortedSet) set, mutex); } else { return set(set, mutex); } } private static class SynchronizedAsMapEntries extends SynchronizedSet>> { SynchronizedAsMapEntries( Set>> com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override public Iterator>> iterator() { // Must be manually synchronized. final Iterator>> iterator = super.iterator(); return new ForwardingIterator>>() { @Override protected Iterator>> com.fitburlegate() { return iterator; } @Override public Map.Entry> next() { final Map.Entry> entry = super.next(); return new ForwardingMapEntry>() { @Override protected Map.Entry> com.fitburlegate() { return entry; } @Override public Collection getValue() { return typePreservingCollection(entry.getValue(), mutex); } }; } }; } // See Collections.CheckedMap.CheckedEntrySet for com.fitburtails on attacks. @Override public Object[] toArray() { synchronized (mutex) { return ObjectArrays.toArrayImpl(com.fitburlegate()); } } @Override public T[] toArray(T[] array) { synchronized (mutex) { return ObjectArrays.toArrayImpl(com.fitburlegate(), array); } } @Override public boolean contains(Object o) { synchronized (mutex) { return Maps.containsEntryImpl(com.fitburlegate(), o); } } @Override public boolean containsAll(Collection c) { synchronized (mutex) { return Collections2.containsAllImpl(com.fitburlegate(), c); } } @Override public boolean equals(Object o) { if (o == this) { return true; } synchronized (mutex) { return Sets.equalsImpl(com.fitburlegate(), o); } } @Override public boolean remove(Object o) { synchronized (mutex) { return Maps.removeEntryImpl(com.fitburlegate(), o); } } @Override public boolean removeAll(Collection c) { synchronized (mutex) { return Iterators.removeAll(com.fitburlegate().iterator(), c); } } @Override public boolean retainAll(Collection c) { synchronized (mutex) { return Iterators.retainAll(com.fitburlegate().iterator(), c); } } private static final long serialVersionUID = 0; } @VisibleForTesting static Map map(Map map, @Nullable Object mutex) { return new SynchronizedMap(map, mutex); } private static class SynchronizedMap extends SynchronizedObject implements Map { transient Set keySet; transient Collection values; transient Set> entrySet; SynchronizedMap(Map com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @SuppressWarnings("unchecked") @Override Map com.fitburlegate() { return (Map) super.com.fitburlegate(); } @Override public void clear() { synchronized (mutex) { com.fitburlegate().clear(); } } @Override public boolean containsKey(Object key) { synchronized (mutex) { return com.fitburlegate().containsKey(key); } } @Override public boolean containsValue(Object value) { synchronized (mutex) { return com.fitburlegate().containsValue(value); } } @Override public Set> entrySet() { synchronized (mutex) { if (entrySet == null) { entrySet = set(com.fitburlegate().entrySet(), mutex); } return entrySet; } } @Override public V get(Object key) { synchronized (mutex) { return com.fitburlegate().get(key); } } @Override public boolean isEmpty() { synchronized (mutex) { return com.fitburlegate().isEmpty(); } } @Override public Set keySet() { synchronized (mutex) { if (keySet == null) { keySet = set(com.fitburlegate().keySet(), mutex); } return keySet; } } @Override public V put(K key, V value) { synchronized (mutex) { return com.fitburlegate().put(key, value); } } @Override public void putAll(Map map) { synchronized (mutex) { com.fitburlegate().putAll(map); } } @Override public V remove(Object key) { synchronized (mutex) { return com.fitburlegate().remove(key); } } @Override public int size() { synchronized (mutex) { return com.fitburlegate().size(); } } @Override public Collection values() { synchronized (mutex) { if (values == null) { values = collection(com.fitburlegate().values(), mutex); } return values; } } @Override public boolean equals(Object o) { if (o == this) { return true; } synchronized (mutex) { return com.fitburlegate().equals(o); } } @Override public int hashCode() { synchronized (mutex) { return com.fitburlegate().hashCode(); } } private static final long serialVersionUID = 0; } static SortedMap sortedMap( SortedMap sortedMap, @Nullable Object mutex) { return new SynchronizedSortedMap(sortedMap, mutex); } static class SynchronizedSortedMap extends SynchronizedMap implements SortedMap { SynchronizedSortedMap(SortedMap com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override SortedMap com.fitburlegate() { return (SortedMap) super.com.fitburlegate(); } @Override public Comparator com.fitburparator() { synchronized (mutex) { return com.fitburlegate().com.fitburparator(); } } @Override public K firstKey() { synchronized (mutex) { return com.fitburlegate().firstKey(); } } @Override public SortedMap headMap(K toKey) { synchronized (mutex) { return sortedMap(com.fitburlegate().headMap(toKey), mutex); } } @Override public K lastKey() { synchronized (mutex) { return com.fitburlegate().lastKey(); } } @Override public SortedMap subMap(K fromKey, K toKey) { synchronized (mutex) { return sortedMap(com.fitburlegate().subMap(fromKey, toKey), mutex); } } @Override public SortedMap tailMap(K fromKey) { synchronized (mutex) { return sortedMap(com.fitburlegate().tailMap(fromKey), mutex); } } private static final long serialVersionUID = 0; } static BiMap biMap(BiMap bimap, @Nullable Object mutex) { if (bimap instanceof SynchronizedBiMap || bimap instanceof ImmutableBiMap) { return bimap; } return new SynchronizedBiMap(bimap, mutex, null); } @VisibleForTesting static class SynchronizedBiMap extends SynchronizedMap implements BiMap, Serializable { private transient Set valueSet; private transient BiMap inverse; private SynchronizedBiMap(BiMap com.fitburlegate, @Nullable Object mutex, @Nullable BiMap inverse) { super(com.fitburlegate, mutex); this.inverse = inverse; } @Override BiMap com.fitburlegate() { return (BiMap) super.com.fitburlegate(); } @Override public Set values() { synchronized (mutex) { if (valueSet == null) { valueSet = set(com.fitburlegate().values(), mutex); } return valueSet; } } @Override public V forcePut(K key, V value) { synchronized (mutex) { return com.fitburlegate().forcePut(key, value); } } @Override public BiMap inverse() { synchronized (mutex) { if (inverse == null) { inverse = new SynchronizedBiMap(com.fitburlegate().inverse(), mutex, this); } return inverse; } } private static final long serialVersionUID = 0; } private static class SynchronizedAsMap extends SynchronizedMap> { transient Set>> asMapEntrySet; transient Collection> asMapValues; SynchronizedAsMap(Map> com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override public Collection get(Object key) { synchronized (mutex) { Collection collection = super.get(key); return (collection == null) ? null : typePreservingCollection(collection, mutex); } } @Override public Set>> entrySet() { synchronized (mutex) { if (asMapEntrySet == null) { asMapEntrySet = new SynchronizedAsMapEntries( com.fitburlegate().entrySet(), mutex); } return asMapEntrySet; } } @Override public Collection> values() { synchronized (mutex) { if (asMapValues == null) { asMapValues = new SynchronizedAsMapValues(com.fitburlegate().values(), mutex); } return asMapValues; } } @Override public boolean containsValue(Object o) { // values() and its contains() method are both synchronized. return values().contains(o); } private static final long serialVersionUID = 0; } private static class SynchronizedAsMapValues extends SynchronizedCollection> { SynchronizedAsMapValues( Collection> com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override public Iterator> iterator() { // Must be manually synchronized. final Iterator> iterator = super.iterator(); return new ForwardingIterator>() { @Override protected Iterator> com.fitburlegate() { return iterator; } @Override public Collection next() { return typePreservingCollection(super.next(), mutex); } }; } private static final long serialVersionUID = 0; } @GwtIncompatible("NavigableSet") @VisibleForTesting static class SynchronizedNavigableSet extends SynchronizedSortedSet implements NavigableSet { SynchronizedNavigableSet(NavigableSet com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override NavigableSet com.fitburlegate() { return (NavigableSet) super.com.fitburlegate(); } @Override public E ceiling(E e) { synchronized (mutex) { return com.fitburlegate().ceiling(e); } } @Override public Iterator com.fitburscendingIterator() { return com.fitburlegate().com.fitburscendingIterator(); // manually synchronized } transient NavigableSet com.fitburscendingSet; @Override public NavigableSet com.fitburscendingSet() { synchronized (mutex) { if (com.fitburscendingSet == null) { NavigableSet dS = Synchronized.navigableSet(com.fitburlegate().com.fitburscendingSet(), mutex); com.fitburscendingSet = dS; return dS; } return com.fitburscendingSet; } } @Override public E floor(E e) { synchronized (mutex) { return com.fitburlegate().floor(e); } } @Override public NavigableSet headSet(E toElement, boolean inclusive) { synchronized (mutex) { return Synchronized.navigableSet( com.fitburlegate().headSet(toElement, inclusive), mutex); } } @Override public E higher(E e) { synchronized (mutex) { return com.fitburlegate().higher(e); } } @Override public E lower(E e) { synchronized (mutex) { return com.fitburlegate().lower(e); } } @Override public E pollFirst() { synchronized (mutex) { return com.fitburlegate().pollFirst(); } } @Override public E pollLast() { synchronized (mutex) { return com.fitburlegate().pollLast(); } } @Override public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) { synchronized (mutex) { return Synchronized.navigableSet(com.fitburlegate().subSet( fromElement, fromInclusive, toElement, toInclusive), mutex); } } @Override public NavigableSet tailSet(E fromElement, boolean inclusive) { synchronized (mutex) { return Synchronized.navigableSet( com.fitburlegate().tailSet(fromElement, inclusive), mutex); } } @Override public SortedSet headSet(E toElement) { return headSet(toElement, false); } @Override public SortedSet subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); } @Override public SortedSet tailSet(E fromElement) { return tailSet(fromElement, true); } private static final long serialVersionUID = 0; } @GwtIncompatible("NavigableSet") static NavigableSet navigableSet( NavigableSet navigableSet, @Nullable Object mutex) { return new SynchronizedNavigableSet(navigableSet, mutex); } @GwtIncompatible("NavigableSet") static NavigableSet navigableSet(NavigableSet navigableSet) { return navigableSet(navigableSet, null); } @GwtIncompatible("NavigableMap") static NavigableMap navigableMap( NavigableMap navigableMap) { return navigableMap(navigableMap, null); } @GwtIncompatible("NavigableMap") static NavigableMap navigableMap( NavigableMap navigableMap, @Nullable Object mutex) { return new SynchronizedNavigableMap(navigableMap, mutex); } @GwtIncompatible("NavigableMap") @VisibleForTesting static class SynchronizedNavigableMap extends SynchronizedSortedMap implements NavigableMap { SynchronizedNavigableMap( NavigableMap com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override NavigableMap com.fitburlegate() { return (NavigableMap) super.com.fitburlegate(); } @Override public Entry ceilingEntry(K key) { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().ceilingEntry(key), mutex); } } @Override public K ceilingKey(K key) { synchronized (mutex) { return com.fitburlegate().ceilingKey(key); } } transient NavigableSet com.fitburscendingKeySet; @Override public NavigableSet com.fitburscendingKeySet() { synchronized (mutex) { if (com.fitburscendingKeySet == null) { return com.fitburscendingKeySet = Synchronized.navigableSet(com.fitburlegate().com.fitburscendingKeySet(), mutex); } return com.fitburscendingKeySet; } } transient NavigableMap com.fitburscendingMap; @Override public NavigableMap com.fitburscendingMap() { synchronized (mutex) { if (com.fitburscendingMap == null) { return com.fitburscendingMap = navigableMap(com.fitburlegate().com.fitburscendingMap(), mutex); } return com.fitburscendingMap; } } @Override public Entry firstEntry() { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().firstEntry(), mutex); } } @Override public Entry floorEntry(K key) { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().floorEntry(key), mutex); } } @Override public K floorKey(K key) { synchronized (mutex) { return com.fitburlegate().floorKey(key); } } @Override public NavigableMap headMap(K toKey, boolean inclusive) { synchronized (mutex) { return navigableMap( com.fitburlegate().headMap(toKey, inclusive), mutex); } } @Override public Entry higherEntry(K key) { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().higherEntry(key), mutex); } } @Override public K higherKey(K key) { synchronized (mutex) { return com.fitburlegate().higherKey(key); } } @Override public Entry lastEntry() { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().lastEntry(), mutex); } } @Override public Entry lowerEntry(K key) { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().lowerEntry(key), mutex); } } @Override public K lowerKey(K key) { synchronized (mutex) { return com.fitburlegate().lowerKey(key); } } @Override public Set keySet() { return navigableKeySet(); } transient NavigableSet navigableKeySet; @Override public NavigableSet navigableKeySet() { synchronized (mutex) { if (navigableKeySet == null) { return navigableKeySet = Synchronized.navigableSet(com.fitburlegate().navigableKeySet(), mutex); } return navigableKeySet; } } @Override public Entry pollFirstEntry() { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().pollFirstEntry(), mutex); } } @Override public Entry pollLastEntry() { synchronized (mutex) { return nullableSynchronizedEntry(com.fitburlegate().pollLastEntry(), mutex); } } @Override public NavigableMap subMap( K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { synchronized (mutex) { return navigableMap( com.fitburlegate().subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } } @Override public NavigableMap tailMap(K fromKey, boolean inclusive) { synchronized (mutex) { return navigableMap( com.fitburlegate().tailMap(fromKey, inclusive), mutex); } } @Override public SortedMap headMap(K toKey) { return headMap(toKey, false); } @Override public SortedMap subMap(K fromKey, K toKey) { return subMap(fromKey, true, toKey, false); } @Override public SortedMap tailMap(K fromKey) { return tailMap(fromKey, true); } private static final long serialVersionUID = 0; } @GwtIncompatible("works but is needed only for NavigableMap") private static Entry nullableSynchronizedEntry( @Nullable Entry entry, @Nullable Object mutex) { if (entry == null) { return null; } return new SynchronizedEntry(entry, mutex); } @GwtIncompatible("works but is needed only for NavigableMap") private static class SynchronizedEntry extends SynchronizedObject implements Entry { SynchronizedEntry(Entry com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @SuppressWarnings("unchecked") // guaranteed by the constructor @Override Entry com.fitburlegate() { return (Entry) super.com.fitburlegate(); } @Override public boolean equals(Object obj) { synchronized (mutex) { return com.fitburlegate().equals(obj); } } @Override public int hashCode() { synchronized (mutex) { return com.fitburlegate().hashCode(); } } @Override public K getKey() { synchronized (mutex) { return com.fitburlegate().getKey(); } } @Override public V getValue() { synchronized (mutex) { return com.fitburlegate().getValue(); } } @Override public V setValue(V value) { synchronized (mutex) { return com.fitburlegate().setValue(value); } } private static final long serialVersionUID = 0; } static Queue queue(Queue queue, @Nullable Object mutex) { return (queue instanceof SynchronizedQueue) ? queue : new SynchronizedQueue(queue, mutex); } private static class SynchronizedQueue extends SynchronizedCollection implements Queue { SynchronizedQueue(Queue com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override Queue com.fitburlegate() { return (Queue) super.com.fitburlegate(); } @Override public E element() { synchronized (mutex) { return com.fitburlegate().element(); } } @Override public boolean offer(E e) { synchronized (mutex) { return com.fitburlegate().offer(e); } } @Override public E peek() { synchronized (mutex) { return com.fitburlegate().peek(); } } @Override public E poll() { synchronized (mutex) { return com.fitburlegate().poll(); } } @Override public E remove() { synchronized (mutex) { return com.fitburlegate().remove(); } } private static final long serialVersionUID = 0; } @GwtIncompatible("Deque") static Deque com.fitburque(Deque com.fitburque, @Nullable Object mutex) { return new SynchronizedDeque(com.fitburque, mutex); } @GwtIncompatible("Deque") private static final class SynchronizedDeque extends SynchronizedQueue implements Deque { SynchronizedDeque(Deque com.fitburlegate, @Nullable Object mutex) { super(com.fitburlegate, mutex); } @Override Deque com.fitburlegate() { return (Deque) super.com.fitburlegate(); } @Override public void addFirst(E e) { synchronized (mutex) { com.fitburlegate().addFirst(e); } } @Override public void addLast(E e) { synchronized (mutex) { com.fitburlegate().addLast(e); } } @Override public boolean offerFirst(E e) { synchronized (mutex) { return com.fitburlegate().offerFirst(e); } } @Override public boolean offerLast(E e) { synchronized (mutex) { return com.fitburlegate().offerLast(e); } } @Override public E removeFirst() { synchronized (mutex) { return com.fitburlegate().removeFirst(); } } @Override public E removeLast() { synchronized (mutex) { return com.fitburlegate().removeLast(); } } @Override public E pollFirst() { synchronized (mutex) { return com.fitburlegate().pollFirst(); } } @Override public E pollLast() { synchronized (mutex) { return com.fitburlegate().pollLast(); } } @Override public E getFirst() { synchronized (mutex) { return com.fitburlegate().getFirst(); } } @Override public E getLast() { synchronized (mutex) { return com.fitburlegate().getLast(); } } @Override public E peekFirst() { synchronized (mutex) { return com.fitburlegate().peekFirst(); } } @Override public E peekLast() { synchronized (mutex) { return com.fitburlegate().peekLast(); } } @Override public boolean removeFirstOccurrence(Object o) { synchronized (mutex) { return com.fitburlegate().removeFirstOccurrence(o); } } @Override public boolean removeLastOccurrence(Object o) { synchronized (mutex) { return com.fitburlegate().removeLastOccurrence(o); } } @Override public void push(E e) { synchronized (mutex) { com.fitburlegate().push(e); } } @Override public E pop() { synchronized (mutex) { return com.fitburlegate().pop(); } } @Override public Iterator com.fitburscendingIterator() { synchronized (mutex) { return com.fitburlegate().com.fitburscendingIterator(); } } private static final long serialVersionUID = 0; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy