31 import java.util.*; |
31 import java.util.*; |
32 |
32 |
33 public class EmptyIterator { |
33 public class EmptyIterator { |
34 |
34 |
35 void test(String[] args) throws Throwable { |
35 void test(String[] args) throws Throwable { |
36 testEmptyCollection(Collections.<Object>emptyList()); |
36 testEmptyCollection(emptyList()); |
37 testEmptyCollection(Collections.<Object>emptySet()); |
37 testEmptyCollection(emptySet()); |
38 |
38 |
39 testEmptyMap(Collections.<Object, Object>emptyMap()); |
39 testEmptyMap(emptyMap()); |
40 |
40 |
41 Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>(); |
41 Hashtable<?,?> emptyTable = new Hashtable<>(); |
42 testEmptyEnumeration(emptyTable.keys()); |
42 testEmptyEnumeration(emptyTable.keys()); |
43 testEmptyEnumeration(emptyTable.elements()); |
43 testEmptyEnumeration(emptyTable.elements()); |
44 testEmptyIterator(emptyTable.keySet().iterator()); |
44 testEmptyIterator(emptyTable.keySet().iterator()); |
45 testEmptyIterator(emptyTable.values().iterator()); |
45 testEmptyIterator(emptyTable.values().iterator()); |
46 testEmptyIterator(emptyTable.entrySet().iterator()); |
46 testEmptyIterator(emptyTable.entrySet().iterator()); |
47 |
47 |
48 final Enumeration<EmptyIterator> finalEmptyTyped = |
48 final Enumeration<EmptyIterator> finalEmptyTyped = emptyEnumeration(); |
49 Collections.emptyEnumeration(); |
|
50 testEmptyEnumeration(finalEmptyTyped); |
49 testEmptyEnumeration(finalEmptyTyped); |
51 |
50 |
52 final Enumeration finalEmptyAbstract = |
51 final Enumeration<?> finalEmptyAbstract = emptyEnumeration(); |
53 Collections.emptyEnumeration(); |
|
54 testEmptyEnumeration(finalEmptyAbstract); |
52 testEmptyEnumeration(finalEmptyAbstract); |
55 |
53 |
56 @SuppressWarnings("unchecked") Iterator<?> x = |
54 testEmptyIterator(emptyIterator()); |
57 new sun.tools.java.MethodSet() |
|
58 .lookupName(sun.tools.java.Identifier.lookup("")); |
|
59 testEmptyIterator(x); |
|
60 } |
55 } |
61 |
56 |
62 <T> void testEmptyEnumeration(final Enumeration<T> e) { |
57 void testEmptyEnumeration(final Enumeration<?> e) { |
63 check(e == emptyEnumeration()); |
58 check(e == emptyEnumeration()); |
64 check(! e.hasMoreElements()); |
59 check(!e.hasMoreElements()); |
65 THROWS(NoSuchElementException.class, |
60 THROWS(NoSuchElementException.class, |
66 new F(){void f(){ e.nextElement(); }}); |
61 new F(){void f(){ e.nextElement(); }}); |
67 } |
62 } |
68 |
63 |
69 <T> void testEmptyIterator(final Iterator<T> it) { |
64 void testEmptyIterator(final Iterator<?> it) { |
70 check(it == emptyIterator()); |
65 check(it == emptyIterator()); |
71 check(! it.hasNext()); |
66 check(! it.hasNext()); |
72 THROWS(NoSuchElementException.class, |
67 THROWS(NoSuchElementException.class, |
73 new F(){void f(){ it.next(); }}); |
68 new F(){void f(){ it.next(); }}); |
74 THROWS(IllegalStateException.class, |
69 THROWS(IllegalStateException.class, |
75 new F(){void f(){ it.remove(); }}); |
70 new F(){void f(){ it.remove(); }}); |
76 } |
71 } |
77 |
72 |
78 void testEmptyMap(Map<Object, Object> m) { |
73 void testEmptyMap(Map<?,?> m) { |
79 check(m == emptyMap()); |
74 check(m == emptyMap()); |
80 check(m.entrySet().iterator() == |
75 check(m.entrySet().iterator() == |
81 Collections.<Map.Entry<Object,Object>>emptyIterator()); |
76 Collections.<Map.Entry<?,?>>emptyIterator()); |
82 check(m.values().iterator() == emptyIterator()); |
77 check(m.values().iterator() == emptyIterator()); |
83 check(m.keySet().iterator() == emptyIterator()); |
78 check(m.keySet().iterator() == emptyIterator()); |
84 equal(m, unmodifiableMap(m)); |
79 equal(m, unmodifiableMap(m)); |
85 |
80 |
86 testEmptyCollection(m.keySet()); |
81 testEmptyCollection(m.keySet()); |
87 testEmptyCollection(m.entrySet()); |
82 testEmptyCollection(m.entrySet()); |
88 testEmptyCollection(m.values()); |
83 testEmptyCollection(m.values()); |
89 } |
84 } |
90 |
85 |
91 <E> void testToArray(final Collection<E> c) { |
86 void testToArray(final Collection<?> c) { |
92 Object[] a = c.toArray(); |
87 Object[] a = c.toArray(); |
93 equal(a.length, 0); |
88 equal(a.length, 0); |
94 equal(a.getClass().getComponentType(), Object.class); |
89 equal(a.getClass().getComponentType(), Object.class); |
95 THROWS(NullPointerException.class, |
90 THROWS(NullPointerException.class, |
96 new F(){void f(){ c.toArray((Object[])null); }}); |
91 new F(){void f(){ c.toArray((Object[])null); }}); |