⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collections.java

📁 this gcc-g++-3.3.1.tar.gz is a source file of gcc, you can learn more about gcc through this codes f
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /**     * The single key.     * @serial the singleton key     */    private final Object k;    /**     * The corresponding value.     * @serial the singleton value     */    private final Object v;    /**     * Cache the entry set.     */    private transient Set entries;    /**     * Construct a singleton.     * @param key the key     * @param value the value     */    SingletonMap(Object key, Object value)    {      k = key;      v = value;    }    /**     * There is a single immutable entry.     */    public Set entrySet()    {      if (entries == null)        entries = singleton(new AbstractMap.BasicMapEntry(k, v)        {          public Object setValue(Object o)          {            throw new UnsupportedOperationException();          }        });      return entries;    }    // The remaining methods are optional, but provide a performance    // advantage by not allocating unnecessary iterators in AbstractMap.    /**     * Single entry.     */    public boolean containsKey(Object key)    {      return equals(key, k);    }    /**     * Single entry.     */    public boolean containsValue(Object value)    {      return equals(value, v);    }    /**     * Single entry.     */    public Object get(Object key)    {      return equals(key, k) ? v : null;    }    /**     * Calculate the hashcode directly.     */    public int hashCode()    {      return hashCode(k) ^ hashCode(v);    }    /**     * Return the keyset.     */    public Set keySet()    {      if (keys == null)        keys = singleton(k);      return keys;    }    /**     * The size: always 1!     */    public int size()    {      return 1;    }    /**     * Return the values. Technically, a singleton, while more specific than     * a general Collection, will work. Besides, that's what the JDK uses!     */    public Collection values()    {      if (values == null)        values = singleton(v);      return values;    }    /**     * Obvious string.     */    public String toString()    {      return "{" + k + "=" + v + "}";    }  } // class SingletonMap  /**   * Sort a list according to the natural ordering of its elements. The list   * must be modifiable, but can be of fixed size. The sort algorithm is   * precisely that used by Arrays.sort(Object[]), which offers guaranteed   * nlog(n) performance. This implementation dumps the list into an array,   * sorts the array, and then iterates over the list setting each element from   * the array.   *   * @param l the List to sort   * @throws ClassCastException if some items are not mutually comparable   * @throws UnsupportedOperationException if the List is not modifiable   * @throws NullPointerException if some element is null   * @see Arrays#sort(Object[])   */  public static void sort(List l)  {    sort(l, null);  }  /**   * Sort a list according to a specified Comparator. The list must be   * modifiable, but can be of fixed size. The sort algorithm is precisely that   * used by Arrays.sort(Object[], Comparator), which offers guaranteed   * nlog(n) performance. This implementation dumps the list into an array,   * sorts the array, and then iterates over the list setting each element from   * the array.   *   * @param l the List to sort   * @param c the Comparator specifying the ordering for the elements, or   *        null for natural ordering   * @throws ClassCastException if c will not compare some pair of items   * @throws UnsupportedOperationException if the List is not modifiable   * @throws NullPointerException if null is compared by natural ordering   *        (only possible when c is null)   * @see Arrays#sort(Object[], Comparator)   */  public static void sort(List l, Comparator c)  {    Object[] a = l.toArray();    Arrays.sort(a, c);    ListIterator i = l.listIterator(a.length);    for (int pos = a.length; --pos >= 0; )      {	i.previous();	i.set(a[pos]);      }  }  /**   * Swaps the elements at the specified positions within the list. Equal   * positions have no effect.   *   * @param l the list to work on   * @param i the first index to swap   * @param j the second index   * @throws UnsupportedOperationException if list.set is not supported   * @throws IndexOutOfBoundsException if either i or j is &lt; 0 or &gt;=   *         list.size()   * @since 1.4   */  public static void swap(List l, int i, int j)  {    l.set(i, l.set(j, l.get(i)));  }  /**   * Returns a synchronized (thread-safe) collection wrapper backed by the   * given collection. Notice that element access through the iterators   * is thread-safe, but if the collection can be structurally modified   * (adding or removing elements) then you should synchronize around the   * iteration to avoid non-deterministic behavior:<br>   * <pre>   * Collection c = Collections.synchronizedCollection(new Collection(...));   * ...   * synchronized (c)   *   {   *     Iterator i = c.iterator();   *     while (i.hasNext())   *       foo(i.next());   *   }   * </pre><p>   *   * Since the collection might be a List or a Set, and those have incompatible   * equals and hashCode requirements, this relies on Object's implementation   * rather than passing those calls on to the wrapped collection. The returned   * Collection implements Serializable, but can only be serialized if   * the collection it wraps is likewise Serializable.   *   * @param c the collection to wrap   * @return a synchronized view of the collection   * @see Serializable   */  public static Collection synchronizedCollection(Collection c)  {    return new SynchronizedCollection(c);  }  /**   * The implementation of {@link #synchronizedCollection(Collection)}. This   * class name is required for compatibility with Sun's JDK serializability.   * Package visible, so that collections such as the one for   * Hashtable.values() can specify which object to synchronize on.   *   * @author Eric Blake <ebb9@email.byu.edu>   */  static class SynchronizedCollection    implements Collection, Serializable  {    /**     * Compatible with JDK 1.4.     */    private static final long serialVersionUID = 3053995032091335093L;    /**     * The wrapped collection. Package visible for use by subclasses.     * @serial the real collection     */    final Collection c;    /**     * The object to synchronize on.  When an instance is created via public     * methods, it will be this; but other uses like SynchronizedMap.values()     * must specify another mutex. Package visible for use by subclasses.     * @serial the lock     */    final Object mutex;    /**     * Wrap a given collection.     * @param c the collection to wrap     * @throws NullPointerException if c is null     */    SynchronizedCollection(Collection c)    {      this.c = c;      mutex = this;      if (c == null)        throw new NullPointerException();    }    /**     * Called only by trusted code to specify the mutex as well as the     * collection.     * @param sync the mutex     * @param c the collection     */    SynchronizedCollection(Object sync, Collection c)    {      this.c = c;      mutex = sync;    }    public boolean add(Object o)    {      synchronized (mutex)        {          return c.add(o);        }    }    public boolean addAll(Collection col)    {      synchronized (mutex)        {          return c.addAll(col);        }    }    public void clear()    {      synchronized (mutex)        {          c.clear();        }    }    public boolean contains(Object o)    {      synchronized (mutex)        {          return c.contains(o);        }    }    public boolean containsAll(Collection c1)    {      synchronized (mutex)        {          return c.containsAll(c1);        }    }    public boolean isEmpty()    {      synchronized (mutex)        {          return c.isEmpty();        }    }    public Iterator iterator()    {      synchronized (mutex)        {          return new SynchronizedIterator(mutex, c.iterator());        }    }    public boolean remove(Object o)    {      synchronized (mutex)        {          return c.remove(o);        }    }    public boolean removeAll(Collection col)    {      synchronized (mutex)        {          return c.removeAll(col);        }    }    public boolean retainAll(Collection col)    {      synchronized (mutex)        {          return c.retainAll(col);        }    }    public int size()    {      synchronized (mutex)        {          return c.size();        }    }    public Object[] toArray()    {      synchronized (mutex)        {          return c.toArray();        }    }    public Object[] toArray(Object[] a)    {      synchronized (mutex)        {          return c.toArray(a);        }    }    public String toString()    {      synchronized (mutex)        {          return c.toString();        }    }  } // class SynchronizedCollection  /**   * The implementation of the various iterator methods in the   * synchronized classes. These iterators must "sync" on the same object   * as the collection they iterate over.   *   * @author Eric Blake <ebb9@email.byu.edu>   */  private static class SynchronizedIterator implements Iterator  {    /**     * The object to synchronize on. Package visible for use by subclass.     */    final Object mutex;    /**     * The wrapped iterator.     */    private final Iterator i;    /**     * Only trusted code creates a wrapper, with the specified sync.     * @param sync the mutex     * @param i the wrapped iterator     */    SynchronizedIterator(Object sync, Iterator i)    {      this.i = i;      mutex = sync;    }    public Object next()    {      synchronized (mutex)        {          return i.next();        }    }    public boolean hasNext()    {      synchronized (mutex)        {          return i.hasNext();        }    }    public void remove()    {      synchronized (mutex)        {          i.remove();        }    }  } // class SynchronizedIterator  /**   * Returns a synchronized (thread-safe) list wrapper backed by the   * given list. Notice that element access through the iterators   * is thread-safe, but if the list can be structurally modified   * (adding or removing elements) then you should synchronize around the   * iteration to avoid non-deterministic behavior:<br>   * <pre>   * List l = Collections.synchronizedList(new List(...));   * ...   * synchronized (l)   *   {   *     Iterator i = l.iterator();   *     while (i.hasNext())   *       foo(i.next());   *   }   * </pre><p>   *   * The returned List implements Serializable, but can only be serialized if   * the list it wraps is likewise Serializable. In addition, if the wrapped   * list implements RandomAccess, this does too.   *   * @param l the list to wrap   * @return a synchronized view of the list   * @see Serializable   * @see RandomAccess   */  public static List synchronizedList(List l)  {    if (l instanceof RandomAccess)      return new SynchronizedRandomAccessList(l);    return new SynchronizedList(l);  }  /**   * The implementation of {@link #synchronizedList(List)} for sequential   * lists. This class name is required for compatibility with Sun's JDK   * serializability. Package visible, so that lists such as Vector.subList()   * can specify which object to synchronize on.   *   * @author Eric Blake <ebb9@email.byu.edu>   */  static class SynchronizedList extends SynchronizedCollection    implements List  {    /**     * Compatible with JDK 1.4.     */    private static final long serialVersionUID = -7754090372962971524L;    /**     * The wrapped list; stored both here and in the superclass to avoid     * excessive casting. Package visible for use by subclass.     * @serial the wrapped list     */    final List list;    /**     * Wrap a given list.     * @param l the list to wrap     * @throws NullPointerException if l is null     */    SynchronizedList(List l)    {      super(l);      list = l;    }    /**     * Called only by trusted code to specify the mutex as well as the list.     * @param sync the mutex     * @param l the list     */    SynchronizedList(Object sync, List l)    {      super(sync, l);      list = l;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -