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