| 1 | /* Set.java -- A collection that prohibits duplicates
|
|---|
| 2 | Copyright (C) 1998, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.util;
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * A collection that contains no duplicates. In other words, for two set
|
|---|
| 43 | * elements e1 and e2, <code>e1.equals(e2)</code> returns false. There
|
|---|
| 44 | * are additional stipulations on <code>add</code>, <code>equals</code>
|
|---|
| 45 | * and <code>hashCode</code>, as well as the requirements that constructors
|
|---|
| 46 | * do not permit duplicate elements. The Set interface is incompatible with
|
|---|
| 47 | * List; you cannot implement both simultaneously.
|
|---|
| 48 | * <p>
|
|---|
| 49 | *
|
|---|
| 50 | * Note: Be careful about using mutable objects in sets. In particular,
|
|---|
| 51 | * if a mutable object changes to become equal to another set element, you
|
|---|
| 52 | * have violated the contract. As a special case of this, a Set is not
|
|---|
| 53 | * allowed to be an element of itself, without risking undefined behavior.
|
|---|
| 54 | *
|
|---|
| 55 | * @author Original author unknown
|
|---|
| 56 | * @author Eric Blake <[email protected]>
|
|---|
| 57 | * @see Collection
|
|---|
| 58 | * @see List
|
|---|
| 59 | * @see SortedSet
|
|---|
| 60 | * @see HashSet
|
|---|
| 61 | * @see TreeSet
|
|---|
| 62 | * @see LinkedHashSet
|
|---|
| 63 | * @see AbstractSet
|
|---|
| 64 | * @see Collections#singleton(Object)
|
|---|
| 65 | * @see Collections#EMPTY_SET
|
|---|
| 66 | * @since 1.2
|
|---|
| 67 | * @status updated to 1.4
|
|---|
| 68 | */
|
|---|
| 69 | public interface Set extends Collection
|
|---|
| 70 | {
|
|---|
| 71 | /**
|
|---|
| 72 | * Adds the specified element to the set if it is not already present
|
|---|
| 73 | * (optional operation). In particular, the comparison algorithm is
|
|---|
| 74 | * <code>o == null ? e == null : o.equals(e)</code>. Sets need not permit
|
|---|
| 75 | * all values, and may document what exceptions will be thrown if
|
|---|
| 76 | * a value is not permitted.
|
|---|
| 77 | *
|
|---|
| 78 | * @param o the object to add
|
|---|
| 79 | * @return true if the object was not previously in the set
|
|---|
| 80 | * @throws UnsupportedOperationException if this operation is not allowed
|
|---|
| 81 | * @throws ClassCastException if the class of o prevents it from being added
|
|---|
| 82 | * @throws IllegalArgumentException if some aspect of o prevents it from
|
|---|
| 83 | * being added
|
|---|
| 84 | * @throws NullPointerException if null is not permitted in this set
|
|---|
| 85 | */
|
|---|
| 86 | boolean add(Object o);
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Adds all of the elements of the given collection to this set (optional
|
|---|
| 90 | * operation). If the argument is also a Set, this returns the mathematical
|
|---|
| 91 | * <i>union</i> of the two. The behavior is unspecified if the set is
|
|---|
| 92 | * modified while this is taking place.
|
|---|
| 93 | *
|
|---|
| 94 | * @param c the collection to add
|
|---|
| 95 | * @return true if the set changed as a result
|
|---|
| 96 | * @throws UnsupportedOperationException if this operation is not allowed
|
|---|
| 97 | * @throws ClassCastException if the class of an element prevents it from
|
|---|
| 98 | * being added
|
|---|
| 99 | * @throws IllegalArgumentException if something about an element prevents
|
|---|
| 100 | * it from being added
|
|---|
| 101 | * @throws NullPointerException if null is not permitted in this set, or
|
|---|
| 102 | * if the argument c is null
|
|---|
| 103 | * @see #add(Object)
|
|---|
| 104 | */
|
|---|
| 105 | boolean addAll(Collection c);
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * Removes all elements from this set (optional operation). This set will
|
|---|
| 109 | * be empty afterwords, unless an exception occurs.
|
|---|
| 110 | *
|
|---|
| 111 | * @throws UnsupportedOperationException if this operation is not allowed
|
|---|
| 112 | */
|
|---|
| 113 | void clear();
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Returns true if the set contains the specified element. In other words,
|
|---|
| 117 | * this looks for <code>o == null ? e == null : o.equals(e)</code>.
|
|---|
| 118 | *
|
|---|
| 119 | * @param o the object to look for
|
|---|
| 120 | * @return true if it is found in the set
|
|---|
| 121 | */
|
|---|
| 122 | boolean contains(Object o);
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * Returns true if this set contains all elements in the specified
|
|---|
| 126 | * collection. If the argument is also a set, this is the <i>subset</i>
|
|---|
| 127 | * relationship.
|
|---|
| 128 | *
|
|---|
| 129 | * @param c the collection to check membership in
|
|---|
| 130 | * @return true if all elements in this set are in c
|
|---|
| 131 | * @throws NullPointerException if c is null
|
|---|
| 132 | * @see #contains(Object)
|
|---|
| 133 | */
|
|---|
| 134 | boolean containsAll(Collection c);
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Compares the specified object to this for equality. For sets, the object
|
|---|
| 138 | * must be a set, the two must have the same size, and every element in
|
|---|
| 139 | * one must be in the other.
|
|---|
| 140 | *
|
|---|
| 141 | * @param o the object to compare to
|
|---|
| 142 | * @return true if it is an equal set
|
|---|
| 143 | */
|
|---|
| 144 | boolean equals(Object o);
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Returns the hash code for this set. In order to satisfy the contract of
|
|---|
| 148 | * equals, this is the sum of the hashcode of all elements in the set.
|
|---|
| 149 | *
|
|---|
| 150 | * @return the sum of the hashcodes of all set elements
|
|---|
| 151 | */
|
|---|
| 152 | int hashCode();
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Returns true if the set contains no elements.
|
|---|
| 156 | *
|
|---|
| 157 | * @return true if the set is empty
|
|---|
| 158 | */
|
|---|
| 159 | boolean isEmpty();
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Returns an iterator over the set. The iterator has no specific order,
|
|---|
| 163 | * unless further specified.
|
|---|
| 164 | *
|
|---|
| 165 | * @return a set iterator
|
|---|
| 166 | */
|
|---|
| 167 | Iterator iterator();
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Removes the specified element from this set (optional operation). If
|
|---|
| 171 | * an element e exists, <code>o == null ? e == null : o.equals(e)</code>,
|
|---|
| 172 | * it is removed from the set.
|
|---|
| 173 | *
|
|---|
| 174 | * @param o the object to remove
|
|---|
| 175 | * @return true if the set changed (an object was removed)
|
|---|
| 176 | * @throws UnsupportedOperationException if this operation is not allowed
|
|---|
| 177 | */
|
|---|
| 178 | boolean remove(Object o);
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Removes from this set all elements contained in the specified collection
|
|---|
| 182 | * (optional operation). If the argument is a set, this returns the
|
|---|
| 183 | * <i>asymmetric set difference</i> of the two sets.
|
|---|
| 184 | *
|
|---|
| 185 | * @param c the collection to remove from this set
|
|---|
| 186 | * @return true if this set changed as a result
|
|---|
| 187 | * @throws UnsupportedOperationException if this operation is not allowed
|
|---|
| 188 | * @throws NullPointerException if c is null
|
|---|
| 189 | * @see #remove(Object)
|
|---|
| 190 | */
|
|---|
| 191 | boolean removeAll(Collection c);
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * Retains only the elements in this set that are also in the specified
|
|---|
| 195 | * collection (optional operation). If the argument is also a set, this
|
|---|
| 196 | * performs the <i>intersection</i> of the two sets.
|
|---|
| 197 | *
|
|---|
| 198 | * @param c the collection to keep
|
|---|
| 199 | * @return true if this set was modified
|
|---|
| 200 | * @throws UnsupportedOperationException if this operation is not allowed
|
|---|
| 201 | * @throws NullPointerException if c is null
|
|---|
| 202 | * @see #remove(Object)
|
|---|
| 203 | */
|
|---|
| 204 | boolean retainAll(Collection c);
|
|---|
| 205 |
|
|---|
| 206 | /**
|
|---|
| 207 | * Returns the number of elements in the set. If there are more
|
|---|
| 208 | * than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is
|
|---|
| 209 | * the <i>cardinality</i> of the set.
|
|---|
| 210 | *
|
|---|
| 211 | * @return the number of elements
|
|---|
| 212 | */
|
|---|
| 213 | int size();
|
|---|
| 214 |
|
|---|
| 215 | /**
|
|---|
| 216 | * Returns an array containing the elements of this set. If the set
|
|---|
| 217 | * makes a guarantee about iteration order, the array has the same
|
|---|
| 218 | * order. The array is distinct from the set; modifying one does not
|
|---|
| 219 | * affect the other.
|
|---|
| 220 | *
|
|---|
| 221 | * @return an array of this set's elements
|
|---|
| 222 | * @see #toArray(Object[])
|
|---|
| 223 | */
|
|---|
| 224 | Object[] toArray();
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * Returns an array containing the elements of this set, of the same runtime
|
|---|
| 228 | * type of the argument. If the given set is large enough, it is reused,
|
|---|
| 229 | * and null is inserted in the first unused slot. Otherwise, reflection
|
|---|
| 230 | * is used to build a new array. If the set makes a guarantee about iteration
|
|---|
| 231 | * order, the array has the same order. The array is distinct from the set;
|
|---|
| 232 | * modifying one does not affect the other.
|
|---|
| 233 | *
|
|---|
| 234 | * @param a the array to determine the return type; if it is big enough
|
|---|
| 235 | * it is used and returned
|
|---|
| 236 | * @return an array holding the elements of the set
|
|---|
| 237 | * @throws ArrayStoreException if the runtime type of a is not a supertype
|
|---|
| 238 | * of all elements in the set
|
|---|
| 239 | * @throws NullPointerException if a is null
|
|---|
| 240 | * @see #toArray()
|
|---|
| 241 | */
|
|---|
| 242 | Object[] toArray(Object[] a);
|
|---|
| 243 | }
|
|---|