Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collections/WrappedUnmodifiableCollections.java
41149 views
1
/*
2
* Copyright (c) 2021, 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 6323374
27
* @run testng WrappedUnmodifiableCollections
28
*/
29
30
import java.util.*;
31
import java.util.function.Function;
32
import org.testng.annotations.Test;
33
import static org.testng.Assert.*;
34
35
36
@Test
37
public class WrappedUnmodifiableCollections {
38
39
private static <T,E extends T> void testWrapping(T collection, Function<T,E> wrapper) {
40
var collection1 = wrapper.apply(collection);
41
var collection2 = wrapper.apply(collection1);
42
assertNotSame(collection, collection2);
43
assertSame(collection1, collection2);
44
}
45
46
public void testUnmodifiableListsDontWrap() {
47
List<List<?>> lists = List.of(List.of(), List.of(1,2,3), List.of(1),
48
List.of(1,2,3,4,5,6),
49
List.of(1,2,3).subList(0,1),
50
new LinkedList<>(List.of(1,2,3)),
51
new ArrayList<>(List.of(1,2,3)));
52
53
for(List<?> list : lists) {
54
testWrapping(list, Collections::unmodifiableList);
55
}
56
}
57
58
public void testUnmodifiableCollectionsDontWrap() {
59
Collection<?> list = List.of();
60
testWrapping(list, Collections::unmodifiableCollection);
61
}
62
63
public void testUnmodifiableSetsDontWrap() {
64
65
List<Set<?>> sets = List.of(new TreeSet<>(),
66
Set.of(1, 2),
67
Set.of(1,2,3,4,5,6));
68
69
for (Set<?> set : sets) {
70
testWrapping(set, Collections::unmodifiableSet);
71
}
72
73
TreeSet<?> treeSet = new TreeSet<>();
74
75
//Collections.UnmodifiableSortedSet
76
testWrapping((SortedSet<?>) treeSet, Collections::unmodifiableSortedSet);
77
78
//Collections.UnmodifiableNavigableSet
79
testWrapping((NavigableSet<?>) treeSet, Collections::unmodifiableNavigableSet);
80
81
}
82
83
public void testUnmodifiableMapsDontWrap() {
84
TreeMap<?,?> treeMap = new TreeMap<>();
85
86
List<Map<?,?>> maps = List.of(treeMap,
87
Map.of(1,1),
88
Map.of(1, 1, 2, 2, 3, 3, 4, 4));
89
90
for (Map<?,?> map : maps) {
91
testWrapping(map, Collections::unmodifiableMap);
92
}
93
94
//Collections.UnModifiableSortedMap
95
testWrapping((SortedMap<?,?>) treeMap, Collections::unmodifiableSortedMap);
96
97
//Collections.UnModifiableNavigableMap
98
testWrapping((NavigableMap<?,?>) treeMap, Collections::unmodifiableNavigableMap);
99
100
}
101
102
}
103
104