Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collections/DelegatingIteratorForEachRemaining.java
41149 views
1
/*
2
* Copyright (c) 2018 Google Inc. 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
* @run junit DelegatingIteratorForEachRemaining
27
*/
28
29
import org.junit.Assert;
30
import org.junit.Test;
31
32
import java.util.Collection;
33
import java.util.Collections;
34
import java.util.HashMap;
35
import java.util.Iterator;
36
import java.util.Map;
37
import java.util.Objects;
38
import java.util.Set;
39
import java.util.Spliterator;
40
import java.util.function.BiConsumer;
41
import java.util.function.BiFunction;
42
import java.util.function.Consumer;
43
import java.util.function.Function;
44
import java.util.function.Predicate;
45
import java.util.stream.Stream;
46
47
public class DelegatingIteratorForEachRemaining {
48
49
static abstract class ForwardingIterator<E> implements Iterator<E> {
50
private final Iterator<E> delegate;
51
52
protected ForwardingIterator(Iterator<E> delegate) {
53
this.delegate = Objects.requireNonNull(delegate);
54
}
55
56
@Override public boolean hasNext() { return delegate.hasNext(); }
57
@Override public E next() { return delegate.next(); }
58
@Override public void remove() { delegate.remove(); }
59
@Override public void forEachRemaining(Consumer<? super E> action) {
60
delegate.forEachRemaining(action);
61
}
62
}
63
64
static final class ThrowingIterator<E> extends ForwardingIterator<E> {
65
public ThrowingIterator(Iterator<E> delegate) {
66
super(delegate);
67
}
68
69
@Override
70
public void forEachRemaining(Consumer<? super E> action) {
71
throw new UnsupportedOperationException();
72
}
73
}
74
75
static abstract class ForwardingSet<E> implements Set<E> {
76
private final Set<E> delegate;
77
78
protected ForwardingSet(Set<E> delegate) {
79
this.delegate = Objects.requireNonNull(delegate);
80
}
81
82
@Override public int size() { return delegate.size(); }
83
@Override public boolean isEmpty() { return delegate.isEmpty(); }
84
@Override public boolean contains(Object o) { return delegate.contains(o); }
85
@Override public Iterator<E> iterator() { return delegate.iterator(); }
86
@Override public Object[] toArray() { return delegate.toArray(); }
87
@Override public <T> T[] toArray( T[] ts) { return delegate.toArray(ts); }
88
@Override public boolean add(E e) { return delegate.add(e); }
89
@Override public boolean remove(Object o) { return delegate.remove(o); }
90
@Override public boolean containsAll( Collection<?> c) { return delegate.containsAll(c); }
91
@Override public boolean addAll( Collection<? extends E> c) { return delegate.addAll(c); }
92
@Override public boolean retainAll( Collection<?> c) { return delegate.retainAll(c); }
93
@Override public boolean removeAll( Collection<?> c) { return delegate.removeAll(c); }
94
@Override public void clear() { delegate.clear(); }
95
@Override public boolean equals(Object o) { return delegate.equals(o); }
96
@Override public int hashCode() { return delegate.hashCode(); }
97
@Override public Spliterator<E> spliterator() { return delegate.spliterator(); }
98
@Override public boolean removeIf(Predicate<? super E> filter) { return delegate.removeIf(filter); }
99
@Override public Stream<E> stream() { return delegate.stream(); }
100
@Override public Stream<E> parallelStream() { return delegate.parallelStream(); }
101
@Override public void forEach(Consumer<? super E> action) { delegate.forEach(action); }
102
}
103
104
static class ThrowingSet<E> extends ForwardingSet<E> {
105
public ThrowingSet(Set<E> delegate) {
106
super(delegate);
107
}
108
109
@Override
110
public ThrowingIterator<E> iterator() {
111
return new ThrowingIterator<>(super.iterator());
112
}
113
}
114
115
static abstract class ForwardingMap<K, V> implements Map<K, V> {
116
private final Map<K, V> delegate;
117
118
public ForwardingMap(Map<K, V> delegate) {
119
this.delegate = delegate;
120
}
121
122
@Override public int size() { return delegate.size(); }
123
@Override public boolean isEmpty() { return delegate.isEmpty(); }
124
@Override public boolean containsKey(Object o) { return delegate.containsKey(o); }
125
@Override public boolean containsValue(Object o) { return delegate.containsValue(o); }
126
@Override public V get(Object o) { return delegate.get(o); }
127
@Override public V put(K k, V v) { return delegate.put(k, v); }
128
@Override public V remove(Object o) { return delegate.remove(o); }
129
@Override public void putAll(Map<? extends K, ? extends V> map) { delegate.putAll(map); }
130
@Override public void clear() { delegate.clear(); }
131
@Override public Set<K> keySet() { return delegate.keySet(); }
132
@Override public Collection<V> values() { return delegate.values(); }
133
@Override public Set<Entry<K, V>> entrySet() { return delegate.entrySet(); }
134
@Override public boolean equals(Object o) { return delegate.equals(o); }
135
@Override public int hashCode() { return delegate.hashCode(); }
136
@Override public V getOrDefault(Object key, V defaultValue) { return delegate.getOrDefault(key, defaultValue); }
137
@Override public void forEach(BiConsumer<? super K, ? super V> action) { delegate.forEach(action); }
138
@Override public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { delegate.replaceAll(function); }
139
@Override public V putIfAbsent(K key, V value) { return delegate.putIfAbsent(key, value); }
140
@Override public boolean remove(Object key, Object value) { return delegate.remove(key, value); }
141
@Override public boolean replace(K key, V oldValue, V newValue) { return delegate.replace(key, oldValue, newValue); }
142
@Override public V replace(K key, V value) { return delegate.replace(key, value); }
143
@Override public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { return delegate.computeIfAbsent(key, mappingFunction); }
144
@Override public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { return delegate.computeIfPresent(key, remappingFunction); }
145
@Override public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { return delegate.compute(key, remappingFunction); }
146
@Override public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { return delegate.merge(key, value, remappingFunction); }
147
}
148
149
static class ThrowingMap<K, V> extends ForwardingMap<K, V> {
150
public ThrowingMap(Map<K, V> delegate) {
151
super(delegate);
152
}
153
154
@Override
155
public ThrowingSet<Entry<K, V>> entrySet() {
156
return new ThrowingSet<>(super.entrySet());
157
}
158
159
@Override
160
public Set<K> keySet() {
161
return new ThrowingSet(super.keySet());
162
}
163
}
164
165
static<E> void assertThrowingIterator(Iterator<E> iterator) {
166
try {
167
iterator.forEachRemaining((entry) -> {});
168
Assert.fail();
169
} catch (UnsupportedOperationException expected) {
170
}
171
}
172
173
private static Map<String, Object> map() {
174
Map<String, Object> map = new HashMap<>();
175
map.put("name", "Bill");
176
map.put("age", 23);
177
return new ThrowingMap<>(map);
178
}
179
180
@Test public void testUnwrapped() {
181
assertThrowingIterator(map().entrySet().iterator());
182
assertThrowingIterator(map().keySet().iterator());
183
}
184
185
@Test public void test_unmodifiableMap_entrySet() {
186
assertThrowingIterator(Collections.unmodifiableMap(map()).entrySet().iterator());
187
}
188
189
@Test public void test_checkedMap_entrySet() {
190
assertThrowingIterator(Collections.checkedMap(map(), String.class, Object.class).entrySet().iterator());
191
}
192
193
@Test public void test_entrySet_checkedSet() {
194
Set<Map.Entry<String, Object>> entrySet = map().entrySet();
195
Class clazz = entrySet.iterator().next().getClass();
196
assertThrowingIterator(Collections.checkedSet(entrySet, clazz).iterator());
197
}
198
}
199
200