1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/util/LinkedHashSet.java Sat Sep 07 13:51:24 2013 +0200
1.3 @@ -0,0 +1,171 @@
1.4 +/*
1.5 + * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.util;
1.30 +
1.31 +/**
1.32 + * <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
1.33 + * with predictable iteration order. This implementation differs from
1.34 + * <tt>HashSet</tt> in that it maintains a doubly-linked list running through
1.35 + * all of its entries. This linked list defines the iteration ordering,
1.36 + * which is the order in which elements were inserted into the set
1.37 + * (<i>insertion-order</i>). Note that insertion order is <i>not</i> affected
1.38 + * if an element is <i>re-inserted</i> into the set. (An element <tt>e</tt>
1.39 + * is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
1.40 + * <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
1.41 + * the invocation.)
1.42 + *
1.43 + * <p>This implementation spares its clients from the unspecified, generally
1.44 + * chaotic ordering provided by {@link HashSet}, without incurring the
1.45 + * increased cost associated with {@link TreeSet}. It can be used to
1.46 + * produce a copy of a set that has the same order as the original, regardless
1.47 + * of the original set's implementation:
1.48 + * <pre>
1.49 + * void foo(Set s) {
1.50 + * Set copy = new LinkedHashSet(s);
1.51 + * ...
1.52 + * }
1.53 + * </pre>
1.54 + * This technique is particularly useful if a module takes a set on input,
1.55 + * copies it, and later returns results whose order is determined by that of
1.56 + * the copy. (Clients generally appreciate having things returned in the same
1.57 + * order they were presented.)
1.58 + *
1.59 + * <p>This class provides all of the optional <tt>Set</tt> operations, and
1.60 + * permits null elements. Like <tt>HashSet</tt>, it provides constant-time
1.61 + * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
1.62 + * <tt>remove</tt>), assuming the hash function disperses elements
1.63 + * properly among the buckets. Performance is likely to be just slightly
1.64 + * below that of <tt>HashSet</tt>, due to the added expense of maintaining the
1.65 + * linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
1.66 + * requires time proportional to the <i>size</i> of the set, regardless of
1.67 + * its capacity. Iteration over a <tt>HashSet</tt> is likely to be more
1.68 + * expensive, requiring time proportional to its <i>capacity</i>.
1.69 + *
1.70 + * <p>A linked hash set has two parameters that affect its performance:
1.71 + * <i>initial capacity</i> and <i>load factor</i>. They are defined precisely
1.72 + * as for <tt>HashSet</tt>. Note, however, that the penalty for choosing an
1.73 + * excessively high value for initial capacity is less severe for this class
1.74 + * than for <tt>HashSet</tt>, as iteration times for this class are unaffected
1.75 + * by capacity.
1.76 + *
1.77 + * <p><strong>Note that this implementation is not synchronized.</strong>
1.78 + * If multiple threads access a linked hash set concurrently, and at least
1.79 + * one of the threads modifies the set, it <em>must</em> be synchronized
1.80 + * externally. This is typically accomplished by synchronizing on some
1.81 + * object that naturally encapsulates the set.
1.82 + *
1.83 + * If no such object exists, the set should be "wrapped" using the
1.84 + * {@link Collections#synchronizedSet Collections.synchronizedSet}
1.85 + * method. This is best done at creation time, to prevent accidental
1.86 + * unsynchronized access to the set: <pre>
1.87 + * Set s = Collections.synchronizedSet(new LinkedHashSet(...));</pre>
1.88 + *
1.89 + * <p>The iterators returned by this class's <tt>iterator</tt> method are
1.90 + * <em>fail-fast</em>: if the set is modified at any time after the iterator
1.91 + * is created, in any way except through the iterator's own <tt>remove</tt>
1.92 + * method, the iterator will throw a {@link ConcurrentModificationException}.
1.93 + * Thus, in the face of concurrent modification, the iterator fails quickly
1.94 + * and cleanly, rather than risking arbitrary, non-deterministic behavior at
1.95 + * an undetermined time in the future.
1.96 + *
1.97 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
1.98 + * as it is, generally speaking, impossible to make any hard guarantees in the
1.99 + * presence of unsynchronized concurrent modification. Fail-fast iterators
1.100 + * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
1.101 + * Therefore, it would be wrong to write a program that depended on this
1.102 + * exception for its correctness: <i>the fail-fast behavior of iterators
1.103 + * should be used only to detect bugs.</i>
1.104 + *
1.105 + * <p>This class is a member of the
1.106 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
1.107 + * Java Collections Framework</a>.
1.108 + *
1.109 + * @param <E> the type of elements maintained by this set
1.110 + *
1.111 + * @author Josh Bloch
1.112 + * @see Object#hashCode()
1.113 + * @see Collection
1.114 + * @see Set
1.115 + * @see HashSet
1.116 + * @see TreeSet
1.117 + * @see Hashtable
1.118 + * @since 1.4
1.119 + */
1.120 +
1.121 +public class LinkedHashSet<E>
1.122 + extends HashSet<E>
1.123 + implements Set<E>, Cloneable, java.io.Serializable {
1.124 +
1.125 + private static final long serialVersionUID = -2851667679971038690L;
1.126 +
1.127 + /**
1.128 + * Constructs a new, empty linked hash set with the specified initial
1.129 + * capacity and load factor.
1.130 + *
1.131 + * @param initialCapacity the initial capacity of the linked hash set
1.132 + * @param loadFactor the load factor of the linked hash set
1.133 + * @throws IllegalArgumentException if the initial capacity is less
1.134 + * than zero, or if the load factor is nonpositive
1.135 + */
1.136 + public LinkedHashSet(int initialCapacity, float loadFactor) {
1.137 + super(initialCapacity, loadFactor, true);
1.138 + }
1.139 +
1.140 + /**
1.141 + * Constructs a new, empty linked hash set with the specified initial
1.142 + * capacity and the default load factor (0.75).
1.143 + *
1.144 + * @param initialCapacity the initial capacity of the LinkedHashSet
1.145 + * @throws IllegalArgumentException if the initial capacity is less
1.146 + * than zero
1.147 + */
1.148 + public LinkedHashSet(int initialCapacity) {
1.149 + super(initialCapacity, .75f, true);
1.150 + }
1.151 +
1.152 + /**
1.153 + * Constructs a new, empty linked hash set with the default initial
1.154 + * capacity (16) and load factor (0.75).
1.155 + */
1.156 + public LinkedHashSet() {
1.157 + super(16, .75f, true);
1.158 + }
1.159 +
1.160 + /**
1.161 + * Constructs a new linked hash set with the same elements as the
1.162 + * specified collection. The linked hash set is created with an initial
1.163 + * capacity sufficient to hold the elements in the specified collection
1.164 + * and the default load factor (0.75).
1.165 + *
1.166 + * @param c the collection whose elements are to be placed into
1.167 + * this set
1.168 + * @throws NullPointerException if the specified collection is null
1.169 + */
1.170 + public LinkedHashSet(Collection<? extends E> c) {
1.171 + super(Math.max(2*c.size(), 11), .75f, true);
1.172 + addAll(c);
1.173 + }
1.174 +}