Beispiel #1
0
 @GwtIncompatible("ObjectArrays.newArray(Class, int)")
 public void testNewArray_fromClass_OfArray() {
   String[][] array = ObjectArrays.newArray(String[].class, 1);
   assertEquals(String[][].class, array.getClass());
   assertEquals(1, array.length);
   assertNull(array[0]);
 }
Beispiel #2
0
 @Test
 public void testCollectionInstance() {
   Maps.newLinkedHashMap();
   Lists.newArrayList();
   Sets.newHashSet();
   ObjectArrays.newArray(Integer.class, 10);
 }
Beispiel #3
0
 @Override
 public <T> T[] toArray(T[] other) {
   if (other.length < size) {
     other = ObjectArrays.newArray(other, size);
   } else if (other.length > size) {
     other[size] = null;
   }
   System.arraycopy(array, offset, other, 0, size);
   return other;
 }
Beispiel #4
0
 // TODO: Move to ObjectArrays (same code in ImmutableList).
 @Override
 public <T> T[] toArray(T[] array) {
   int size = size();
   if (array.length < size) {
     array = ObjectArrays.newArray(array, size);
   } else if (array.length > size) {
     array[size] = null;
   }
   System.arraycopy(elements, fromIndex, array, 0, size);
   return array;
 }
 @Override
 public <T> T[] toArray(T[] array) {
   if (array.length == 0) {
     array = ObjectArrays.newArray(array, 1);
   } else if (array.length > 1) {
     array[1] = null;
   }
   // Writes will produce ArrayStoreException when the toArray() doc requires.
   Object[] objectArray = array;
   objectArray[0] = element;
   return array;
 }
 public <T> T[] toArray(T[] paramArrayOfT) {
   Object localObject;
   if (paramArrayOfT.length == 0) {
     localObject = ObjectArrays.newArray(paramArrayOfT, 1);
   }
   for (; ; ) {
     localObject[0] = element;
     return (T[]) localObject;
     localObject = paramArrayOfT;
     if (paramArrayOfT.length > 1) {
       paramArrayOfT[1] = null;
       localObject = paramArrayOfT;
     }
   }
 }
Beispiel #7
0
    /*
     * TODO(hhchan): Revert once we have a separate, manual emulation of this
     * class.
     */
    @Override
    public <T> T[] toArray(T[] other) {
      int size = size();
      if (other.length < size) {
        other = ObjectArrays.newArray(other, size);
      } else if (other.length > size) {
        other[size] = null;
      }

      // Writes will produce ArrayStoreException when the toArray() doc requires
      Object[] otherAsObjectArray = other;
      int index = 0;
      for (Entry<?> element : this) {
        otherAsObjectArray[index++] = element;
      }
      return other;
    }
  @Override
  @SuppressWarnings("nullness")
  // Suppressed due to annotations of toArray
  public <T> /*@Nullable*/ T[] toArray(T[] other) {
    int size = size();
    if (other.length < size) {
      other = ObjectArrays.newArray(other, size);
    } else if (other.length > size) {
      other[size] = null;
    }

    // Writes will produce ArrayStoreException when the toArray() doc requires.
    Object[] otherAsObjectArray = other;
    int index = 0;
    for (E element : this) {
      otherAsObjectArray[index++] = element;
    }
    return other;
  }
Beispiel #9
0
 public void testNewArray_fromArray_OfArray() {
   String[][] array = ObjectArrays.newArray(new String[0][0], 1);
   assertEquals(String[][].class, array.getClass());
   assertEquals(1, array.length);
   assertNull(array[0]);
 }
Beispiel #10
0
 public void testNewArray_fromArray_Empty() {
   String[] in = new String[0];
   String[] empty = ObjectArrays.newArray(in, 0);
   assertEquals(0, empty.length);
 }
Beispiel #11
0
 @GwtIncompatible("ObjectArrays.newArray(Class, int)")
 public void testNewArray_fromClass_Empty() {
   String[] empty = ObjectArrays.newArray(String.class, 0);
   assertEquals(String[].class, empty.getClass());
   assertEquals(0, empty.length);
 }
 private static void doTestNewArrayEquals(Object[] expected, int length) {
   checkArrayEquals(expected, ObjectArrays.newArray(expected, length));
 }
 public void testNonEmptyToLonger() {
   checkArrayEquals(
       new String[10], ObjectArrays.newArray(new String[] {"a", "b", "c", "d", "e"}, 10));
 }
 public void testNonEmptyToShorter() {
   checkArrayEquals(new String[9], ObjectArrays.newArray(new String[10], 9));
 }
 public void testEmptyArrayToNonEmpty() {
   checkArrayEquals(new Long[5], ObjectArrays.newArray(new Long[0], 5));
 }
Beispiel #16
0
 /**
  * Copies an iterable's elements into an array.
  *
  * @param iterable the iterable to copy
  * @param type the type of the elements
  * @return a newly-allocated array into which all the elements of the iterable have been copied
  */
 @GwtIncompatible("Array.newInstance(Class, int)")
 public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) {
   Collection<? extends T> collection = toCollection(iterable);
   T[] array = ObjectArrays.newArray(type, collection.size());
   return collection.toArray(array);
 }