1 /* 2 * Copyright (c) 2005, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464 27 * 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753 28 * 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215 29 * 4802647 7123424 30 * @summary Run many tests on many Collection and Map implementations 31 * @author Martin Buchholz 32 * @run main MOAT 33 * @run main/othervm -XX:+AggressiveOpts MOAT 34 */ 35 36 /* Mother Of All (Collection) Tests 37 * 38 * Testing of collection classes is often spotty, because many tests 39 * need to be performed on many implementations, but the onus on 40 * writing the tests falls on the engineer introducing the new 41 * implementation. 42 * 43 * The idea of this mega-test is that: 44 * 45 * An engineer adding a new collection implementation could simply add 46 * their new implementation to a list of implementations in this 47 * test's main method. Any general purpose Collection<Integer> or 48 * Map<Integer,Integer> class is appropriate. 49 * 50 * An engineer fixing a regression could add their regression test here and 51 * simultaneously test all other implementations. 52 */ 53 54 import java.io.*; 55 import java.util.*; 56 import java.util.concurrent.*; 57 import static java.util.Collections.*; 58 59 public class MOAT { 60 public static void realMain(String[] args) { 61 62 testCollection(new NewAbstractCollection<Integer>()); 63 testCollection(new NewAbstractSet<Integer>()); 64 testCollection(new LinkedHashSet<Integer>()); 65 testCollection(new HashSet<Integer>()); 66 testCollection(new Vector<Integer>()); 67 testCollection(new Vector<Integer>().subList(0,0)); 68 testCollection(new ArrayDeque<Integer>()); 69 testCollection(new ArrayList<Integer>()); 70 testCollection(new ArrayList<Integer>().subList(0,0)); 71 testCollection(new LinkedList<Integer>()); 72 testCollection(new LinkedList<Integer>().subList(0,0)); 73 testCollection(new TreeSet<Integer>()); 74 testCollection(Collections.checkedList(new ArrayList<Integer>(), Integer.class)); 75 testCollection(Collections.synchronizedList(new ArrayList<Integer>())); 76 testCollection(Collections.checkedSet(new HashSet<Integer>(), Integer.class)); 77 testCollection(Collections.checkedSortedSet(new TreeSet<Integer>(), Integer.class)); 78 testCollection(Collections.checkedNavigableSet(new TreeSet<Integer>(), Integer.class)); 79 testCollection(Collections.synchronizedSet(new HashSet<Integer>())); 80 testCollection(Collections.synchronizedSortedSet(new TreeSet<Integer>())); 81 testCollection(Collections.synchronizedNavigableSet(new TreeSet<Integer>())); 82 83 testCollection(new CopyOnWriteArrayList<Integer>()); 84 testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0)); 85 testCollection(new CopyOnWriteArraySet<Integer>()); 86 testCollection(new PriorityQueue<Integer>()); 87 testCollection(new PriorityBlockingQueue<Integer>()); 88 testCollection(new ArrayBlockingQueue<Integer>(20)); 89 testCollection(new LinkedBlockingQueue<Integer>(20)); 90 testCollection(new LinkedBlockingDeque<Integer>(20)); 91 testCollection(new ConcurrentLinkedDeque<Integer>()); 92 testCollection(new ConcurrentLinkedQueue<Integer>()); 93 testCollection(new LinkedTransferQueue<Integer>()); 94 testCollection(new ConcurrentSkipListSet<Integer>()); 95 testCollection(Arrays.asList(new Integer(42))); 96 testCollection(Arrays.asList(1,2,3)); 97 testCollection(nCopies(25,1)); 98 testImmutableList(nCopies(25,1)); 99 testImmutableList(unmodifiableList(Arrays.asList(1,2,3))); 100 101 testMap(new HashMap<Integer,Integer>()); 102 testMap(new LinkedHashMap<Integer,Integer>()); 103 testMap(new WeakHashMap<Integer,Integer>()); 104 testMap(new IdentityHashMap<Integer,Integer>()); 105 testMap(new TreeMap<Integer,Integer>()); 106 testMap(new Hashtable<Integer,Integer>()); 107 testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f)); 108 testMap(new ConcurrentSkipListMap<Integer,Integer>()); 109 testMap(Collections.checkedMap(new HashMap<Integer,Integer>(), Integer.class, Integer.class)); 110 testMap(Collections.checkedSortedMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class)); 111 testMap(Collections.checkedNavigableMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class)); 112 testMap(Collections.synchronizedMap(new HashMap<Integer,Integer>())); 113 testMap(Collections.synchronizedSortedMap(new TreeMap<Integer,Integer>())); 114 testMap(Collections.synchronizedNavigableMap(new TreeMap<Integer,Integer>())); 115 116 // Empty collections 117 final List<Integer> emptyArray = Arrays.asList(new Integer[]{}); 118 testCollection(emptyArray); 119 testEmptyList(emptyArray); 120 THROWS(IndexOutOfBoundsException.class, 121 new Fun(){void f(){ emptyArray.set(0,1); }}); 122 THROWS(UnsupportedOperationException.class, 123 new Fun(){void f(){ emptyArray.add(0,1); }}); 124 125 List<Integer> noOne = nCopies(0,1); 126 testCollection(noOne); 127 testEmptyList(noOne); 128 testImmutableList(noOne); 129 130 Set<Integer> emptySet = emptySet(); 131 testCollection(emptySet); 132 testEmptySet(emptySet); 133 testEmptySet(EMPTY_SET); 134 testEmptySet(Collections.emptySet()); 135 testEmptySet(Collections.emptySortedSet()); 136 testEmptySet(Collections.emptyNavigableSet()); 137 testImmutableSet(emptySet); 138 139 List<Integer> emptyList = emptyList(); 140 testCollection(emptyList); 141 testEmptyList(emptyList); 142 testEmptyList(EMPTY_LIST); 143 testEmptyList(Collections.emptyList()); 144 testImmutableList(emptyList); 145 146 Map<Integer,Integer> emptyMap = emptyMap(); 147 testMap(emptyMap); 148 testEmptyMap(emptyMap); 149 testEmptyMap(EMPTY_MAP); 150 testEmptyMap(Collections.emptyMap()); 151 testEmptyMap(Collections.emptySortedMap()); 152 testEmptyMap(Collections.emptyNavigableMap()); 153 testImmutableMap(emptyMap); 154 testImmutableMap(Collections.emptyMap()); 155 testImmutableMap(Collections.emptySortedMap()); 156 testImmutableMap(Collections.emptyNavigableMap()); 157 158 // Singleton collections 159 Set<Integer> singletonSet = singleton(1); 160 equal(singletonSet.size(), 1); 161 testCollection(singletonSet); 162 testImmutableSet(singletonSet); 163 164 List<Integer> singletonList = singletonList(1); 165 equal(singletonList.size(), 1); 166 testCollection(singletonList); 167 testImmutableList(singletonList); 168 testImmutableList(singletonList.subList(0,1)); 169 testImmutableList(singletonList.subList(0,1).subList(0,1)); 170 testEmptyList(singletonList.subList(0,0)); 171 testEmptyList(singletonList.subList(0,0).subList(0,0)); 172 173 Map<Integer,Integer> singletonMap = singletonMap(1,2); 174 equal(singletonMap.size(), 1); 175 testMap(singletonMap); 176 testImmutableMap(singletonMap); 177 } 178 179 private static void checkContainsSelf(Collection<Integer> c) { 180 check(c.containsAll(c)); 181 check(c.containsAll(Arrays.asList(c.toArray()))); 182 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0])))); 183 } 184 185 private static void checkContainsEmpty(Collection<Integer> c) { 186 check(c.containsAll(new ArrayList<Integer>())); 187 } 188 189 private static <T> void testEmptyCollection(Collection<T> c) { 190 check(c.isEmpty()); 191 equal(c.size(), 0); 192 equal(c.toString(),"[]"); 193 equal(c.toArray().length, 0); 194 equal(c.toArray(new Object[0]).length, 0); 195 check(c.toArray(new Object[]{42})[0] == null); 196 197 Object[] a = new Object[1]; a[0] = Boolean.TRUE; 198 equal(c.toArray(a), a); 199 equal(a[0], null); 200 testEmptyIterator(c.iterator()); 201 } 202 203 static <T> void testEmptyIterator(final Iterator<T> it) { 204 if (rnd.nextBoolean()) 205 check(! it.hasNext()); 206 207 THROWS(NoSuchElementException.class, 208 new Fun(){void f(){ it.next(); }}); 209 210 try { it.remove(); } 211 catch (IllegalStateException _) { pass(); } 212 catch (UnsupportedOperationException _) { pass(); } 213 catch (Throwable t) { unexpected(t); } 214 215 if (rnd.nextBoolean()) 216 check(! it.hasNext()); 217 } 218 219 private static void testEmptyList(List<?> c) { 220 testEmptyCollection(c); 221 equal(c.hashCode(), 1); 222 equal2(c, Collections.<Integer>emptyList()); 223 } 224 225 private static <T> void testEmptySet(Set<T> c) { 226 testEmptyCollection(c); 227 equal(c.hashCode(), 0); 228 equal2(c, Collections.<Integer>emptySet()); 229 if (c instanceof NavigableSet<?>) 230 testEmptyIterator(((NavigableSet<T>)c).descendingIterator()); 231 } 232 233 private static void testImmutableCollection(final Collection<Integer> c) { 234 THROWS(UnsupportedOperationException.class, 235 new Fun(){void f(){ c.add(99); }}, 236 new Fun(){void f(){ c.addAll(singleton(99)); }}); 237 if (! c.isEmpty()) { 238 final Integer first = c.iterator().next(); 239 THROWS(UnsupportedOperationException.class, 240 new Fun(){void f(){ c.clear(); }}, 241 new Fun(){void f(){ c.remove(first); }}, 242 new Fun(){void f(){ c.removeAll(singleton(first)); }}, 243 new Fun(){void f(){ c.retainAll(emptyList()); }} 244 ); 245 } 246 } 247 248 private static void testImmutableSet(final Set<Integer> c) { 249 testImmutableCollection(c); 250 } 251 252 private static void testImmutableList(final List<Integer> c) { 253 testList(c); 254 testImmutableCollection(c); 255 THROWS(UnsupportedOperationException.class, 256 new Fun(){void f(){ c.set(0,42); }}, 257 new Fun(){void f(){ c.add(0,42); }}, 258 new Fun(){void f(){ c.addAll(0,singleton(86)); }}); 259 if (! c.isEmpty()) 260 THROWS(UnsupportedOperationException.class, 261 new Fun(){void f(){ 262 Iterator<Integer> it = c.iterator(); 263 it.next(); it.remove();}}, 264 new Fun(){void f(){ 265 ListIterator<Integer> it = c.listIterator(); 266 it.next(); it.remove();}}); 267 } 268 269 private static void clear(Collection<Integer> c) { 270 try { c.clear(); } 271 catch (Throwable t) { unexpected(t); } 272 testEmptyCollection(c); 273 } 274 275 private static <K,V> void testEmptyMap(final Map<K,V> m) { 276 check(m.isEmpty()); 277 equal(m.size(), 0); 278 equal(m.toString(),"{}"); 279 testEmptySet(m.keySet()); 280 testEmptySet(m.entrySet()); 281 testEmptyCollection(m.values()); 282 283 try { check(! m.containsValue(null)); } 284 catch (NullPointerException _) { /* OK */ } 285 try { check(! m.containsKey(null)); } 286 catch (NullPointerException _) { /* OK */ } 287 check(! m.containsValue(1)); 288 check(! m.containsKey(1)); 289 } 290 291 private static void testImmutableMap(final Map<Integer,Integer> m) { 292 THROWS(UnsupportedOperationException.class, 293 new Fun(){void f(){ m.put(1,1); }}, 294 new Fun(){void f(){ m.putAll(singletonMap(1,1)); }}); 295 if (! m.isEmpty()) { 296 final Integer first = m.keySet().iterator().next(); 297 THROWS(UnsupportedOperationException.class, 298 new Fun(){void f(){ m.remove(first); }}, 299 new Fun(){void f(){ m.clear(); }}); 300 final Map.Entry<Integer,Integer> me 301 = m.entrySet().iterator().next(); 302 Integer key = me.getKey(); 303 Integer val = me.getValue(); 304 THROWS(UnsupportedOperationException.class, 305 new Fun(){void f(){ me.setValue(3); }}); 306 equal(key, me.getKey()); 307 equal(val, me.getValue()); 308 } 309 testImmutableSet(m.keySet()); 310 testImmutableCollection(m.values()); 311 //testImmutableSet(m.entrySet()); 312 } 313 314 private static void clear(Map<?,?> m) { 315 try { m.clear(); } 316 catch (Throwable t) { unexpected(t); } 317 testEmptyMap(m); 318 } 319 320 private static void oneElement(Collection<Integer> c) { 321 clear(c); 322 try { 323 check(c.add(-42)); 324 equal(c.toString(), "[-42]"); 325 if (c instanceof Set) check(! c.add(-42)); 326 } catch (Throwable t) { unexpected(t); } 327 check(! c.isEmpty()); check(c.size() == 1); 328 } 329 330 private static boolean supportsAdd(Collection<Integer> c) { 331 try { check(c.add(778347983)); } 332 catch (UnsupportedOperationException t) { return false; } 333 catch (Throwable t) { unexpected(t); } 334 335 try { 336 check(c.contains(778347983)); 337 check(c.remove(778347983)); 338 } catch (Throwable t) { unexpected(t); } 339 return true; 340 } 341 342 private static boolean supportsRemove(Collection<Integer> c) { 343 try { check(! c.remove(19134032)); } 344 catch (UnsupportedOperationException t) { return false; } 345 catch (Throwable t) { unexpected(t); } 346 return true; 347 } 348 349 private static void checkFunctionalInvariants(Collection<Integer> c) { 350 try { 351 checkContainsSelf(c); 352 checkContainsEmpty(c); 353 check(c.size() != 0 ^ c.isEmpty()); 354 355 { 356 int size = 0; 357 for (Integer i : c) size++; 358 check(c.size() == size); 359 } 360 361 check(c.toArray().length == c.size()); 362 check(c.toArray().getClass() == Object[].class 363 || 364 // !!!! 365 // 6260652: (coll) Arrays.asList(x).toArray().getClass() 366 // should be Object[].class 367 (c.getClass().getName().equals("java.util.Arrays$ArrayList")) 368 ); 369 for (int size : new int[]{0,1,c.size(), c.size()+1}) { 370 Integer[] a = c.toArray(new Integer[size]); 371 check((size > c.size()) || a.length == c.size()); 372 int i = 0; for (Integer j : c) check(a[i++] == j); 373 check((size <= c.size()) || (a[c.size()] == null)); 374 check(a.getClass() == Integer[].class); 375 } 376 377 check(c.equals(c)); 378 if (c instanceof Serializable) { 379 //System.out.printf("Serializing %s%n", c.getClass().getName()); 380 try { 381 Object clone = serialClone(c); 382 equal(c instanceof Serializable, 383 clone instanceof Serializable); 384 equal(c instanceof RandomAccess, 385 clone instanceof RandomAccess); 386 if ((c instanceof List) || (c instanceof Set)) 387 equal(c, clone); 388 else 389 equal(new HashSet<Integer>(c), 390 new HashSet<Integer>(serialClone(c))); 391 } catch (Error xxx) { 392 if (! (xxx.getCause() instanceof NotSerializableException)) 393 throw xxx; 394 } 395 } 396 } 397 catch (Throwable t) { unexpected(t); } 398 } 399 400 //---------------------------------------------------------------- 401 // If add(null) succeeds, contains(null) & remove(null) should succeed 402 //---------------------------------------------------------------- 403 private static void testNullElement(Collection<Integer> c) { 404 405 try { 406 check(c.add(null)); 407 if (c.size() == 1) 408 equal(c.toString(), "[null]"); 409 try { 410 checkFunctionalInvariants(c); 411 check(c.contains(null)); 412 check(c.remove(null)); 413 } 414 catch (Throwable t) { unexpected(t); } 415 } 416 catch (NullPointerException e) { /* OK */ } 417 catch (Throwable t) { unexpected(t); } 418 } 419 420 421 //---------------------------------------------------------------- 422 // If add("x") succeeds, contains("x") & remove("x") should succeed 423 //---------------------------------------------------------------- 424 @SuppressWarnings("unchecked") 425 private static void testStringElement(Collection<Integer> c) { 426 Collection x = (Collection)c; // Make type-unsafe 427 try { 428 check(x.add("x")); 429 try { 430 check(x.contains("x")); 431 check(x.remove("x")); 432 } catch (Throwable t) { unexpected(t); } 433 } 434 catch (ClassCastException e) { /* OK */ } 435 catch (Throwable t) { unexpected(t); } 436 } 437 438 private static void testConcurrentCollection(Collection<Integer> c) { 439 try { 440 c.add(1); 441 Iterator<Integer> it = c.iterator(); 442 check(it.hasNext()); 443 clear(c); 444 check(it.next() instanceof Integer); // No CME 445 check(c.isEmpty()); 446 } 447 catch (Throwable t) { unexpected(t); } 448 } 449 450 private static void testQueue(Queue<Integer> q) { 451 q.clear(); 452 for (int i = 0; i < 5; i++) { 453 testQueueAddRemove(q, null); 454 testQueueAddRemove(q, 537); 455 q.add(i); 456 } 457 equal(q.size(), 5); 458 checkFunctionalInvariants(q); 459 q.poll(); 460 equal(q.size(), 4); 461 checkFunctionalInvariants(q); 462 if ((q instanceof LinkedBlockingQueue) || 463 (q instanceof LinkedBlockingDeque) || 464 (q instanceof ConcurrentLinkedDeque) || 465 (q instanceof ConcurrentLinkedQueue)) { 466 testQueueIteratorRemove(q); 467 } 468 } 469 470 private static void testQueueAddRemove(final Queue<Integer> q, 471 final Integer e) { 472 final List<Integer> originalContents = new ArrayList<Integer>(q); 473 final boolean isEmpty = q.isEmpty(); 474 final boolean isList = (q instanceof List); 475 final List asList = isList ? (List) q : null; 476 check(!q.contains(e)); 477 try { 478 q.add(e); 479 } catch (NullPointerException npe) { 480 check(e == null); 481 return; // Null elements not supported 482 } 483 check(q.contains(e)); 484 check(q.remove(e)); 485 check(!q.contains(e)); 486 equal(new ArrayList<Integer>(q), originalContents); 487 488 if (q instanceof Deque<?>) { 489 final Deque<Integer> deq = (Deque<Integer>) q; 490 final List<Integer> singleton = Collections.singletonList(e); 491 492 // insert, query, remove element at head 493 if (isEmpty) { 494 THROWS(NoSuchElementException.class, 495 new Fun(){void f(){ deq.getFirst(); }}, 496 new Fun(){void f(){ deq.element(); }}, 497 new Fun(){void f(){ deq.iterator().next(); }}); 498 check(deq.peekFirst() == null); 499 check(deq.peek() == null); 500 } else { 501 check(deq.getFirst() != e); 502 check(deq.element() != e); 503 check(deq.iterator().next() != e); 504 check(deq.peekFirst() != e); 505 check(deq.peek() != e); 506 } 507 check(!deq.contains(e)); 508 check(!deq.removeFirstOccurrence(e)); 509 check(!deq.removeLastOccurrence(e)); 510 if (isList) { 511 check(asList.indexOf(e) == -1); 512 check(asList.lastIndexOf(e) == -1); 513 } 514 switch (rnd.nextInt(isList ? 4 : 3)) { 515 case 0: deq.addFirst(e); break; 516 case 1: check(deq.offerFirst(e)); break; 517 case 2: deq.push(e); break; 518 case 3: asList.add(0, e); break; 519 default: throw new AssertionError(); 520 } 521 check(deq.peekFirst() == e); 522 check(deq.getFirst() == e); 523 check(deq.element() == e); 524 check(deq.peek() == e); 525 check(deq.iterator().next() == e); 526 check(deq.contains(e)); 527 if (isList) { 528 check(asList.get(0) == e); 529 check(asList.indexOf(e) == 0); 530 check(asList.lastIndexOf(e) == 0); 531 check(asList.subList(0, 1).equals(singleton)); 532 } 533 switch (rnd.nextInt(isList ? 11 : 9)) { 534 case 0: check(deq.pollFirst() == e); break; 535 case 1: check(deq.removeFirst() == e); break; 536 case 2: check(deq.remove() == e); break; 537 case 3: check(deq.pop() == e); break; 538 case 4: check(deq.removeFirstOccurrence(e)); break; 539 case 5: check(deq.removeLastOccurrence(e)); break; 540 case 6: check(deq.remove(e)); break; 541 case 7: check(deq.removeAll(singleton)); break; 542 case 8: Iterator it = deq.iterator(); it.next(); it.remove(); break; 543 case 9: asList.remove(0); break; 544 case 10: asList.subList(0, 1).clear(); break; 545 default: throw new AssertionError(); 546 } 547 if (isEmpty) { 548 THROWS(NoSuchElementException.class, 549 new Fun(){void f(){ deq.getFirst(); }}, 550 new Fun(){void f(){ deq.element(); }}, 551 new Fun(){void f(){ deq.iterator().next(); }}); 552 check(deq.peekFirst() == null); 553 check(deq.peek() == null); 554 } else { 555 check(deq.getFirst() != e); 556 check(deq.element() != e); 557 check(deq.iterator().next() != e); 558 check(deq.peekFirst() != e); 559 check(deq.peek() != e); 560 } 561 check(!deq.contains(e)); 562 check(!deq.removeFirstOccurrence(e)); 563 check(!deq.removeLastOccurrence(e)); 564 if (isList) { 565 check(isEmpty || asList.get(0) != e); 566 check(asList.indexOf(e) == -1); 567 check(asList.lastIndexOf(e) == -1); 568 } 569 equal(new ArrayList<Integer>(deq), originalContents); 570 571 // insert, query, remove element at tail 572 if (isEmpty) { 573 check(deq.peekLast() == null); 574 THROWS(NoSuchElementException.class, 575 new Fun(){void f(){ deq.getLast(); }}); 576 } else { 577 check(deq.peekLast() != e); 578 check(deq.getLast() != e); 579 } 580 switch (rnd.nextInt(isList ? 6 : 4)) { 581 case 0: deq.addLast(e); break; 582 case 1: check(deq.offerLast(e)); break; 583 case 2: check(deq.add(e)); break; 584 case 3: deq.addAll(singleton); break; 585 case 4: asList.addAll(deq.size(), singleton); break; 586 case 5: asList.add(deq.size(), e); break; 587 default: throw new AssertionError(); 588 } 589 check(deq.peekLast() == e); 590 check(deq.getLast() == e); 591 check(deq.contains(e)); 592 if (isList) { 593 ListIterator it = asList.listIterator(asList.size()); 594 check(it.previous() == e); 595 check(asList.get(asList.size() - 1) == e); 596 check(asList.indexOf(e) == asList.size() - 1); 597 check(asList.lastIndexOf(e) == asList.size() - 1); 598 int size = asList.size(); 599 check(asList.subList(size - 1, size).equals(singleton)); 600 } 601 switch (rnd.nextInt(isList ? 8 : 6)) { 602 case 0: check(deq.pollLast() == e); break; 603 case 1: check(deq.removeLast() == e); break; 604 case 2: check(deq.removeFirstOccurrence(e)); break; 605 case 3: check(deq.removeLastOccurrence(e)); break; 606 case 4: check(deq.remove(e)); break; 607 case 5: check(deq.removeAll(singleton)); break; 608 case 6: asList.remove(asList.size() - 1); break; 609 case 7: 610 ListIterator it = asList.listIterator(asList.size()); 611 it.previous(); 612 it.remove(); 613 break; 614 default: throw new AssertionError(); 615 } 616 if (isEmpty) { 617 check(deq.peekLast() == null); 618 THROWS(NoSuchElementException.class, 619 new Fun(){void f(){ deq.getLast(); }}); 620 } else { 621 check(deq.peekLast() != e); 622 check(deq.getLast() != e); 623 } 624 check(!deq.contains(e)); 625 equal(new ArrayList<Integer>(deq), originalContents); 626 627 // Test operations on empty deque 628 switch (rnd.nextInt(isList ? 4 : 2)) { 629 case 0: deq.clear(); break; 630 case 1: 631 Iterator it = deq.iterator(); 632 while (it.hasNext()) { 633 it.next(); 634 it.remove(); 635 } 636 break; 637 case 2: asList.subList(0, asList.size()).clear(); break; 638 case 3: 639 ListIterator lit = asList.listIterator(asList.size()); 640 while (lit.hasPrevious()) { 641 lit.previous(); 642 lit.remove(); 643 } 644 break; 645 default: throw new AssertionError(); 646 } 647 testEmptyCollection(deq); 648 check(!deq.iterator().hasNext()); 649 if (isList) { 650 check(!asList.listIterator().hasPrevious()); 651 THROWS(NoSuchElementException.class, 652 new Fun(){void f(){ asList.listIterator().previous(); }}); 653 } 654 THROWS(NoSuchElementException.class, 655 new Fun(){void f(){ deq.iterator().next(); }}, 656 new Fun(){void f(){ deq.element(); }}, 657 new Fun(){void f(){ deq.getFirst(); }}, 658 new Fun(){void f(){ deq.getLast(); }}, 659 new Fun(){void f(){ deq.pop(); }}, 660 new Fun(){void f(){ deq.remove(); }}, 661 new Fun(){void f(){ deq.removeFirst(); }}, 662 new Fun(){void f(){ deq.removeLast(); }}); 663 664 check(deq.poll() == null); 665 check(deq.pollFirst() == null); 666 check(deq.pollLast() == null); 667 check(deq.peek() == null); 668 check(deq.peekFirst() == null); 669 check(deq.peekLast() == null); 670 check(!deq.removeFirstOccurrence(e)); 671 check(!deq.removeLastOccurrence(e)); 672 673 check(deq.addAll(originalContents) == !isEmpty); 674 equal(new ArrayList<Integer>(deq), originalContents); 675 check(!deq.addAll(Collections.<Integer>emptyList())); 676 equal(new ArrayList<Integer>(deq), originalContents); 677 } 678 } 679 680 private static void testQueueIteratorRemove(Queue<Integer> q) { 681 System.err.printf("testQueueIteratorRemove %s%n", 682 q.getClass().getSimpleName()); 683 q.clear(); 684 for (int i = 0; i < 5; i++) 685 q.add(i); 686 Iterator<Integer> it = q.iterator(); 687 check(it.hasNext()); 688 for (int i = 3; i >= 0; i--) 689 q.remove(i); 690 equal(it.next(), 0); 691 equal(it.next(), 4); 692 693 q.clear(); 694 for (int i = 0; i < 5; i++) 695 q.add(i); 696 it = q.iterator(); 697 equal(it.next(), 0); 698 check(it.hasNext()); 699 for (int i = 1; i < 4; i++) 700 q.remove(i); 701 equal(it.next(), 1); 702 equal(it.next(), 4); 703 } 704 705 private static void testList(final List<Integer> l) { 706 //---------------------------------------------------------------- 707 // 4802633: (coll) AbstractList.addAll(-1,emptyCollection) 708 // doesn't throw IndexOutOfBoundsException 709 //---------------------------------------------------------------- 710 try { 711 l.addAll(-1, Collections.<Integer>emptyList()); 712 fail("Expected IndexOutOfBoundsException not thrown"); 713 } 714 catch (UnsupportedOperationException _) {/* OK */} 715 catch (IndexOutOfBoundsException _) {/* OK */} 716 catch (Throwable t) { unexpected(t); } 717 718 // equal(l instanceof Serializable, 719 // l.subList(0,0) instanceof Serializable); 720 if (l.subList(0,0) instanceof Serializable) 721 check(l instanceof Serializable); 722 723 equal(l instanceof RandomAccess, 724 l.subList(0,0) instanceof RandomAccess); 725 } 726 727 private static void testCollection(Collection<Integer> c) { 728 try { testCollection1(c); } 729 catch (Throwable t) { unexpected(t); } 730 } 731 732 private static void testCollection1(Collection<Integer> c) { 733 734 System.out.println("\n==> " + c.getClass().getName()); 735 736 checkFunctionalInvariants(c); 737 738 if (! supportsAdd(c)) return; 739 //System.out.println("add() supported"); 740 741 if (c instanceof NavigableSet) { 742 System.out.println("NavigableSet tests..."); 743 744 NavigableSet<Integer> ns = (NavigableSet<Integer>)c; 745 testNavigableSet(ns); 746 testNavigableSet(ns.headSet(6, false)); 747 testNavigableSet(ns.headSet(5, true)); 748 testNavigableSet(ns.tailSet(0, false)); 749 testNavigableSet(ns.tailSet(1, true)); 750 testNavigableSet(ns.subSet(0, false, 5, true)); 751 testNavigableSet(ns.subSet(1, true, 6, false)); 752 } 753 754 if (c instanceof Queue) 755 testQueue((Queue<Integer>)c); 756 757 if (c instanceof List) 758 testList((List<Integer>)c); 759 760 check(supportsRemove(c)); 761 762 try { 763 oneElement(c); 764 checkFunctionalInvariants(c); 765 } 766 catch (Throwable t) { unexpected(t); } 767 768 clear(c); testNullElement(c); 769 oneElement(c); testNullElement(c); 770 771 clear(c); testStringElement(c); 772 oneElement(c); testStringElement(c); 773 774 if (c.getClass().getName().matches(".*concurrent.*")) 775 testConcurrentCollection(c); 776 777 //---------------------------------------------------------------- 778 // The "all" operations should throw NPE when passed null 779 //---------------------------------------------------------------- 780 { 781 clear(c); 782 try { 783 c.removeAll(null); 784 fail("Expected NullPointerException"); 785 } 786 catch (NullPointerException e) { pass(); } 787 catch (Throwable t) { unexpected(t); } 788 789 oneElement(c); 790 try { 791 c.removeAll(null); 792 fail("Expected NullPointerException"); 793 } 794 catch (NullPointerException e) { pass(); } 795 catch (Throwable t) { unexpected(t); } 796 797 clear(c); 798 try { 799 c.retainAll(null); 800 fail("Expected NullPointerException"); 801 } 802 catch (NullPointerException e) { pass(); } 803 catch (Throwable t) { unexpected(t); } 804 805 oneElement(c); 806 try { 807 c.retainAll(null); 808 fail("Expected NullPointerException"); 809 } 810 catch (NullPointerException e) { pass(); } 811 catch (Throwable t) { unexpected(t); } 812 813 oneElement(c); 814 try { 815 c.addAll(null); 816 fail("Expected NullPointerException"); 817 } 818 catch (NullPointerException e) { pass(); } 819 catch (Throwable t) { unexpected(t); } 820 821 oneElement(c); 822 try { 823 c.containsAll(null); 824 fail("Expected NullPointerException"); 825 } 826 catch (NullPointerException e) { pass(); } 827 catch (Throwable t) { unexpected(t); } 828 } 829 } 830 831 //---------------------------------------------------------------- 832 // Map 833 //---------------------------------------------------------------- 834 private static void checkFunctionalInvariants(Map<Integer,Integer> m) { 835 check(m.keySet().size() == m.entrySet().size()); 836 check(m.keySet().size() == m.size()); 837 checkFunctionalInvariants(m.keySet()); 838 checkFunctionalInvariants(m.values()); 839 check(m.size() != 0 ^ m.isEmpty()); 840 } 841 842 private static void testMap(Map<Integer,Integer> m) { 843 System.out.println("\n==> " + m.getClass().getName()); 844 845 if (m instanceof ConcurrentMap) 846 testConcurrentMap((ConcurrentMap<Integer,Integer>) m); 847 848 if (m instanceof NavigableMap) { 849 System.out.println("NavigableMap tests..."); 850 851 NavigableMap<Integer,Integer> nm = 852 (NavigableMap<Integer,Integer>) m; 853 testNavigableMapRemovers(nm); 854 testNavigableMap(nm); 855 testNavigableMap(nm.headMap(6, false)); 856 testNavigableMap(nm.headMap(5, true)); 857 testNavigableMap(nm.tailMap(0, false)); 858 testNavigableMap(nm.tailMap(1, true)); 859 testNavigableMap(nm.subMap(1, true, 6, false)); 860 testNavigableMap(nm.subMap(0, false, 5, true)); 861 } 862 863 checkFunctionalInvariants(m); 864 865 if (supportsClear(m)) { 866 try { clear(m); } 867 catch (Throwable t) { unexpected(t); } 868 } 869 870 if (supportsPut(m)) { 871 try { 872 check(m.put(3333, 77777) == null); 873 check(m.put(9134, 74982) == null); 874 check(m.get(9134) == 74982); 875 check(m.put(9134, 1382) == 74982); 876 check(m.get(9134) == 1382); 877 check(m.size() == 2); 878 checkFunctionalInvariants(m); 879 checkNPEConsistency(m); 880 } 881 catch (Throwable t) { unexpected(t); } 882 } 883 } 884 885 private static boolean supportsPut(Map<Integer,Integer> m) { 886 // We're asking for .equals(...) semantics 887 if (m instanceof IdentityHashMap) return false; 888 889 try { check(m.put(778347983,12735) == null); } 890 catch (UnsupportedOperationException t) { return false; } 891 catch (Throwable t) { unexpected(t); } 892 893 try { 894 check(m.containsKey(778347983)); 895 check(m.remove(778347983) != null); 896 } catch (Throwable t) { unexpected(t); } 897 return true; 898 } 899 900 private static boolean supportsClear(Map<?,?> m) { 901 try { m.clear(); } 902 catch (UnsupportedOperationException t) { return false; } 903 catch (Throwable t) { unexpected(t); } 904 return true; 905 } 906 907 //---------------------------------------------------------------- 908 // ConcurrentMap 909 //---------------------------------------------------------------- 910 private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) { 911 System.out.println("ConcurrentMap tests..."); 912 913 try { 914 clear(m); 915 916 check(m.putIfAbsent(18357,7346) == null); 917 check(m.containsKey(18357)); 918 check(m.putIfAbsent(18357,8263) == 7346); 919 try { m.putIfAbsent(18357,null); fail("NPE"); } 920 catch (NullPointerException t) { } 921 check(m.containsKey(18357)); 922 923 check(! m.replace(18357,8888,7777)); 924 check(m.containsKey(18357)); 925 try { m.replace(18357,null,7777); fail("NPE"); } 926 catch (NullPointerException t) { } 927 check(m.containsKey(18357)); 928 check(m.get(18357) == 7346); 929 check(m.replace(18357,7346,5555)); 930 check(m.replace(18357,5555,7346)); 931 check(m.get(18357) == 7346); 932 933 check(m.replace(92347,7834) == null); 934 try { m.replace(18357,null); fail("NPE"); } 935 catch (NullPointerException t) { } 936 check(m.replace(18357,7346) == 7346); 937 check(m.replace(18357,5555) == 7346); 938 check(m.get(18357) == 5555); 939 check(m.replace(18357,7346) == 5555); 940 check(m.get(18357) == 7346); 941 942 check(! m.remove(18357,9999)); 943 check(m.get(18357) == 7346); 944 check(m.containsKey(18357)); 945 check(! m.remove(18357,null)); // 6272521 946 check(m.get(18357) == 7346); 947 check(m.remove(18357,7346)); 948 check(m.get(18357) == null); 949 check(! m.containsKey(18357)); 950 check(m.isEmpty()); 951 952 m.putIfAbsent(1,2); 953 check(m.size() == 1); 954 check(! m.remove(1,null)); 955 check(! m.remove(1,null)); 956 check(! m.remove(1,1)); 957 check(m.remove(1,2)); 958 check(m.isEmpty()); 959 960 testEmptyMap(m); 961 } 962 catch (Throwable t) { unexpected(t); } 963 } 964 965 private static void throwsConsistently(Class<? extends Throwable> k, 966 Iterable<Fun> fs) { 967 List<Class<? extends Throwable>> threw 968 = new ArrayList<Class<? extends Throwable>>(); 969 for (Fun f : fs) 970 try { f.f(); threw.add(null); } 971 catch (Throwable t) { 972 check(k.isAssignableFrom(t.getClass())); 973 threw.add(t.getClass()); 974 } 975 if (new HashSet<Object>(threw).size() != 1) 976 fail(threw.toString()); 977 } 978 979 private static <T> void checkNPEConsistency(final Map<T,Integer> m) { 980 m.clear(); 981 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap) 982 ? (ConcurrentMap<T,Integer>) m 983 : null; 984 List<Fun> fs = new ArrayList<Fun>(); 985 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}}); 986 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}}); 987 fs.add(new Fun(){void f(){ equal(m.get(null), null);}}); 988 if (cm != null) { 989 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});} 990 throwsConsistently(NullPointerException.class, fs); 991 992 fs.clear(); 993 final Map<T,Integer> sm = singletonMap(null,1); 994 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}}); 995 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}}); 996 if (cm != null) { 997 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}}); 998 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}}); 999 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}}); 1000 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}}); 1001 } 1002 throwsConsistently(NullPointerException.class, fs); 1003 } 1004 1005 //---------------------------------------------------------------- 1006 // NavigableMap 1007 //---------------------------------------------------------------- 1008 private static void 1009 checkNavigableMapKeys(NavigableMap<Integer,Integer> m, 1010 Integer i, 1011 Integer lower, 1012 Integer floor, 1013 Integer ceiling, 1014 Integer higher) { 1015 equal(m.lowerKey(i), lower); 1016 equal(m.floorKey(i), floor); 1017 equal(m.ceilingKey(i), ceiling); 1018 equal(m.higherKey(i), higher); 1019 } 1020 1021 private static void 1022 checkNavigableSetKeys(NavigableSet<Integer> m, 1023 Integer i, 1024 Integer lower, 1025 Integer floor, 1026 Integer ceiling, 1027 Integer higher) { 1028 equal(m.lower(i), lower); 1029 equal(m.floor(i), floor); 1030 equal(m.ceiling(i), ceiling); 1031 equal(m.higher(i), higher); 1032 } 1033 1034 static final Random rnd = new Random(); 1035 static void equalNext(final Iterator<?> it, Object expected) { 1036 if (rnd.nextBoolean()) 1037 check(it.hasNext()); 1038 equal(it.next(), expected); 1039 } 1040 1041 static void equalMaps(Map m1, Map m2) { 1042 equal(m1, m2); 1043 equal(m2, m1); 1044 equal(m1.size(), m2.size()); 1045 equal(m1.isEmpty(), m2.isEmpty()); 1046 equal(m1.toString(), m2.toString()); 1047 check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray())); 1048 } 1049 1050 @SuppressWarnings({"unchecked", "rawtypes"}) 1051 static void testNavigableMapRemovers(NavigableMap m) 1052 { 1053 final Map emptyMap = new HashMap(); 1054 1055 final Map singletonMap = new HashMap(); 1056 singletonMap.put(1, 2); 1057 1058 abstract class NavigableMapView { 1059 abstract NavigableMap view(NavigableMap m); 1060 } 1061 1062 NavigableMapView[] views = { 1063 new NavigableMapView() { NavigableMap view(NavigableMap m) { 1064 return m; }}, 1065 new NavigableMapView() { NavigableMap view(NavigableMap m) { 1066 return m.headMap(99, true); }}, 1067 new NavigableMapView() { NavigableMap view(NavigableMap m) { 1068 return m.tailMap(-99, false); }}, 1069 new NavigableMapView() { NavigableMap view(NavigableMap m) { 1070 return m.subMap(-99, true, 99, false); }}, 1071 }; 1072 1073 abstract class Remover { 1074 abstract void remove(NavigableMap m, Object k, Object v); 1075 } 1076 1077 Remover[] removers = { 1078 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1079 equal(m.remove(k), v); }}, 1080 1081 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1082 equal(m.descendingMap().remove(k), v); }}, 1083 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1084 equal(m.descendingMap().headMap(-86, false).remove(k), v); }}, 1085 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1086 equal(m.descendingMap().tailMap(86, true).remove(k), v); }}, 1087 1088 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1089 equal(m.headMap(86, true).remove(k), v); }}, 1090 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1091 equal(m.tailMap(-86, true).remove(k), v); }}, 1092 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1093 equal(m.subMap(-86, false, 86, true).remove(k), v); }}, 1094 1095 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1096 check(m.keySet().remove(k)); }}, 1097 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1098 check(m.navigableKeySet().remove(k)); }}, 1099 1100 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1101 check(m.navigableKeySet().headSet(86, true).remove(k)); }}, 1102 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1103 check(m.navigableKeySet().tailSet(-86, false).remove(k)); }}, 1104 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1105 check(m.navigableKeySet().subSet(-86, true, 86, false) 1106 .remove(k)); }}, 1107 1108 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1109 check(m.descendingKeySet().headSet(-86, false).remove(k)); }}, 1110 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1111 check(m.descendingKeySet().tailSet(86, true).remove(k)); }}, 1112 new Remover() { void remove(NavigableMap m, Object k, Object v) { 1113 check(m.descendingKeySet().subSet(86, true, -86, false) 1114 .remove(k)); }}, 1115 }; 1116 1117 for (NavigableMapView view : views) { 1118 for (Remover remover : removers) { 1119 try { 1120 m.clear(); 1121 equalMaps(m, emptyMap); 1122 equal(m.put(1, 2), null); 1123 equalMaps(m, singletonMap); 1124 NavigableMap v = view.view(m); 1125 remover.remove(v, 1, 2); 1126 equalMaps(m, emptyMap); 1127 } catch (Throwable t) { unexpected(t); } 1128 } 1129 } 1130 } 1131 1132 private static void testNavigableMap(NavigableMap<Integer,Integer> m) 1133 { 1134 clear(m); 1135 checkNavigableMapKeys(m, 1, null, null, null, null); 1136 1137 equal(m.put(1, 2), null); 1138 equal(m.put(3, 4), null); 1139 equal(m.put(5, 9), null); 1140 1141 equal(m.put(1, 2), 2); 1142 equal(m.put(3, 4), 4); 1143 equal(m.put(5, 6), 9); 1144 1145 checkNavigableMapKeys(m, 0, null, null, 1, 1); 1146 checkNavigableMapKeys(m, 1, null, 1, 1, 3); 1147 checkNavigableMapKeys(m, 2, 1, 1, 3, 3); 1148 checkNavigableMapKeys(m, 3, 1, 3, 3, 5); 1149 checkNavigableMapKeys(m, 5, 3, 5, 5, null); 1150 checkNavigableMapKeys(m, 6, 5, 5, null, null); 1151 1152 for (final Iterator<Integer> it : 1153 (Iterator<Integer>[]) 1154 new Iterator<?>[] { 1155 m.descendingKeySet().iterator(), 1156 m.navigableKeySet().descendingIterator()}) { 1157 equalNext(it, 5); 1158 equalNext(it, 3); 1159 equalNext(it, 1); 1160 check(! it.hasNext()); 1161 THROWS(NoSuchElementException.class, 1162 new Fun(){void f(){it.next();}}); 1163 } 1164 1165 { 1166 final Iterator<Map.Entry<Integer,Integer>> it 1167 = m.descendingMap().entrySet().iterator(); 1168 check(it.hasNext()); equal(it.next().getKey(), 5); 1169 check(it.hasNext()); equal(it.next().getKey(), 3); 1170 check(it.hasNext()); equal(it.next().getKey(), 1); 1171 check(! it.hasNext()); 1172 THROWS(NoSuchElementException.class, 1173 new Fun(){void f(){it.next();}}); 1174 } 1175 } 1176 1177 1178 private static void testNavigableSet(NavigableSet<Integer> s) { 1179 clear(s); 1180 checkNavigableSetKeys(s, 1, null, null, null, null); 1181 1182 check(s.add(1)); 1183 check(s.add(3)); 1184 check(s.add(5)); 1185 1186 check(! s.add(1)); 1187 check(! s.add(3)); 1188 check(! s.add(5)); 1189 1190 checkNavigableSetKeys(s, 0, null, null, 1, 1); 1191 checkNavigableSetKeys(s, 1, null, 1, 1, 3); 1192 checkNavigableSetKeys(s, 2, 1, 1, 3, 3); 1193 checkNavigableSetKeys(s, 3, 1, 3, 3, 5); 1194 checkNavigableSetKeys(s, 5, 3, 5, 5, null); 1195 checkNavigableSetKeys(s, 6, 5, 5, null, null); 1196 1197 for (final Iterator<Integer> it : 1198 (Iterator<Integer>[]) 1199 new Iterator<?>[] { 1200 s.descendingIterator(), 1201 s.descendingSet().iterator()}) { 1202 equalNext(it, 5); 1203 equalNext(it, 3); 1204 equalNext(it, 1); 1205 check(! it.hasNext()); 1206 THROWS(NoSuchElementException.class, 1207 new Fun(){void f(){it.next();}}); 1208 } 1209 } 1210 1211 //--------------------- Infrastructure --------------------------- 1212 static volatile int passed = 0, failed = 0; 1213 static void pass() { passed++; } 1214 static void fail() { failed++; Thread.dumpStack(); } 1215 static void fail(String msg) { System.out.println(msg); fail(); } 1216 static void unexpected(Throwable t) { failed++; t.printStackTrace(); } 1217 static void check(boolean cond) { if (cond) pass(); else fail(); } 1218 static void equal(Object x, Object y) { 1219 if (x == null ? y == null : x.equals(y)) pass(); 1220 else {System.out.println(x + " not equal to " + y); fail();}} 1221 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} 1222 public static void main(String[] args) throws Throwable { 1223 try { realMain(args); } catch (Throwable t) { unexpected(t); } 1224 1225 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 1226 if (failed > 0) throw new Exception("Some tests failed"); 1227 } 1228 private static abstract class Fun {abstract void f() throws Throwable;} 1229 private static void THROWS(Class<? extends Throwable> k, Fun... fs) { 1230 for (Fun f : fs) 1231 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } 1232 catch (Throwable t) { 1233 if (k.isAssignableFrom(t.getClass())) pass(); 1234 else unexpected(t);}} 1235 static byte[] serializedForm(Object obj) { 1236 try { 1237 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1238 new ObjectOutputStream(baos).writeObject(obj); 1239 return baos.toByteArray(); 1240 } catch (IOException e) { throw new Error(e); }} 1241 static Object readObject(byte[] bytes) 1242 throws IOException, ClassNotFoundException { 1243 InputStream is = new ByteArrayInputStream(bytes); 1244 return new ObjectInputStream(is).readObject();} 1245 @SuppressWarnings("unchecked") 1246 static <T> T serialClone(T obj) { 1247 try { return (T) readObject(serializedForm(obj)); } 1248 catch (Exception e) { throw new Error(e); }} 1249 private static class NewAbstractCollection<E> extends AbstractCollection<E> { 1250 ArrayList<E> list = new ArrayList<>(); 1251 public boolean remove(Object obj) { 1252 return list.remove(obj); 1253 } 1254 public boolean add(E e) { 1255 return list.add(e); 1256 } 1257 public Iterator<E> iterator() { 1258 return list.iterator(); 1259 } 1260 public int size() { 1261 return list.size(); 1262 } 1263 } 1264 private static class NewAbstractSet<E> extends AbstractSet<E> { 1265 HashSet<E> set = new HashSet<>(); 1266 public boolean remove(Object obj) { 1267 return set.remove(obj); 1268 } 1269 public boolean add(E e) { 1270 return set.add(e); 1271 } 1272 public Iterator<E> iterator() { 1273 return set.iterator(); 1274 } 1275 public int size() { 1276 return set.size(); 1277 } 1278 } 1279 1280 }