Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2007 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 
     17 package com.google.common.collect;
     18 
     19 import com.google.common.annotations.GwtCompatible;
     20 import static com.google.common.base.Preconditions.checkElementIndex;
     21 import static com.google.common.base.Preconditions.checkPositionIndex;
     22 import static com.google.common.base.Preconditions.checkPositionIndexes;
     23 
     24 import java.util.Collection;
     25 import java.util.Collections;
     26 import java.util.List;
     27 import java.util.ListIterator;
     28 
     29 import javax.annotation.Nullable;
     30 
     31 /**
     32  * An empty immutable list.
     33  *
     34  * @author Kevin Bourrillion
     35  */
     36 @GwtCompatible(serializable = true)
     37 final class EmptyImmutableList extends ImmutableList<Object> {
     38   static final EmptyImmutableList INSTANCE = new EmptyImmutableList();
     39 
     40   private EmptyImmutableList() {}
     41 
     42   public int size() {
     43     return 0;
     44   }
     45 
     46   @Override public boolean isEmpty() {
     47     return true;
     48   }
     49 
     50   @Override public boolean contains(Object target) {
     51     return false;
     52   }
     53 
     54   @Override public UnmodifiableIterator<Object> iterator() {
     55     return Iterators.emptyIterator();
     56   }
     57 
     58   private static final Object[] EMPTY_ARRAY = new Object[0];
     59 
     60   @Override public Object[] toArray() {
     61     return EMPTY_ARRAY;
     62   }
     63 
     64   @Override public <T> T[] toArray(T[] a) {
     65     if (a.length > 0) {
     66       a[0] = null;
     67     }
     68     return a;
     69   }
     70 
     71   public Object get(int index) {
     72     // guaranteed to fail, but at least we get a consistent message
     73     checkElementIndex(index, 0);
     74     throw new AssertionError("unreachable");
     75   }
     76 
     77   @Override public int indexOf(Object target) {
     78     return -1;
     79   }
     80 
     81   @Override public int lastIndexOf(Object target) {
     82     return -1;
     83   }
     84 
     85   @Override public ImmutableList<Object> subList(int fromIndex, int toIndex) {
     86     checkPositionIndexes(fromIndex, toIndex, 0);
     87     return this;
     88   }
     89 
     90   public ListIterator<Object> listIterator() {
     91     return Collections.emptyList().listIterator();
     92   }
     93 
     94   public ListIterator<Object> listIterator(int start) {
     95     checkPositionIndex(start, 0);
     96     return Collections.emptyList().listIterator();
     97   }
     98 
     99   @Override public boolean containsAll(Collection<?> targets) {
    100     return targets.isEmpty();
    101   }
    102 
    103   @Override public boolean equals(@Nullable Object object) {
    104     if (object instanceof List) {
    105       List<?> that = (List<?>) object;
    106       return that.isEmpty();
    107     }
    108     return false;
    109   }
    110 
    111   @Override public int hashCode() {
    112     return 1;
    113   }
    114 
    115   @Override public String toString() {
    116     return "[]";
    117   }
    118 
    119   Object readResolve() {
    120     return INSTANCE; // preserve singleton property
    121   }
    122 
    123   private static final long serialVersionUID = 0;
    124 }
    125