|
| 1 | +/* |
| 2 | + * Copyright (c) 1997, 2013, 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 |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package java.util; |
| 27 | + |
| 28 | +import org.cprover.CProver; |
| 29 | + |
| 30 | +/** |
| 31 | + * This class provides a skeletal implementation of the <tt>Set</tt> |
| 32 | + * interface to minimize the effort required to implement this |
| 33 | + * interface. <p> |
| 34 | + * |
| 35 | + * The process of implementing a set by extending this class is identical |
| 36 | + * to that of implementing a Collection by extending AbstractCollection, |
| 37 | + * except that all of the methods and constructors in subclasses of this |
| 38 | + * class must obey the additional constraints imposed by the <tt>Set</tt> |
| 39 | + * interface (for instance, the add method must not permit addition of |
| 40 | + * multiple instances of an object to a set).<p> |
| 41 | + * |
| 42 | + * Note that this class does not override any of the implementations from |
| 43 | + * the <tt>AbstractCollection</tt> class. It merely adds implementations |
| 44 | + * for <tt>equals</tt> and <tt>hashCode</tt>.<p> |
| 45 | + * |
| 46 | + * This class is a member of the |
| 47 | + * <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
| 48 | + * Java Collections Framework</a>. |
| 49 | + * |
| 50 | + * @param <E> the type of elements maintained by this set |
| 51 | + * |
| 52 | + * @author Josh Bloch |
| 53 | + * @author Neal Gafter |
| 54 | + * @see Collection |
| 55 | + * @see AbstractCollection |
| 56 | + * @see Set |
| 57 | + * @since 1.2 |
| 58 | + */ |
| 59 | + |
| 60 | +public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> { |
| 61 | + /** |
| 62 | + * Sole constructor. (For invocation by subclass constructors, typically |
| 63 | + * implicit.) |
| 64 | + */ |
| 65 | + protected AbstractSet() { |
| 66 | + } |
| 67 | + |
| 68 | + // Comparison and hashing |
| 69 | + |
| 70 | + /** |
| 71 | + * Compares the specified object with this set for equality. Returns |
| 72 | + * <tt>true</tt> if the given object is also a set, the two sets have |
| 73 | + * the same size, and every member of the given set is contained in |
| 74 | + * this set. This ensures that the <tt>equals</tt> method works |
| 75 | + * properly across different implementations of the <tt>Set</tt> |
| 76 | + * interface.<p> |
| 77 | + * |
| 78 | + * This implementation first checks if the specified object is this |
| 79 | + * set; if so it returns <tt>true</tt>. Then, it checks if the |
| 80 | + * specified object is a set whose size is identical to the size of |
| 81 | + * this set; if not, it returns false. If so, it returns |
| 82 | + * <tt>containsAll((Collection) o)</tt>. |
| 83 | + * |
| 84 | + * @param o object to be compared for equality with this set |
| 85 | + * @return <tt>true</tt> if the specified object is equal to this set |
| 86 | + */ |
| 87 | + public boolean equals(Object o) { |
| 88 | + // if (o == this) |
| 89 | + // return true; |
| 90 | + |
| 91 | + // if (!(o instanceof Set)) |
| 92 | + // return false; |
| 93 | + // Collection<?> c = (Collection<?>) o; |
| 94 | + // if (c.size() != size()) |
| 95 | + // return false; |
| 96 | + // try { |
| 97 | + // return containsAll(c); |
| 98 | + // } catch (ClassCastException unused) { |
| 99 | + // return false; |
| 100 | + // } catch (NullPointerException unused) { |
| 101 | + // return false; |
| 102 | + // } |
| 103 | + CProver.notModelled(); |
| 104 | + return CProver.nondetBoolean(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns the hash code value for this set. The hash code of a set is |
| 109 | + * defined to be the sum of the hash codes of the elements in the set, |
| 110 | + * where the hash code of a <tt>null</tt> element is defined to be zero. |
| 111 | + * This ensures that <tt>s1.equals(s2)</tt> implies that |
| 112 | + * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt> |
| 113 | + * and <tt>s2</tt>, as required by the general contract of |
| 114 | + * {@link Object#hashCode}. |
| 115 | + * |
| 116 | + * <p>This implementation iterates over the set, calling the |
| 117 | + * <tt>hashCode</tt> method on each element in the set, and adding up |
| 118 | + * the results. |
| 119 | + * |
| 120 | + * @return the hash code value for this set |
| 121 | + * @see Object#equals(Object) |
| 122 | + * @see Set#equals(Object) |
| 123 | + */ |
| 124 | + public int hashCode() { |
| 125 | + // int h = 0; |
| 126 | + // Iterator<E> i = iterator(); |
| 127 | + // while (i.hasNext()) { |
| 128 | + // E obj = i.next(); |
| 129 | + // if (obj != null) |
| 130 | + // h += obj.hashCode(); |
| 131 | + // } |
| 132 | + // return h; |
| 133 | + return 0; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Removes from this set all of its elements that are contained in the |
| 138 | + * specified collection (optional operation). If the specified |
| 139 | + * collection is also a set, this operation effectively modifies this |
| 140 | + * set so that its value is the <i>asymmetric set difference</i> of |
| 141 | + * the two sets. |
| 142 | + * |
| 143 | + * <p>This implementation determines which is the smaller of this set |
| 144 | + * and the specified collection, by invoking the <tt>size</tt> |
| 145 | + * method on each. If this set has fewer elements, then the |
| 146 | + * implementation iterates over this set, checking each element |
| 147 | + * returned by the iterator in turn to see if it is contained in |
| 148 | + * the specified collection. If it is so contained, it is removed |
| 149 | + * from this set with the iterator's <tt>remove</tt> method. If |
| 150 | + * the specified collection has fewer elements, then the |
| 151 | + * implementation iterates over the specified collection, removing |
| 152 | + * from this set each element returned by the iterator, using this |
| 153 | + * set's <tt>remove</tt> method. |
| 154 | + * |
| 155 | + * <p>Note that this implementation will throw an |
| 156 | + * <tt>UnsupportedOperationException</tt> if the iterator returned by the |
| 157 | + * <tt>iterator</tt> method does not implement the <tt>remove</tt> method. |
| 158 | + * |
| 159 | + * @param c collection containing elements to be removed from this set |
| 160 | + * @return <tt>true</tt> if this set changed as a result of the call |
| 161 | + * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation |
| 162 | + * is not supported by this set |
| 163 | + * @throws ClassCastException if the class of an element of this set |
| 164 | + * is incompatible with the specified collection |
| 165 | + * (<a href="Collection.html#optional-restrictions">optional</a>) |
| 166 | + * @throws NullPointerException if this set contains a null element and the |
| 167 | + * specified collection does not permit null elements |
| 168 | + * (<a href="Collection.html#optional-restrictions">optional</a>), |
| 169 | + * or if the specified collection is null |
| 170 | + * @see #remove(Object) |
| 171 | + * @see #contains(Object) |
| 172 | + */ |
| 173 | + public boolean removeAll(Collection<?> c) { |
| 174 | + // Objects.requireNonNull(c); |
| 175 | + // boolean modified = false; |
| 176 | + |
| 177 | + // if (size() > c.size()) { |
| 178 | + // for (Iterator<?> i = c.iterator(); i.hasNext(); ) |
| 179 | + // modified |= remove(i.next()); |
| 180 | + // } else { |
| 181 | + // for (Iterator<?> i = iterator(); i.hasNext(); ) { |
| 182 | + // if (c.contains(i.next())) { |
| 183 | + // i.remove(); |
| 184 | + // modified = true; |
| 185 | + // } |
| 186 | + // } |
| 187 | + // } |
| 188 | + // return modified; |
| 189 | + CProver.notModelled(); |
| 190 | + return CProver.nondetBoolean(); |
| 191 | + } |
| 192 | + |
| 193 | + /* |
| 194 | + * @diffblue.noSupport |
| 195 | + * This method cannot be modelled because the iterator |
| 196 | + * cannot be guaranteed to be return the elements in the same |
| 197 | + * order from models-library as in the jdk. |
| 198 | + * It is added here to override AbstractCollection as a safeguard so |
| 199 | + * that if in the future toString() is implemented it more generally |
| 200 | + * in AbstractCollection, this will not allow Sets to use that |
| 201 | + * method, which would generate traces that do not pass because |
| 202 | + * equality between the returned Strings cannot be asserted. |
| 203 | + */ |
| 204 | + @Override |
| 205 | + public String toString() { |
| 206 | + CProver.notModelled(); |
| 207 | + return CProver.nondetWithoutNullForNotModelled(); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments