1/*
2 * Copyright (C) 2007 The Guava Authors
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.google.common.collect.testing;
18
19import java.util.AbstractCollection;
20import java.util.Arrays;
21import java.util.Collection;
22import java.util.Iterator;
23
24/**
25 * A simplistic collection which implements only the bare minimum allowed by the
26 * spec, and throws exceptions whenever it can.
27 *
28 * @author Kevin Bourrillion
29 */
30public class MinimalCollection<E> extends AbstractCollection<E> {
31  // TODO: expose allow nulls parameter?
32
33  public static <E> MinimalCollection<E> of(E... contents) {
34    return new MinimalCollection<E>(Object.class, true, contents);
35  }
36
37  // TODO: use this
38  public static <E> MinimalCollection<E> ofClassAndContents(
39      Class<? super E> type, E... contents) {
40    return new MinimalCollection<E>(type, true, contents);
41  }
42
43  private final E[] contents;
44  private final Class<? super E> type;
45  private final boolean allowNulls;
46
47  // Package-private so that it can be extended.
48  MinimalCollection(Class<? super E> type, boolean allowNulls, E... contents) {
49    // TODO: consider making it shuffle the contents to test iteration order.
50    this.contents = Platform.clone(contents);
51    this.type = type;
52    this.allowNulls = allowNulls;
53
54    if (!allowNulls) {
55      for (Object element : contents) {
56        if (element == null) {
57          throw new NullPointerException();
58        }
59      }
60    }
61  }
62
63  @Override public int size() {
64    return contents.length;
65  }
66
67  @Override public boolean contains(Object object) {
68    if (!allowNulls) {
69      // behave badly
70      if (object == null) {
71        throw new NullPointerException();
72      }
73    }
74    Platform.checkCast(type, object);  // behave badly
75    return Arrays.asList(contents).contains(object);
76  }
77
78  @Override public boolean containsAll(Collection<?> collection) {
79    if (!allowNulls) {
80      for (Object object : collection) {
81        // behave badly
82        if (object == null) {
83          throw new NullPointerException();
84        }
85      }
86    }
87    return super.containsAll(collection);
88  }
89
90  @Override public Iterator<E> iterator() {
91    return Arrays.asList(contents).iterator();
92  }
93
94  @Override public Object[] toArray() {
95    Object[] result = new Object[contents.length];
96    Platform.unsafeArrayCopy(contents, 0, result, 0, contents.length);
97    return result;
98  }
99
100  /*
101   * a "type A" unmodifiable collection freaks out proactively, even if there
102   * wasn't going to be any actual work to do anyway
103   */
104
105  @Override public boolean addAll(Collection<? extends E> elementsToAdd) {
106    throw up();
107  }
108  @Override public boolean removeAll(Collection<?> elementsToRemove) {
109    throw up();
110  }
111  @Override public boolean retainAll(Collection<?> elementsToRetain) {
112    throw up();
113  }
114  @Override public void clear() {
115    throw up();
116  }
117  private static UnsupportedOperationException up() {
118    throw new UnsupportedOperationException();
119  }
120}
121