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