Coverage Summary for Class: SingletonImmutableList (com.google.common.collect)

Class Class, % Method, % Line, %
SingletonImmutableList 100% (1/1) 75% (6/8) 75% (9/12)


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 static com.google.common.base.Preconditions.checkNotNull; 20  21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.base.Preconditions; 23 import java.util.Collections; 24 import java.util.Spliterator; 25  26 /** 27  * Implementation of {@link ImmutableList} with exactly one element. 28  * 29  * @author Hayward Chan 30  */ 31 @GwtCompatible(serializable = true, emulated = true) 32 @SuppressWarnings("serial") // uses writeReplace(), not default serialization 33 @ElementTypesAreNonnullByDefault 34 final class SingletonImmutableList<E> extends ImmutableList<E> { 35  36  final transient E element; 37  38  SingletonImmutableList(E element) { 39  this.element = checkNotNull(element); 40  } 41  42  @Override 43  public E get(int index) { 44  Preconditions.checkElementIndex(index, 1); 45  return element; 46  } 47  48  @Override 49  public UnmodifiableIterator<E> iterator() { 50  return Iterators.singletonIterator(element); 51  } 52  53  @Override 54  public Spliterator<E> spliterator() { 55  return Collections.singleton(element).spliterator(); 56  } 57  58  @Override 59  public int size() { 60  return 1; 61  } 62  63  @Override 64  public ImmutableList<E> subList(int fromIndex, int toIndex) { 65  Preconditions.checkPositionIndexes(fromIndex, toIndex, 1); 66  return (fromIndex == toIndex) ? ImmutableList.<E>of() : this; 67  } 68  69  @Override 70  public String toString() { 71  return '[' + element.toString() + ']'; 72  } 73  74  @Override 75  boolean isPartialView() { 76  return false; 77  } 78 }