1 /* 2 * Copyright (C) 2009 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; 18 19 import com.google.common.annotations.GwtCompatible; 20 21 import java.io.InvalidObjectException; 22 import java.io.ObjectInputStream; 23 import java.io.Serializable; 24 25 /** 26 * List returned by {@link ImmutableCollection#asList} when the collection isn't 27 * an {@link ImmutableList} or an {@link ImmutableSortedSet}. 28 * 29 * @author Jared Levy 30 */ 31 @GwtCompatible(serializable = true, emulated = true) 32 @SuppressWarnings("serial") 33 final class ImmutableAsList<E> extends RegularImmutableList<E> { 34 private final transient ImmutableCollection<E> collection; 35 36 ImmutableAsList(Object[] array, ImmutableCollection<E> collection) { 37 super(array, 0, array.length); 38 this.collection = collection; 39 } 40 41 @Override public boolean contains(Object target) { 42 // The collection's contains() is at least as fast as RegularImmutableList's 43 // and is often faster. 44 return collection.contains(target); 45 } 46 47 /** 48 * Serialized form that leads to the same performance as the original list. 49 */ 50 static class SerializedForm implements Serializable { 51 final ImmutableCollection<?> collection; 52 SerializedForm(ImmutableCollection<?> collection) { 53 this.collection = collection; 54 } 55 Object readResolve() { 56 return collection.asList(); 57 } 58 private static final long serialVersionUID = 0; 59 } 60 61 private void readObject(ObjectInputStream stream) 62 throws InvalidObjectException { 63 throw new InvalidObjectException("Use SerializedForm"); 64 } 65 66 @Override Object writeReplace() { 67 return new SerializedForm(collection); 68 } 69 } 70