1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing;
18
19 import com.google.common.annotations.GwtCompatible;
20
21 import java.util.AbstractCollection;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Iterator;
25
26
27
28
29
30
31
32 @GwtCompatible
33 public class MinimalCollection<E> extends AbstractCollection<E> {
34
35
36 public static <E> MinimalCollection<E> of(E... contents) {
37 return new MinimalCollection<E>(Object.class, true, contents);
38 }
39
40
41 public static <E> MinimalCollection<E> ofClassAndContents(
42 Class<? super E> type, E... contents) {
43 return new MinimalCollection<E>(type, true, contents);
44 }
45
46 private final E[] contents;
47 private final Class<? super E> type;
48 private final boolean allowNulls;
49
50
51 MinimalCollection(Class<? super E> type, boolean allowNulls, E... contents) {
52
53 this.contents = Platform.clone(contents);
54 this.type = type;
55 this.allowNulls = allowNulls;
56
57 if (!allowNulls) {
58 for (Object element : contents) {
59 if (element == null) {
60 throw new NullPointerException();
61 }
62 }
63 }
64 }
65
66 @Override public int size() {
67 return contents.length;
68 }
69
70 @Override public boolean contains(Object object) {
71 if (!allowNulls) {
72
73 if (object == null) {
74 throw new NullPointerException();
75 }
76 }
77 Platform.checkCast(type, object);
78 return Arrays.asList(contents).contains(object);
79 }
80
81 @Override public boolean containsAll(Collection<?> collection) {
82 if (!allowNulls) {
83 for (Object object : collection) {
84
85 if (object == null) {
86 throw new NullPointerException();
87 }
88 }
89 }
90 return super.containsAll(collection);
91 }
92
93 @Override public Iterator<E> iterator() {
94 return Arrays.asList(contents).iterator();
95 }
96
97 @Override public Object[] toArray() {
98 Object[] result = new Object[contents.length];
99 System.arraycopy(contents, 0, result, 0, contents.length);
100 return result;
101 }
102
103
104
105
106
107
108 @Override public boolean addAll(Collection<? extends E> elementsToAdd) {
109 throw up();
110 }
111 @Override public boolean removeAll(Collection<?> elementsToRemove) {
112 throw up();
113 }
114 @Override public boolean retainAll(Collection<?> elementsToRetain) {
115 throw up();
116 }
117 @Override public void clear() {
118 throw up();
119 }
120 private static UnsupportedOperationException up() {
121 throw new UnsupportedOperationException();
122 }
123 }