Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 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.checkPositionIndexes;
 
 
 
 import  javax.annotation.Nullable;

Static utility methods pertaining to object arrays.

Author(s):
Kevin Bourrillion
Since:
2.0 (imported from Google Collections Library)
 
 @GwtCompatible(emulated = true)
 public final class ObjectArrays {
   static final Object[] EMPTY_ARRAY = new Object[0];
 
   private ObjectArrays() {}

  
Returns a new array of the given length with the specified component type.

Parameters:
type the component type
length the length of the new array
 
   @GwtIncompatible("Array.newInstance(Class, int)")
   @SuppressWarnings("unchecked")
   public static <T> T[] newArray(Class<T> typeint length) {
     return (T[]) Array.newInstance(typelength);
   }

  
Returns a new array of the given length with the same type as a reference array.

Parameters:
reference any array of the desired type
length the length of the new array
 
   public static <T> T[] newArray(T[] referenceint length) {
     return Platform.newArray(referencelength);
   }

  
Returns a new array that contains the concatenated contents of two arrays.

Parameters:
first the first array of elements to concatenate
second the second array of elements to concatenate
type the component type of the returned array
 
   @GwtIncompatible("Array.newInstance(Class, int)")
   public static <T> T[] concat(T[] first, T[] secondClass<T> type) {
     T[] result = newArray(typefirst.length + second.length);
     System.arraycopy(first, 0, result, 0, first.length);
     System.arraycopy(second, 0, resultfirst.lengthsecond.length);
     return result;
   }

  
Returns a new array that prepends element to array.

Parameters:
element the element to prepend to the front of array
array the array of elements to append
Returns:
an array whose size is one larger than array, with element occupying the first position, and the elements of array occupying the remaining elements.
 
   public static <T> T[] concat(@Nullable T element, T[] array) {
     T[] result = newArray(arrayarray.length + 1);
     result[0] = element;
     System.arraycopy(array, 0, result, 1, array.length);
     return result;
   }

  
Returns a new array that appends element to array.

Parameters:
array the array of elements to prepend
element the element to append to the end
Returns:
an array whose size is one larger than array, with the same contents as array, plus element occupying the last position.
  public static <T> T[] concat(T[] array, @Nullable T element) {
    T[] result = arraysCopyOf(arrayarray.length + 1);
    result[array.length] = element;
    return result;
  }

  
GWT safe version of Arrays.copyOf.
  static <T> T[] arraysCopyOf(T[] originalint newLength) {
    T[] copy = newArray(originalnewLength);
    System.arraycopy(
        original, 0, copy, 0, Math.min(original.lengthnewLength));
    return copy;
  }

  
Returns an array containing all of the elements in the specified collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the specified collection.

If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements.

This method returns the elements in the order they are returned by the collection's iterator.

TODO(kevinb): support concurrently modified collections?

Parameters:
c the collection for which to return an array of elements
array the array in which to place the collection elements
Throws:
ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in the specified collection
  static <T> T[] toArrayImpl(Collection<?> c, T[] array) {
    int size = c.size();
    if (array.length < size) {
      array = newArray(arraysize);
    }
    fillArray(carray);
    if (array.length > size) {
      array[size] = null;
    }
    return array;
  }
  
  
Implementation of Collection.toArray(Object[]) for collections backed by an object array. the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the specified collection.

If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements.

  static <T> T[] toArrayImpl(Object[] srcint offsetint len, T[] dst) {
    checkPositionIndexes(offsetoffset + lensrc.length);
    if (dst.length < len) {
      dst = newArray(dstlen);
    } else if (dst.length > len) {
      dst[len] = null;
    }
    System.arraycopy(srcoffsetdst, 0, len);
    return dst;
  }

  
Returns an array containing all of the elements in the specified collection. This method returns the elements in the order they are returned by the collection's iterator. The returned array is "safe" in that no references to it are maintained by the collection. The caller is thus free to modify the returned array.

This method assumes that the collection size doesn't change while the method is running.

TODO(kevinb): support concurrently modified collections?

Parameters:
c the collection for which to return an array of elements
  static Object[] toArrayImpl(Collection<?> c) {
    return fillArray(cnew Object[c.size()]);
  }

  
Returns a copy of the specified subrange of the specified array that is literally an Object[], and not e.g. a String[].
  static Object[] copyAsObjectArray(Object[] elementsint offsetint length) {
    checkPositionIndexes(offsetoffset + lengthelements.length);
    if (length == 0) {
      return ;
    }
    Object[] result = new Object[length];
    System.arraycopy(elementsoffsetresult, 0, length);
    return result;
  }
  private static Object[] fillArray(Iterable<?> elementsObject[] array) {
    int i = 0;
    for (Object element : elements) {
      array[i++] = element;
    }
    return array;
  }

  
Swaps array[i] with array[j].
  static void swap(Object[] arrayint iint j) {
    Object temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
  static Object[] checkElementsNotNull(Object... array) {
    return checkElementsNotNull(arrayarray.length);
  }
  
  static Object[] checkElementsNotNull(Object[] arrayint length) {
    for (int i = 0; i < lengthi++) {
      checkElementNotNull(array[i], i);
    }
    return array;
  }
  // We do this instead of Preconditions.checkNotNull to save boxing and array
  // creation cost.
  static Object checkElementNotNull(Object elementint index) {
    if (element == null) {
      throw new NullPointerException("at index " + index);
    }
    return element;
  }
New to GrepCode? Check out our FAQ X