src/share/classes/java/util/Collections.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1472 
1473             /**
1474              * This "wrapper class" serves two purposes: it prevents
1475              * the client from modifying the backing Map, by short-circuiting
1476              * the setValue method, and it protects the backing Map against
1477              * an ill-behaved Map.Entry that attempts to modify another
1478              * Map Entry when asked to perform an equality check.
1479              */
1480             private static class UnmodifiableEntry<K,V> implements Map.Entry<K,V> {
1481                 private Map.Entry<? extends K, ? extends V> e;
1482 
1483                 UnmodifiableEntry(Map.Entry<? extends K, ? extends V> e) {this.e = e;}
1484 
1485                 public K getKey()        {return e.getKey();}
1486                 public V getValue()      {return e.getValue();}
1487                 public V setValue(V value) {
1488                     throw new UnsupportedOperationException();
1489                 }
1490                 public int hashCode()    {return e.hashCode();}
1491                 public boolean equals(Object o) {


1492                     if (!(o instanceof Map.Entry))
1493                         return false;
1494                     Map.Entry t = (Map.Entry)o;
1495                     return eq(e.getKey(),   t.getKey()) &&
1496                            eq(e.getValue(), t.getValue());
1497                 }
1498                 public String toString() {return e.toString();}
1499             }
1500         }
1501     }
1502 
1503     /**
1504      * Returns an unmodifiable view of the specified sorted map.  This method
1505      * allows modules to provide users with "read-only" access to internal
1506      * sorted maps.  Query operations on the returned sorted map "read through"
1507      * to the specified sorted map.  Attempts to modify the returned
1508      * sorted map, whether direct, via its collection views, or via its
1509      * <tt>subMap</tt>, <tt>headMap</tt>, or <tt>tailMap</tt> views, result in
1510      * an <tt>UnsupportedOperationException</tt>.<p>
1511      *


1692     static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) {
1693         return new SynchronizedSet<>(s, mutex);
1694     }
1695 
1696     /**
1697      * @serial include
1698      */
1699     static class SynchronizedSet<E>
1700           extends SynchronizedCollection<E>
1701           implements Set<E> {
1702         private static final long serialVersionUID = 487447009682186044L;
1703 
1704         SynchronizedSet(Set<E> s) {
1705             super(s);
1706         }
1707         SynchronizedSet(Set<E> s, Object mutex) {
1708             super(s, mutex);
1709         }
1710 
1711         public boolean equals(Object o) {


1712             synchronized (mutex) {return c.equals(o);}
1713         }
1714         public int hashCode() {
1715             synchronized (mutex) {return c.hashCode();}
1716         }
1717     }
1718 
1719     /**
1720      * Returns a synchronized (thread-safe) sorted set backed by the specified
1721      * sorted set.  In order to guarantee serial access, it is critical that
1722      * <strong>all</strong> access to the backing sorted set is accomplished
1723      * through the returned sorted set (or its views).<p>
1724      *
1725      * It is imperative that the user manually synchronize on the returned
1726      * sorted set when iterating over it or any of its <tt>subSet</tt>,
1727      * <tt>headSet</tt>, or <tt>tailSet</tt> views.
1728      * <pre>
1729      *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
1730      *      ...
1731      *  synchronized (s) {


1846     /**
1847      * @serial include
1848      */
1849     static class SynchronizedList<E>
1850         extends SynchronizedCollection<E>
1851         implements List<E> {
1852         private static final long serialVersionUID = -7754090372962971524L;
1853 
1854         final List<E> list;
1855 
1856         SynchronizedList(List<E> list) {
1857             super(list);
1858             this.list = list;
1859         }
1860         SynchronizedList(List<E> list, Object mutex) {
1861             super(list, mutex);
1862             this.list = list;
1863         }
1864 
1865         public boolean equals(Object o) {


1866             synchronized (mutex) {return list.equals(o);}
1867         }
1868         public int hashCode() {
1869             synchronized (mutex) {return list.hashCode();}
1870         }
1871 
1872         public E get(int index) {
1873             synchronized (mutex) {return list.get(index);}
1874         }
1875         public E set(int index, E element) {
1876             synchronized (mutex) {return list.set(index, element);}
1877         }
1878         public void add(int index, E element) {
1879             synchronized (mutex) {list.add(index, element);}
1880         }
1881         public E remove(int index) {
1882             synchronized (mutex) {return list.remove(index);}
1883         }
1884 
1885         public int indexOf(Object o) {


2056             }
2057         }
2058 
2059         public Set<Map.Entry<K,V>> entrySet() {
2060             synchronized (mutex) {
2061                 if (entrySet==null)
2062                     entrySet = new SynchronizedSet<>(m.entrySet(), mutex);
2063                 return entrySet;
2064             }
2065         }
2066 
2067         public Collection<V> values() {
2068             synchronized (mutex) {
2069                 if (values==null)
2070                     values = new SynchronizedCollection<>(m.values(), mutex);
2071                 return values;
2072             }
2073         }
2074 
2075         public boolean equals(Object o) {


2076             synchronized (mutex) {return m.equals(o);}
2077         }
2078         public int hashCode() {
2079             synchronized (mutex) {return m.hashCode();}
2080         }
2081         public String toString() {
2082             synchronized (mutex) {return m.toString();}
2083         }
2084         private void writeObject(ObjectOutputStream s) throws IOException {
2085             synchronized (mutex) {s.defaultWriteObject();}
2086         }
2087     }
2088 
2089     /**
2090      * Returns a synchronized (thread-safe) sorted map backed by the specified
2091      * sorted map.  In order to guarantee serial access, it is critical that
2092      * <strong>all</strong> access to the backing sorted map is accomplished
2093      * through the returned sorted map (or its views).<p>
2094      *
2095      * It is imperative that the user manually synchronize on the returned


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1472 
1473             /**
1474              * This "wrapper class" serves two purposes: it prevents
1475              * the client from modifying the backing Map, by short-circuiting
1476              * the setValue method, and it protects the backing Map against
1477              * an ill-behaved Map.Entry that attempts to modify another
1478              * Map Entry when asked to perform an equality check.
1479              */
1480             private static class UnmodifiableEntry<K,V> implements Map.Entry<K,V> {
1481                 private Map.Entry<? extends K, ? extends V> e;
1482 
1483                 UnmodifiableEntry(Map.Entry<? extends K, ? extends V> e) {this.e = e;}
1484 
1485                 public K getKey()        {return e.getKey();}
1486                 public V getValue()      {return e.getValue();}
1487                 public V setValue(V value) {
1488                     throw new UnsupportedOperationException();
1489                 }
1490                 public int hashCode()    {return e.hashCode();}
1491                 public boolean equals(Object o) {
1492                     if (this == o)
1493                         return true;
1494                     if (!(o instanceof Map.Entry))
1495                         return false;
1496                     Map.Entry t = (Map.Entry)o;
1497                     return eq(e.getKey(),   t.getKey()) &&
1498                            eq(e.getValue(), t.getValue());
1499                 }
1500                 public String toString() {return e.toString();}
1501             }
1502         }
1503     }
1504 
1505     /**
1506      * Returns an unmodifiable view of the specified sorted map.  This method
1507      * allows modules to provide users with "read-only" access to internal
1508      * sorted maps.  Query operations on the returned sorted map "read through"
1509      * to the specified sorted map.  Attempts to modify the returned
1510      * sorted map, whether direct, via its collection views, or via its
1511      * <tt>subMap</tt>, <tt>headMap</tt>, or <tt>tailMap</tt> views, result in
1512      * an <tt>UnsupportedOperationException</tt>.<p>
1513      *


1694     static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) {
1695         return new SynchronizedSet<>(s, mutex);
1696     }
1697 
1698     /**
1699      * @serial include
1700      */
1701     static class SynchronizedSet<E>
1702           extends SynchronizedCollection<E>
1703           implements Set<E> {
1704         private static final long serialVersionUID = 487447009682186044L;
1705 
1706         SynchronizedSet(Set<E> s) {
1707             super(s);
1708         }
1709         SynchronizedSet(Set<E> s, Object mutex) {
1710             super(s, mutex);
1711         }
1712 
1713         public boolean equals(Object o) {
1714             if (this == o)
1715                 return true;
1716             synchronized (mutex) {return c.equals(o);}
1717         }
1718         public int hashCode() {
1719             synchronized (mutex) {return c.hashCode();}
1720         }
1721     }
1722 
1723     /**
1724      * Returns a synchronized (thread-safe) sorted set backed by the specified
1725      * sorted set.  In order to guarantee serial access, it is critical that
1726      * <strong>all</strong> access to the backing sorted set is accomplished
1727      * through the returned sorted set (or its views).<p>
1728      *
1729      * It is imperative that the user manually synchronize on the returned
1730      * sorted set when iterating over it or any of its <tt>subSet</tt>,
1731      * <tt>headSet</tt>, or <tt>tailSet</tt> views.
1732      * <pre>
1733      *  SortedSet s = Collections.synchronizedSortedSet(new TreeSet());
1734      *      ...
1735      *  synchronized (s) {


1850     /**
1851      * @serial include
1852      */
1853     static class SynchronizedList<E>
1854         extends SynchronizedCollection<E>
1855         implements List<E> {
1856         private static final long serialVersionUID = -7754090372962971524L;
1857 
1858         final List<E> list;
1859 
1860         SynchronizedList(List<E> list) {
1861             super(list);
1862             this.list = list;
1863         }
1864         SynchronizedList(List<E> list, Object mutex) {
1865             super(list, mutex);
1866             this.list = list;
1867         }
1868 
1869         public boolean equals(Object o) {
1870             if (this == o) 
1871                 return true;
1872             synchronized (mutex) {return list.equals(o);}
1873         }
1874         public int hashCode() {
1875             synchronized (mutex) {return list.hashCode();}
1876         }
1877 
1878         public E get(int index) {
1879             synchronized (mutex) {return list.get(index);}
1880         }
1881         public E set(int index, E element) {
1882             synchronized (mutex) {return list.set(index, element);}
1883         }
1884         public void add(int index, E element) {
1885             synchronized (mutex) {list.add(index, element);}
1886         }
1887         public E remove(int index) {
1888             synchronized (mutex) {return list.remove(index);}
1889         }
1890 
1891         public int indexOf(Object o) {


2062             }
2063         }
2064 
2065         public Set<Map.Entry<K,V>> entrySet() {
2066             synchronized (mutex) {
2067                 if (entrySet==null)
2068                     entrySet = new SynchronizedSet<>(m.entrySet(), mutex);
2069                 return entrySet;
2070             }
2071         }
2072 
2073         public Collection<V> values() {
2074             synchronized (mutex) {
2075                 if (values==null)
2076                     values = new SynchronizedCollection<>(m.values(), mutex);
2077                 return values;
2078             }
2079         }
2080 
2081         public boolean equals(Object o) {
2082             if (this == o)
2083                 return true;
2084             synchronized (mutex) {return m.equals(o);}
2085         }
2086         public int hashCode() {
2087             synchronized (mutex) {return m.hashCode();}
2088         }
2089         public String toString() {
2090             synchronized (mutex) {return m.toString();}
2091         }
2092         private void writeObject(ObjectOutputStream s) throws IOException {
2093             synchronized (mutex) {s.defaultWriteObject();}
2094         }
2095     }
2096 
2097     /**
2098      * Returns a synchronized (thread-safe) sorted map backed by the specified
2099      * sorted map.  In order to guarantee serial access, it is critical that
2100      * <strong>all</strong> access to the backing sorted map is accomplished
2101      * through the returned sorted map (or its views).<p>
2102      *
2103      * It is imperative that the user manually synchronize on the returned