|
1 /* |
|
2 * Copyright (c) 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 * Portions Copyright (c) 2011 IBM Corporation |
|
26 */ |
|
27 |
|
28 /* |
|
29 * @test |
|
30 * @bug 7014637 |
|
31 * @summary EnumSet's iterator.remove() can be resilient to set's modification. |
|
32 * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com> |
|
33 */ |
|
34 |
|
35 import java.util.EnumSet; |
|
36 import java.util.Iterator; |
|
37 import java.util.Set; |
|
38 |
|
39 public class LargeEnumIteratorRemoveResilience { |
|
40 // enum with more than 64 values |
|
41 private static enum LargeEnum { |
|
42 e00, e01, e02, e03, e04, e05, e06, e07, |
|
43 e08, e09, e0A, e0B, e0C, e0D, e0E, e0F, |
|
44 e10, e11, e12, e13, e14, e15, e16, e17, |
|
45 e18, e19, e1A, e1B, e1C, e1D, e1E, e1F, |
|
46 e20, e21, e22, e23, e24, e25, e26, e27, |
|
47 e28, e29, e2A, e2B, e2C, e2D, e2E, e2F, |
|
48 e30, e31, e32, e33, e34, e35, e36, e37, |
|
49 e38, e39, e3A, e3B, e3C, e3D, e3E, e3F, |
|
50 e40, e41, e42, e43, e44, e45, e46, e47, |
|
51 e48, e49, e4A, e4B, e4C, e4D, e4E, e4F, |
|
52 } |
|
53 |
|
54 public static void main(final String[] args) throws Exception { |
|
55 final Set<LargeEnum> set = EnumSet.noneOf(LargeEnum.class); |
|
56 |
|
57 set.add(LargeEnum.e2D); |
|
58 set.add(LargeEnum.e42); |
|
59 |
|
60 final Iterator<LargeEnum> iterator = set.iterator(); |
|
61 |
|
62 int size = set.size(); |
|
63 LargeEnum element = iterator.next(); |
|
64 |
|
65 iterator.remove(); |
|
66 checkSetAfterRemoval(set, size, element); |
|
67 |
|
68 size = set.size(); |
|
69 element = iterator.next(); |
|
70 |
|
71 set.remove(element); |
|
72 checkSetAfterRemoval(set, size, element); |
|
73 |
|
74 // The Java API declares that the behaviour here - to call |
|
75 // iterator.remove() after the underlying collection has been |
|
76 // modified - is "unspecified". |
|
77 // However, in the case of iterators for EnumSet, it is easy to |
|
78 // implement their remove() operation such that the set is |
|
79 // unmodified if it is called for an element that has already been |
|
80 // removed from the set - this being the naturally "resilient" |
|
81 // behaviour. |
|
82 iterator.remove(); |
|
83 checkSetAfterRemoval(set, size, element); |
|
84 } |
|
85 |
|
86 private static void checkSetAfterRemoval(final Set<LargeEnum> set, |
|
87 final int origSize, final LargeEnum removedElement) |
|
88 throws Exception { |
|
89 if (set.size() != (origSize - 1)) { |
|
90 throw new Exception("Test FAILED: Unexpected set size after removal; expected '" + (origSize - 1) + "' but found '" + set.size() + "'"); |
|
91 } |
|
92 if (set.contains(removedElement)) { |
|
93 throw new Exception("Test FAILED: Element returned from iterator unexpectedly still in set after removal."); |
|
94 } |
|
95 } |
|
96 } |