📄 collections.java
字号:
* @serial include */ static class UnmodifiableEntrySet extends UnmodifiableSet { UnmodifiableEntrySet(Set s) { super(s); } public Iterator iterator() { return new Iterator() { Iterator i = c.iterator(); public boolean hasNext() { return i.hasNext(); } public Object next() { return new UnmodifiableEntry((Map.Entry)i.next()); } public void remove() { throw new UnsupportedOperationException(); } }; } public Object[] toArray() { Object[] a = c.toArray(); for (int i=0; i<a.length; i++) a[i] = new UnmodifiableEntry((Map.Entry)a[i]); return a; } public Object[] toArray(Object a[]) { // We don't pass a to c.toArray, to avoid window of // vulnerability wherein an unscrupulous multithreaded client // could get his hands on raw (unwrapped) Entries from c. Object[] arr = c.toArray(a.length==0 ? a : (Object[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), 0)); for (int i=0; i<arr.length; i++) arr[i] = new UnmodifiableEntry((Map.Entry)arr[i]); if (arr.length > a.length) return arr; System.arraycopy(arr, 0, a, 0, arr.length); if (a.length > arr.length) a[arr.length] = null; return a; } /** * This method is overridden to protect the backing set against * an object with a nefarious equals function that senses * that the equality-candidate is Map.Entry and calls its * setValue method. */ public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; return c.contains(new UnmodifiableEntry((Map.Entry)o)); } /** * The next two methods are overridden to protect against * an unscrupulous List whose contains(Object o) method senses * when o is a Map.Entry, and calls o.setValue. */ public boolean containsAll(Collection coll) { Iterator e = coll.iterator(); while (e.hasNext()) if(!contains(e.next())) // Invokes safe contains() above return false; return true; } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Set)) return false; Set s = (Set) o; if (s.size() != c.size()) return false; return containsAll(s); // Invokes safe containsAll() above } /** * This "wrapper class" serves two purposes: it prevents * the client from modifying the backing Map, by short-circuiting * the setValue method, and it protects the backing Map against * an ill-behaved Map.Entry that attempts to modify another * Map Entry when asked to perform an equality check. */ private static class UnmodifiableEntry implements Map.Entry { private Map.Entry e; UnmodifiableEntry(Map.Entry e) {this.e = e;} public Object getKey() {return e.getKey();} public Object getValue() {return e.getValue();} public Object setValue(Object value) { throw new UnsupportedOperationException(); } public int hashCode() {return e.hashCode();} public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry t = (Map.Entry)o; return eq(e.getKey(), t.getKey()) && eq(e.getValue(), t.getValue()); } public String toString() {return e.toString();} } } } /** * Returns an unmodifiable view of the specified sorted map. This method * allows modules to provide users with "read-only" access to internal * sorted maps. Query operations on the returned sorted map "read through" * to the specified sorted map. Attempts to modify the returned * sorted map, whether direct, via its collection views, or via its * <tt>subMap</tt>, <tt>headMap</tt>, or <tt>tailMap</tt> views, result in * an <tt>UnsupportedOperationException</tt>.<p> * * The returned sorted map will be serializable if the specified sorted map * is serializable. * * @param m the sorted map for which an unmodifiable view is to be * returned. * @return an unmodifiable view of the specified sorted map. */ public static SortedMap unmodifiableSortedMap(SortedMap m) { return new UnmodifiableSortedMap(m); } /** * @serial include */ static class UnmodifiableSortedMap extends UnmodifiableMap implements SortedMap, Serializable { private SortedMap sm; UnmodifiableSortedMap(SortedMap m) {super(m); sm = m;} public Comparator comparator() {return sm.comparator();} public SortedMap subMap(Object fromKey, Object toKey) { return new UnmodifiableSortedMap(sm.subMap(fromKey, toKey)); } public SortedMap headMap(Object toKey) { return new UnmodifiableSortedMap(sm.headMap(toKey)); } public SortedMap tailMap(Object fromKey) { return new UnmodifiableSortedMap(sm.tailMap(fromKey)); } public Object firstKey() {return sm.firstKey();} public Object lastKey() {return sm.lastKey();} } // Synch Wrappers /** * Returns a synchronized (thread-safe) collection backed by the specified * collection. In order to guarantee serial access, it is critical that * <strong>all</strong> access to the backing collection is accomplished * through the returned collection.<p> * * It is imperative that the user manually synchronize on the returned * collection when iterating over it: * <pre> * Collection c = Collections.synchronizedCollection(myCollection); * ... * synchronized(c) { * Iterator i = c.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> * Failure to follow this advice may result in non-deterministic behavior. * * <p>The returned collection does <i>not</i> pass the <tt>hashCode</tt> * and <tt>equals</tt> operations through to the backing collection, but * relies on <tt>Object</tt>'s equals and hashCode methods. This is * necessary to preserve the contracts of these operations in the case * that the backing collection is a set or a list.<p> * * The returned collection will be serializable if the specified collection * is serializable. * * @param c the collection to be "wrapped" in a synchronized collection. * @return a synchronized view of the specified collection. */ public static Collection synchronizedCollection(Collection c) { return new SynchronizedCollection(c); } static Collection synchronizedCollection(Collection c, Object mutex) { return new SynchronizedCollection(c, mutex); } /** * @serial include */ static class SynchronizedCollection implements Collection, Serializable { // use serialVersionUID from JDK 1.2.2 for interoperability private static final long serialVersionUID = 3053995032091335093L; Collection c; // Backing Collection Object mutex; // Object on which to synchronize SynchronizedCollection(Collection c) { if (c==null) throw new NullPointerException(); this.c = c; mutex = this; } SynchronizedCollection(Collection c, Object mutex) { this.c = c; this.mutex = mutex; } public int size() { synchronized(mutex) {return c.size();} } public boolean isEmpty() { synchronized(mutex) {return c.isEmpty();} } public boolean contains(Object o) { synchronized(mutex) {return c.contains(o);} } public Object[] toArray() { synchronized(mutex) {return c.toArray();} } public Object[] toArray(Object[] a) { synchronized(mutex) {return c.toArray(a);} } public Iterator iterator() { return c.iterator(); // Must be manually synched by user! } public boolean add(Object o) { synchronized(mutex) {return c.add(o);} } public boolean remove(Object o) { synchronized(mutex) {return c.remove(o);} } public boolean containsAll(Collection coll) { synchronized(mutex) {return c.containsAll(coll);} } public boolean addAll(Collection coll) { synchronized(mutex) {return c.addAll(coll);} } public boolean removeAll(Collection coll) { synchronized(mutex) {return c.removeAll(coll);} } public boolean retainAll(Collection coll) { synchronized(mutex) {return c.retainAll(coll);} } public void clear() { synchronized(mutex) {c.clear();} } public String toString() { synchronized(mutex) {return c.toString();} } } /** * Returns a synchronized (thread-safe) set backed by the specified * set. In order to guarantee serial access, it is critical that * <strong>all</strong> access to the backing set is accomplished * through the returned set.<p> * * It is imperative that the user manually synchronize on the returned * set when iterating over it: * <pre> * Set s = Collections.synchronizedSet(new HashSet()); * ... * synchronized(s) { * Iterator i = s.iterator(); // Must be in the synchronized block * while (i.hasNext()) * foo(i.next()); * } * </pre> * Failure to follow this advice may result in non-deterministic behavior. * * <p>The returned set will be serializable if the specified set is * serializable. * * @param s the set to be "wrapped" in a synchronized set. * @return a synchronized view of the specified set. */ public static Set synchronizedSet(Set s) { return new SynchronizedSet(s); } static Set synchronizedSet(Set s, Object mutex) { return new SynchronizedSet(s, mutex); } /** * @serial include */ static class SynchronizedSet extends SynchronizedCollection implements Set { SynchronizedSet(Set s) { super(s); } SynchronizedSet(Set s, Object mutex) { super(s, mutex); } public boolean equals(Object o) { synchronized(mutex) {return c.equals(o);} } public int hashCode() { synchronized(mutex) {return c.hashCode();} } } /** * Returns a synchronized (thread-safe) sorted set backed by the specified * sorted set. In order to guarantee serial access, it is critical that * <strong>all</strong> access to the backing sorted set is accomplished * through the returned sorted set (or its views).<p> * * It is imperative that the user manually synchronize on the returned * sorted set when iterating over it or any of its <tt>subSet</tt>, * <tt>headSet</tt>, or <tt>tailSet</tt> vi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -