ImmutableCollection.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/* 2 * Copyright (C) 2008 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; 18 19import java.io.Serializable; 20import java.util.Arrays; 21import java.util.Collection; 22import java.util.Collections; 23import java.util.Iterator; 24 25import javax.annotation.Nullable; 26 27import static com.google.common.base.Preconditions.checkNotNull; 28 29/** 30 * GWT emulated version of {@link ImmutableCollection}. 31 * 32 * @author Jesse Wilson 33 */ 34@SuppressWarnings("serial") // we're overriding default serialization 35public abstract class ImmutableCollection<E> 36 implements Collection<E>, Serializable { 37 38 static final ImmutableCollection<Object> EMPTY_IMMUTABLE_COLLECTION 39 = new ForwardingImmutableCollection<Object>(Collections.emptyList()); 40 41 ImmutableCollection() {} 42 43 public abstract UnmodifiableIterator<E> iterator(); 44 45 public Object[] toArray() { 46 Object[] newArray = new Object[size()]; 47 return toArray(newArray); 48 } 49 50 public <T> T[] toArray(T[] other) { 51 int size = size(); 52 if (other.length < size) { 53 other = ObjectArrays.newArray(other, size); 54 } else if (other.length > size) { 55 other[size] = null; 56 } 57 58 // Writes will produce ArrayStoreException when the toArray() doc requires. 59 Object[] otherAsObjectArray = other; 60 int index = 0; 61 for (E element : this) { 62 otherAsObjectArray[index++] = element; 63 } 64 return other; 65 } 66 67 public boolean contains(@Nullable Object object) { 68 if (object == null) { 69 return false; 70 } 71 for (E element : this) { 72 if (element.equals(object)) { 73 return true; 74 } 75 } 76 return false; 77 } 78 79 public boolean containsAll(Collection<?> targets) { 80 for (Object target : targets) { 81 if (!contains(target)) { 82 return false; 83 } 84 } 85 return true; 86 } 87 88 public boolean isEmpty() { 89 return size() == 0; 90 } 91 92 public final boolean add(E e) { 93 throw new UnsupportedOperationException(); 94 } 95 96 public final boolean remove(Object object) { 97 throw new UnsupportedOperationException(); 98 } 99 100 public final boolean addAll(Collection<? extends E> newElements) { 101 throw new UnsupportedOperationException(); 102 } 103 104 public final boolean removeAll(Collection<?> oldElements) { 105 throw new UnsupportedOperationException(); 106 } 107 108 public final boolean retainAll(Collection<?> elementsToKeep) { 109 throw new UnsupportedOperationException(); 110 } 111 112 public final void clear() { 113 throw new UnsupportedOperationException(); 114 } 115 116 private transient ImmutableList<E> asList; 117 118 public ImmutableList<E> asList() { 119 ImmutableList<E> list = asList; 120 return (list == null) ? (asList = createAsList()) : list; 121 } 122 123 ImmutableList<E> createAsList() { 124 switch (size()) { 125 case 0: 126 return ImmutableList.of(); 127 case 1: 128 return ImmutableList.of(iterator().next()); 129 default: 130 @SuppressWarnings("unchecked") 131 E[] castedArray = (E[]) toArray(); 132 return new ImmutableAsList<E>(Arrays.asList(castedArray)); 133 } 134 } 135 static <E> ImmutableCollection<E> unsafeDelegate(Collection<E> delegate) { 136 return new ForwardingImmutableCollection<E>(delegate); 137 } 138 139 boolean isPartialView(){ 140 return false; 141 } 142 143 abstract static class Builder<E> { 144 145 public abstract Builder<E> add(E element); 146 147 public Builder<E> add(E... elements) { 148 checkNotNull(elements); // for GWT 149 for (E element : elements) { 150 add(checkNotNull(element)); 151 } 152 return this; 153 } 154 155 public Builder<E> addAll(Iterable<? extends E> elements) { 156 checkNotNull(elements); // for GWT 157 for (E element : elements) { 158 add(checkNotNull(element)); 159 } 160 return this; 161 } 162 163 public Builder<E> addAll(Iterator<? extends E> elements) { 164 checkNotNull(elements); // for GWT 165 while (elements.hasNext()) { 166 add(checkNotNull(elements.next())); 167 } 168 return this; 169 } 170 171 public abstract ImmutableCollection<E> build(); 172 } 173} 174