org.docx4j.com.google.common.collect.Synchronized Maven / Gradle / Ivy
/*
* Copyright (C) 2007 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/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 org.docx4j.com.google.common.collect;
import static org.docx4j.com.google.common.base.Preconditions.checkNotNull;
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 java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.docx4j.com.google.common.annotations.GwtCompatible;
import org.docx4j.com.google.common.annotations.GwtIncompatible;
import org.docx4j.com.google.common.annotations.VisibleForTesting;
/**
* 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 delegate;
final Object mutex;
SynchronizedObject(Object delegate, @Nullable Object mutex) {
this.delegate = checkNotNull(delegate);
this.mutex = (mutex == null) ? this : mutex;
}
Object delegate() {
return delegate;
}
// No equals and hashCode; see ForwardingObject for details.
@Override
public String toString() {
synchronized (mutex) {
return delegate.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.defaultWriteObject();
}
}
@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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@SuppressWarnings("unchecked")
@Override
Collection delegate() {
return (Collection) super.delegate();
}
@Override
public boolean add(E e) {
synchronized (mutex) {
return delegate().add(e);
}
}
@Override
public boolean addAll(Collection c) {
synchronized (mutex) {
return delegate().addAll(c);
}
}
@Override
public void clear() {
synchronized (mutex) {
delegate().clear();
}
}
@Override
public boolean contains(Object o) {
synchronized (mutex) {
return delegate().contains(o);
}
}
@Override
public boolean containsAll(Collection c) {
synchronized (mutex) {
return delegate().containsAll(c);
}
}
@Override
public boolean isEmpty() {
synchronized (mutex) {
return delegate().isEmpty();
}
}
@Override
public Iterator iterator() {
return delegate().iterator(); // manually synchronized
}
@Override
public Spliterator spliterator() {
synchronized (mutex) {
return delegate().spliterator();
}
}
@Override
public Stream stream() {
synchronized (mutex) {
return delegate().stream();
}
}
@Override
public Stream parallelStream() {
synchronized (mutex) {
return delegate().parallelStream();
}
}
@Override
public void forEach(Consumer action) {
synchronized (mutex) {
delegate().forEach(action);
}
}
@Override
public boolean remove(Object o) {
synchronized (mutex) {
return delegate().remove(o);
}
}
@Override
public boolean removeAll(Collection c) {
synchronized (mutex) {
return delegate().removeAll(c);
}
}
@Override
public boolean retainAll(Collection c) {
synchronized (mutex) {
return delegate().retainAll(c);
}
}
@Override
public boolean removeIf(Predicate filter) {
synchronized (mutex) {
return delegate().removeIf(filter);
}
}
@Override
public int size() {
synchronized (mutex) {
return delegate().size();
}
}
@Override
public Object[] toArray() {
synchronized (mutex) {
return delegate().toArray();
}
}
@Override
public T[] toArray(T[] a) {
synchronized (mutex) {
return delegate().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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
Set delegate() {
return (Set) super.delegate();
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
synchronized (mutex) {
return delegate().equals(o);
}
}
@Override
public int hashCode() {
synchronized (mutex) {
return delegate().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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
SortedSet delegate() {
return (SortedSet) super.delegate();
}
@Override
public Comparator comparator() {
synchronized (mutex) {
return delegate().comparator();
}
}
@Override
public SortedSet subSet(E fromElement, E toElement) {
synchronized (mutex) {
return sortedSet(delegate().subSet(fromElement, toElement), mutex);
}
}
@Override
public SortedSet headSet(E toElement) {
synchronized (mutex) {
return sortedSet(delegate().headSet(toElement), mutex);
}
}
@Override
public SortedSet tailSet(E fromElement) {
synchronized (mutex) {
return sortedSet(delegate().tailSet(fromElement), mutex);
}
}
@Override
public E first() {
synchronized (mutex) {
return delegate().first();
}
}
@Override
public E last() {
synchronized (mutex) {
return delegate().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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
List delegate() {
return (List) super.delegate();
}
@Override
public void add(int index, E element) {
synchronized (mutex) {
delegate().add(index, element);
}
}
@Override
public boolean addAll(int index, Collection c) {
synchronized (mutex) {
return delegate().addAll(index, c);
}
}
@Override
public E get(int index) {
synchronized (mutex) {
return delegate().get(index);
}
}
@Override
public int indexOf(Object o) {
synchronized (mutex) {
return delegate().indexOf(o);
}
}
@Override
public int lastIndexOf(Object o) {
synchronized (mutex) {
return delegate().lastIndexOf(o);
}
}
@Override
public ListIterator listIterator() {
return delegate().listIterator(); // manually synchronized
}
@Override
public ListIterator listIterator(int index) {
return delegate().listIterator(index); // manually synchronized
}
@Override
public E remove(int index) {
synchronized (mutex) {
return delegate().remove(index);
}
}
@Override
public E set(int index, E element) {
synchronized (mutex) {
return delegate().set(index, element);
}
}
@Override
public void replaceAll(UnaryOperator operator) {
synchronized (mutex) {
delegate().replaceAll(operator);
}
}
@Override
public void sort(Comparator c) {
synchronized (mutex) {
delegate().sort(c);
}
}
@Override
public List subList(int fromIndex, int toIndex) {
synchronized (mutex) {
return list(delegate().subList(fromIndex, toIndex), mutex);
}
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
synchronized (mutex) {
return delegate().equals(o);
}
}
@Override
public int hashCode() {
synchronized (mutex) {
return delegate().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;
}
@VisibleForTesting
static Map map(Map map, @Nullable Object mutex) {
return new SynchronizedMap<>(map, mutex);
}
private static class SynchronizedMap extends SynchronizedObject implements Map {
@MonotonicNonNull transient Set keySet;
@MonotonicNonNull transient Collection values;
@MonotonicNonNull transient Set> entrySet;
SynchronizedMap(Map delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@SuppressWarnings("unchecked")
@Override
Map delegate() {
return (Map) super.delegate();
}
@Override
public void clear() {
synchronized (mutex) {
delegate().clear();
}
}
@Override
public boolean containsKey(Object key) {
synchronized (mutex) {
return delegate().containsKey(key);
}
}
@Override
public boolean containsValue(Object value) {
synchronized (mutex) {
return delegate().containsValue(value);
}
}
@Override
public Set> entrySet() {
synchronized (mutex) {
if (entrySet == null) {
entrySet = set(delegate().entrySet(), mutex);
}
return entrySet;
}
}
@Override
public void forEach(BiConsumer action) {
synchronized (mutex) {
delegate().forEach(action);
}
}
@Override
public V get(Object key) {
synchronized (mutex) {
return delegate().get(key);
}
}
@Override
public V getOrDefault(Object key, V defaultValue) {
synchronized (mutex) {
return delegate().getOrDefault(key, defaultValue);
}
}
@Override
public boolean isEmpty() {
synchronized (mutex) {
return delegate().isEmpty();
}
}
@Override
public Set keySet() {
synchronized (mutex) {
if (keySet == null) {
keySet = set(delegate().keySet(), mutex);
}
return keySet;
}
}
@Override
public V put(K key, V value) {
synchronized (mutex) {
return delegate().put(key, value);
}
}
@Override
public V putIfAbsent(K key, V value) {
synchronized (mutex) {
return delegate().putIfAbsent(key, value);
}
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
synchronized (mutex) {
return delegate().replace(key, oldValue, newValue);
}
}
@Override
public V replace(K key, V value) {
synchronized (mutex) {
return delegate().replace(key, value);
}
}
@Override
public V computeIfAbsent(K key, Function mappingFunction) {
synchronized (mutex) {
return delegate().computeIfAbsent(key, mappingFunction);
}
}
@Override
public V computeIfPresent(
K key, BiFunction remappingFunction) {
synchronized (mutex) {
return delegate().computeIfPresent(key, remappingFunction);
}
}
@Override
public V compute(K key, BiFunction remappingFunction) {
synchronized (mutex) {
return delegate().compute(key, remappingFunction);
}
}
@Override
public V merge(
K key, V value, BiFunction remappingFunction) {
synchronized (mutex) {
return delegate().merge(key, value, remappingFunction);
}
}
@Override
public void putAll(Map map) {
synchronized (mutex) {
delegate().putAll(map);
}
}
@Override
public void replaceAll(BiFunction function) {
synchronized (mutex) {
delegate().replaceAll(function);
}
}
@Override
public V remove(Object key) {
synchronized (mutex) {
return delegate().remove(key);
}
}
@Override
public boolean remove(Object key, Object value) {
synchronized (mutex) {
return delegate().remove(key, value);
}
}
@Override
public int size() {
synchronized (mutex) {
return delegate().size();
}
}
@Override
public Collection values() {
synchronized (mutex) {
if (values == null) {
values = collection(delegate().values(), mutex);
}
return values;
}
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
synchronized (mutex) {
return delegate().equals(o);
}
}
@Override
public int hashCode() {
synchronized (mutex) {
return delegate().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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
SortedMap delegate() {
return (SortedMap) super.delegate();
}
@Override
public Comparator comparator() {
synchronized (mutex) {
return delegate().comparator();
}
}
@Override
public K firstKey() {
synchronized (mutex) {
return delegate().firstKey();
}
}
@Override
public SortedMap headMap(K toKey) {
synchronized (mutex) {
return sortedMap(delegate().headMap(toKey), mutex);
}
}
@Override
public K lastKey() {
synchronized (mutex) {
return delegate().lastKey();
}
}
@Override
public SortedMap subMap(K fromKey, K toKey) {
synchronized (mutex) {
return sortedMap(delegate().subMap(fromKey, toKey), mutex);
}
}
@Override
public SortedMap tailMap(K fromKey) {
synchronized (mutex) {
return sortedMap(delegate().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 @MonotonicNonNull Set valueSet;
// @MonotonicNonNull @RetainedWith private transient BiMap inverse;
//
// private SynchronizedBiMap(
// BiMap delegate, @Nullable Object mutex, @Nullable BiMap inverse) {
// super(delegate, mutex);
// this.inverse = inverse;
// }
//
// @Override
// BiMap delegate() {
// return (BiMap) super.delegate();
// }
//
// @Override
// public Set values() {
// synchronized (mutex) {
// if (valueSet == null) {
// valueSet = set(delegate().values(), mutex);
// }
// return valueSet;
// }
// }
//
// @Override
// public V forcePut(K key, V value) {
// synchronized (mutex) {
// return delegate().forcePut(key, value);
// }
// }
//
// @Override
// public BiMap inverse() {
// synchronized (mutex) {
// if (inverse == null) {
// inverse = new SynchronizedBiMap<>(delegate().inverse(), mutex, this);
// }
// return inverse;
// }
// }
//
// private static final long serialVersionUID = 0;
// }
//
// private static class SynchronizedAsMap extends SynchronizedMap> {
// @MonotonicNonNull transient Set>> asMapEntrySet;
// @MonotonicNonNull transient Collection> asMapValues;
//
// SynchronizedAsMap(Map> delegate, @Nullable Object mutex) {
// super(delegate, 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<>(delegate().entrySet(), mutex);
// }
// return asMapEntrySet;
// }
// }
//
// @Override
// public Collection> values() {
// synchronized (mutex) {
// if (asMapValues == null) {
// asMapValues = new SynchronizedAsMapValues(delegate().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> delegate, @Nullable Object mutex) {
// super(delegate, mutex);
// }
//
// @Override
// public Iterator> iterator() {
// // Must be manually synchronized.
// return new TransformedIterator, Collection>(super.iterator()) {
// @Override
// Collection transform(Collection from) {
// return typePreservingCollection(from, mutex);
// }
// };
// }
//
// private static final long serialVersionUID = 0;
// }
@GwtIncompatible // NavigableSet
@VisibleForTesting
static class SynchronizedNavigableSet extends SynchronizedSortedSet
implements NavigableSet {
SynchronizedNavigableSet(NavigableSet delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
NavigableSet delegate() {
return (NavigableSet) super.delegate();
}
@Override
public E ceiling(E e) {
synchronized (mutex) {
return delegate().ceiling(e);
}
}
@Override
public Iterator descendingIterator() {
return delegate().descendingIterator(); // manually synchronized
}
@MonotonicNonNull transient NavigableSet descendingSet;
@Override
public NavigableSet descendingSet() {
synchronized (mutex) {
if (descendingSet == null) {
NavigableSet dS = Synchronized.navigableSet(delegate().descendingSet(), mutex);
descendingSet = dS;
return dS;
}
return descendingSet;
}
}
@Override
public E floor(E e) {
synchronized (mutex) {
return delegate().floor(e);
}
}
@Override
public NavigableSet headSet(E toElement, boolean inclusive) {
synchronized (mutex) {
return Synchronized.navigableSet(delegate().headSet(toElement, inclusive), mutex);
}
}
@Override
public SortedSet headSet(E toElement) {
return headSet(toElement, false);
}
@Override
public E higher(E e) {
synchronized (mutex) {
return delegate().higher(e);
}
}
@Override
public E lower(E e) {
synchronized (mutex) {
return delegate().lower(e);
}
}
@Override
public E pollFirst() {
synchronized (mutex) {
return delegate().pollFirst();
}
}
@Override
public E pollLast() {
synchronized (mutex) {
return delegate().pollLast();
}
}
@Override
public NavigableSet subSet(
E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
synchronized (mutex) {
return Synchronized.navigableSet(
delegate().subSet(fromElement, fromInclusive, toElement, toInclusive), mutex);
}
}
@Override
public SortedSet subSet(E fromElement, E toElement) {
return subSet(fromElement, true, toElement, false);
}
@Override
public NavigableSet tailSet(E fromElement, boolean inclusive) {
synchronized (mutex) {
return Synchronized.navigableSet(delegate().tailSet(fromElement, inclusive), mutex);
}
}
@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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
NavigableMap delegate() {
return (NavigableMap) super.delegate();
}
@Override
public Entry ceilingEntry(K key) {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().ceilingEntry(key), mutex);
}
}
@Override
public K ceilingKey(K key) {
synchronized (mutex) {
return delegate().ceilingKey(key);
}
}
@MonotonicNonNull transient NavigableSet descendingKeySet;
@Override
public NavigableSet descendingKeySet() {
synchronized (mutex) {
if (descendingKeySet == null) {
return descendingKeySet = Synchronized.navigableSet(delegate().descendingKeySet(), mutex);
}
return descendingKeySet;
}
}
@MonotonicNonNull transient NavigableMap descendingMap;
@Override
public NavigableMap descendingMap() {
synchronized (mutex) {
if (descendingMap == null) {
return descendingMap = navigableMap(delegate().descendingMap(), mutex);
}
return descendingMap;
}
}
@Override
public Entry firstEntry() {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().firstEntry(), mutex);
}
}
@Override
public Entry floorEntry(K key) {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().floorEntry(key), mutex);
}
}
@Override
public K floorKey(K key) {
synchronized (mutex) {
return delegate().floorKey(key);
}
}
@Override
public NavigableMap headMap(K toKey, boolean inclusive) {
synchronized (mutex) {
return navigableMap(delegate().headMap(toKey, inclusive), mutex);
}
}
@Override
public SortedMap headMap(K toKey) {
return headMap(toKey, false);
}
@Override
public Entry higherEntry(K key) {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().higherEntry(key), mutex);
}
}
@Override
public K higherKey(K key) {
synchronized (mutex) {
return delegate().higherKey(key);
}
}
@Override
public Entry lastEntry() {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().lastEntry(), mutex);
}
}
@Override
public Entry lowerEntry(K key) {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().lowerEntry(key), mutex);
}
}
@Override
public K lowerKey(K key) {
synchronized (mutex) {
return delegate().lowerKey(key);
}
}
@Override
public Set keySet() {
return navigableKeySet();
}
@MonotonicNonNull transient NavigableSet navigableKeySet;
@Override
public NavigableSet navigableKeySet() {
synchronized (mutex) {
if (navigableKeySet == null) {
return navigableKeySet = Synchronized.navigableSet(delegate().navigableKeySet(), mutex);
}
return navigableKeySet;
}
}
@Override
public Entry pollFirstEntry() {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().pollFirstEntry(), mutex);
}
}
@Override
public Entry pollLastEntry() {
synchronized (mutex) {
return nullableSynchronizedEntry(delegate().pollLastEntry(), mutex);
}
}
@Override
public NavigableMap subMap(
K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
synchronized (mutex) {
return navigableMap(delegate().subMap(fromKey, fromInclusive, toKey, toInclusive), mutex);
}
}
@Override
public SortedMap subMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}
@Override
public NavigableMap tailMap(K fromKey, boolean inclusive) {
synchronized (mutex) {
return navigableMap(delegate().tailMap(fromKey, inclusive), mutex);
}
}
@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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@SuppressWarnings("unchecked") // guaranteed by the constructor
@Override
Entry delegate() {
return (Entry) super.delegate();
}
@Override
public boolean equals(Object obj) {
synchronized (mutex) {
return delegate().equals(obj);
}
}
@Override
public int hashCode() {
synchronized (mutex) {
return delegate().hashCode();
}
}
@Override
public K getKey() {
synchronized (mutex) {
return delegate().getKey();
}
}
@Override
public V getValue() {
synchronized (mutex) {
return delegate().getValue();
}
}
@Override
public V setValue(V value) {
synchronized (mutex) {
return delegate().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 delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
Queue delegate() {
return (Queue) super.delegate();
}
@Override
public E element() {
synchronized (mutex) {
return delegate().element();
}
}
@Override
public boolean offer(E e) {
synchronized (mutex) {
return delegate().offer(e);
}
}
@Override
public E peek() {
synchronized (mutex) {
return delegate().peek();
}
}
@Override
public E poll() {
synchronized (mutex) {
return delegate().poll();
}
}
@Override
public E remove() {
synchronized (mutex) {
return delegate().remove();
}
}
private static final long serialVersionUID = 0;
}
static Deque deque(Deque deque, @Nullable Object mutex) {
return new SynchronizedDeque(deque, mutex);
}
private static final class SynchronizedDeque extends SynchronizedQueue implements Deque {
SynchronizedDeque(Deque delegate, @Nullable Object mutex) {
super(delegate, mutex);
}
@Override
Deque delegate() {
return (Deque) super.delegate();
}
@Override
public void addFirst(E e) {
synchronized (mutex) {
delegate().addFirst(e);
}
}
@Override
public void addLast(E e) {
synchronized (mutex) {
delegate().addLast(e);
}
}
@Override
public boolean offerFirst(E e) {
synchronized (mutex) {
return delegate().offerFirst(e);
}
}
@Override
public boolean offerLast(E e) {
synchronized (mutex) {
return delegate().offerLast(e);
}
}
@Override
public E removeFirst() {
synchronized (mutex) {
return delegate().removeFirst();
}
}
@Override
public E removeLast() {
synchronized (mutex) {
return delegate().removeLast();
}
}
@Override
public E pollFirst() {
synchronized (mutex) {
return delegate().pollFirst();
}
}
@Override
public E pollLast() {
synchronized (mutex) {
return delegate().pollLast();
}
}
@Override
public E getFirst() {
synchronized (mutex) {
return delegate().getFirst();
}
}
@Override
public E getLast() {
synchronized (mutex) {
return delegate().getLast();
}
}
@Override
public E peekFirst() {
synchronized (mutex) {
return delegate().peekFirst();
}
}
@Override
public E peekLast() {
synchronized (mutex) {
return delegate().peekLast();
}
}
@Override
public boolean removeFirstOccurrence(Object o) {
synchronized (mutex) {
return delegate().removeFirstOccurrence(o);
}
}
@Override
public boolean removeLastOccurrence(Object o) {
synchronized (mutex) {
return delegate().removeLastOccurrence(o);
}
}
@Override
public void push(E e) {
synchronized (mutex) {
delegate().push(e);
}
}
@Override
public E pop() {
synchronized (mutex) {
return delegate().pop();
}
}
@Override
public Iterator descendingIterator() {
synchronized (mutex) {
return delegate().descendingIterator();
}
}
private static final long serialVersionUID = 0;
}
}