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 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 * A simplistic collection which implements only the bare minimum allowed by the 28 * spec, and throws exceptions whenever it can. 29 * 30 * @author Kevin Bourrillion 31 */ 32 @GwtCompatible 33 public class MinimalCollection<E> extends AbstractCollection<E> { 34 // TODO: expose allow nulls parameter? 35 of(E... contents)36 public static <E> MinimalCollection<E> of(E... contents) { 37 return new MinimalCollection<E>(Object.class, true, contents); 38 } 39 40 // TODO: use this ofClassAndContents( Class<? super E> type, E... contents)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 // Package-private so that it can be extended. MinimalCollection(Class<? super E> type, boolean allowNulls, E... contents)51 MinimalCollection(Class<? super E> type, boolean allowNulls, E... contents) { 52 // TODO: consider making it shuffle the contents to test iteration order. 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 size()66 @Override public int size() { 67 return contents.length; 68 } 69 contains(Object object)70 @Override public boolean contains(Object object) { 71 if (!allowNulls) { 72 // behave badly 73 if (object == null) { 74 throw new NullPointerException(); 75 } 76 } 77 Platform.checkCast(type, object); // behave badly 78 return Arrays.asList(contents).contains(object); 79 } 80 containsAll(Collection<?> collection)81 @Override public boolean containsAll(Collection<?> collection) { 82 if (!allowNulls) { 83 for (Object object : collection) { 84 // behave badly 85 if (object == null) { 86 throw new NullPointerException(); 87 } 88 } 89 } 90 return super.containsAll(collection); 91 } 92 iterator()93 @Override public Iterator<E> iterator() { 94 return Arrays.asList(contents).iterator(); 95 } 96 toArray()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 * a "type A" unmodifiable collection freaks out proactively, even if there 105 * wasn't going to be any actual work to do anyway 106 */ 107 addAll(Collection<? extends E> elementsToAdd)108 @Override public boolean addAll(Collection<? extends E> elementsToAdd) { 109 throw up(); 110 } removeAll(Collection<?> elementsToRemove)111 @Override public boolean removeAll(Collection<?> elementsToRemove) { 112 throw up(); 113 } retainAll(Collection<?> elementsToRetain)114 @Override public boolean retainAll(Collection<?> elementsToRetain) { 115 throw up(); 116 } clear()117 @Override public void clear() { 118 throw up(); 119 } up()120 private static UnsupportedOperationException up() { 121 throw new UnsupportedOperationException(); 122 } 123 } 124