Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2008 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.ObjectArrays.checkElementsNotNull;
 
 
 
 import  javax.annotation.Nullable;

An immutable collection. Does not permit null elements.

In addition to the Collection methods, this class has an asList() method, which returns a list view of the collection's elements.

Note: Although this class is not final, it cannot be subclassed outside of this package as it has no public or protected constructors. Thus, instances of this type are guaranteed to be immutable.

Author(s):
Jesse Wilson
Since:
2.0 (imported from Google Collections Library)
 
 @GwtCompatible(emulated = true)
 @SuppressWarnings("serial"// we're overriding default serialization
 public abstract class ImmutableCollection<E> extends AbstractCollection<E>
     implements Serializable {
 
   ImmutableCollection() {}

  
Returns an unmodifiable iterator across the elements in this collection.
 
   @Override
   public abstract UnmodifiableIterator<E> iterator();
 
   @Override
   public final Object[] toArray() {
     int size = size();
     if (size == 0) {
       return .;
     }
     Object[] result = new Object[size()];
     copyIntoArray(result, 0);
     return result;
   }
 
   @Override
   public final <T> T[] toArray(T[] other) {
     checkNotNull(other);
     int size = size();
     if (other.length < size) {
       other = ObjectArrays.newArray(othersize);
     } else if (other.length > size) {
       other[size] = null;
     }
     copyIntoArray(other, 0);
     return other;
   }
 
   @Override
   public boolean contains(@Nullable Object object) {
     return object != null && super.contains(object);
   }

  
Guaranteed to throw an exception and leave the collection unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
 
   @Override
   public final boolean add(E e) {
     throw new UnsupportedOperationException();
   }

  
Guaranteed to throw an exception and leave the collection unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  public final boolean remove(Object object) {
    throw new UnsupportedOperationException();
  }

  
Guaranteed to throw an exception and leave the collection unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  public final boolean addAll(Collection<? extends E> newElements) {
    throw new UnsupportedOperationException();
  }

  
Guaranteed to throw an exception and leave the collection unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  public final boolean removeAll(Collection<?> oldElements) {
    throw new UnsupportedOperationException();
  }

  
Guaranteed to throw an exception and leave the collection unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  public final boolean retainAll(Collection<?> elementsToKeep) {
    throw new UnsupportedOperationException();
  }

  
Guaranteed to throw an exception and leave the collection unmodified.

Deprecated:
Unsupported operation.
Throws:
UnsupportedOperationException always
  public final void clear() {
    throw new UnsupportedOperationException();
  }
  /*
   * TODO(kevinb): Restructure code so ImmutableList doesn't contain this
   * variable, which it doesn't use.
   */
  private transient ImmutableList<E> asList;

  
Returns a list view of the collection.

Since:
2.0
  public ImmutableList<E> asList() {
    ImmutableList<E> list = ;
    return (list == null) ? ( = createAsList()) : list;
  }
    switch (size()) {
      case 0:
        return ImmutableList.of();
      case 1:
        return ImmutableList.of(iterator().next());
      default:
        return new RegularImmutableAsList<E>(thistoArray());
    }
  }

  
Returns true if this immutable collection's implementation contains references to user-created objects that aren't accessible via this collection's methods. This is generally used to determine whether copyOf implementations should make an explicit copy to avoid memory leaks.
  abstract boolean isPartialView();
  
  
Copies the contents of this immutable collection into the specified array at the specified offset. Returns offset + size().
  int copyIntoArray(Object[] dstint offset) {
    for (E e : this) {
      dst[offset++] = e;
    }
    return offset;
  }
    // We serialize by default to ImmutableList, the simplest thing that works.
    return new ImmutableList.SerializedForm(toArray());
  }

  
Abstract base class for builders of ImmutableCollection types.

Since:
10.0
  public abstract static class Builder<E> {
    static final int DEFAULT_INITIAL_CAPACITY = 4;
    static int expandedCapacity(int oldCapacityint minCapacity) {
      if (minCapacity < 0) {
        throw new AssertionError("cannot store more than MAX_VALUE elements");
      }
      // careful of overflow!
      int newCapacity = oldCapacity + (oldCapacity >> 1) + 1;
      if (newCapacity < minCapacity) {
        newCapacity = Integer.highestOneBit(minCapacity - 1) << 1;
      }
      if (newCapacity < 0) {
        newCapacity = .;
        // guaranteed to be >= newCapacity
      }
      return newCapacity;
    }
    Builder() {
    }

    
Adds element to the ImmutableCollection being built.

Note that each builder class covariantly returns its own type from this method.

Parameters:
element the element to add
Returns:
this Builder instance
Throws:
NullPointerException if element is null
    public abstract Builder<E> add(E element);

    
Adds each element of elements to the ImmutableCollection being built.

Note that each builder class overrides this method in order to covariantly return its own type.

Parameters:
elements the elements to add
Returns:
this Builder instance
Throws:
NullPointerException if elements is null or contains a null element
    public Builder<E> add(E... elements) {
      for (E element : elements) {
        add(element);
      }
      return this;
    }

    
Adds each element of elements to the ImmutableCollection being built.

Note that each builder class overrides this method in order to covariantly return its own type.

Parameters:
elements the elements to add
Returns:
this Builder instance
Throws:
NullPointerException if elements is null or contains a null element
    public Builder<E> addAll(Iterable<? extends E> elements) {
      for (E element : elements) {
        add(element);
      }
      return this;
    }

    
Adds each element of elements to the ImmutableCollection being built.

Note that each builder class overrides this method in order to covariantly return its own type.

Parameters:
elements the elements to add
Returns:
this Builder instance
Throws:
NullPointerException if elements is null or contains a null element
    public Builder<E> addAll(Iterator<? extends E> elements) {
      while (elements.hasNext()) {
        add(elements.next());
      }
      return this;
    }

    
Returns a newly-created ImmutableCollection of the appropriate type, containing the elements provided to this builder.

Note that each builder class covariantly returns the appropriate type of ImmutableCollection from this method.

    public abstract ImmutableCollection<E> build();
  }
  
  abstract static class ArrayBasedBuilder<E> extends ImmutableCollection.Builder<E> {
    Object[] contents;
    int size;
    
    ArrayBasedBuilder(int initialCapacity) {
      checkArgument(initialCapacity >= 0, "capacity must be >= 0 but was %s"initialCapacity);
      this. = new Object[initialCapacity];
      this. = 0;
    }
    
    
Expand the absolute capacity of the builder so it can accept at least the specified number of elements without being resized.
    private void ensureCapacity(int minCapacity) {
      if (. < minCapacity) {
        this. = ObjectArrays.arraysCopyOf(
            this.expandedCapacity(.minCapacity));
      }
    }
    @Override
    public ArrayBasedBuilder<E> add(E element) {
      checkNotNull(element);
      ensureCapacity( + 1);
      [++] = element;
      return this;
    }
    @Override
    public Builder<E> add(E... elements) {
      checkElementsNotNull(elements);
      ensureCapacity( + elements.length);
      System.arraycopy(elements, 0, elements.length);
       += elements.length;
      return this;
    }
    @Override
    public Builder<E> addAll(Iterable<? extends E> elements) {
      if (elements instanceof Collection) {
        Collection<?> collection = (Collection<?>) elements;
        ensureCapacity( + collection.size());
      }
      super.addAll(elements);
      return this;
    }
  }
New to GrepCode? Check out our FAQ X