2
* Copyright (C) 2007 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.collect.testing;
19
import com.google.common.annotations.GwtCompatible;
20
import java.util.AbstractCollection;
21
import java.util.Arrays;
22
import java.util.Collection;
23
import java.util.Iterator;
24
import org.checkerframework.checker.nullness.qual.NonNull;
25
import org.checkerframework.checker.nullness.qual.Nullable;
28
* A simplistic collection which implements only the bare minimum allowed by the spec, and throws
29
* exceptions whenever it can.
31
* @author Kevin Bourrillion
34
@ElementTypesAreNonnullByDefault
35
public class MinimalCollection<E extends @Nullable Object> extends AbstractCollection<E> {
36
// TODO: expose allow nulls parameter?
38
public static <E extends @Nullable Object> MinimalCollection<E> of(E... contents) {
39
return new MinimalCollection<>(Object.class, true, contents);
43
public static <E extends @Nullable Object> MinimalCollection<E> ofClassAndContents(
44
Class<? super @NonNull E> type, E... contents) {
45
return new MinimalCollection<>(type, true, contents);
48
private final E[] contents;
49
private final Class<? super @NonNull E> type;
50
private final boolean allowNulls;
52
// Package-private so that it can be extended.
53
MinimalCollection(Class<? super @NonNull E> type, boolean allowNulls, E... contents) {
54
// TODO: consider making it shuffle the contents to test iteration order.
55
this.contents = Platform.clone(contents);
57
this.allowNulls = allowNulls;
60
for (Object element : contents) {
61
if (element == null) {
62
throw new NullPointerException();
70
return contents.length;
74
public boolean contains(@Nullable Object object) {
78
throw new NullPointerException();
81
Platform.checkCast(type, object); // behave badly
82
return Arrays.asList(contents).contains(object);
86
public boolean containsAll(Collection<?> collection) {
88
for (Object object : collection) {
91
throw new NullPointerException();
95
return super.containsAll(collection);
99
public Iterator<E> iterator() {
100
return Arrays.asList(contents).iterator();
104
public @Nullable Object[] toArray() {
105
@Nullable Object[] result = new @Nullable Object[contents.length];
106
System.arraycopy(contents, 0, result, 0, contents.length);
111
* a "type A" unmodifiable collection freaks out proactively, even if there
112
* wasn't going to be any actual work to do anyway
116
public boolean addAll(Collection<? extends E> elementsToAdd) {
121
public boolean removeAll(Collection<?> elementsToRemove) {
126
public boolean retainAll(Collection<?> elementsToRetain) {
131
public void clear() {
135
private static UnsupportedOperationException up() {
136
throw new UnsupportedOperationException();