All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.internal.Lists Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
/*
 * Copyright (C) 2007 Google Inc.
 *
 * 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.inject.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Static utility methods pertaining to {@link List} instances. Also see this
 * class's counterparts {@link Sets} and {@link Maps}.
 *
 * @author Kevin Bourrillion
 * @author Mike Bostock
 */
public final class Lists {
  private Lists() {}

  // ArrayList

  /**
   * Creates an empty {@code ArrayList} instance.
   *
   * 

Note: if you need an immutable empty list, use {@link * Collections#emptyList} instead. * * @return a new, empty {@code ArrayList} */ public static ArrayList newArrayList() { return new ArrayList(); } /** * Creates an {@code ArrayList} instance containing the given elements. * *

Note: if you need an immutable List, use {@link ImmutableList} * instead. * *

Note: due to a bug in javac 1.5.0_06, we cannot support the * following: * *

{@code List list = Lists.newArrayList(sub1, sub2);} * *

where {@code sub1} and {@code sub2} are references to subtypes of {@code * Base}, not of {@code Base} itself. To get around this, you must use: * *

{@code List list = Lists.newArrayList(sub1, sub2);} * * @param elements the elements that the list should contain, in order * @return a new {@code ArrayList} containing those elements */ public static ArrayList newArrayList(E... elements) { // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList list = new ArrayList(capacity); Collections.addAll(list, elements); return list; } static int computeArrayListCapacity(int arraySize) { Preconditions.checkArgument(arraySize >= 0); // TODO: Figure out the right behavior, and document it return (int) Math.min(5L + arraySize + (arraySize / 10), Integer.MAX_VALUE); } /** * Creates an {@code ArrayList} instance containing the given elements. * * @param elements the elements that the list should contain, in order * @return a new {@code ArrayList} containing those elements */ public static ArrayList newArrayList(Iterable elements) { // Let ArrayList's sizing logic work, if possible if (elements instanceof Collection) { @SuppressWarnings("unchecked") Collection collection = (Collection) elements; return new ArrayList(collection); } else { return newArrayList(elements.iterator()); } } /** * Creates an {@code ArrayList} instance containing the given elements. * * @param elements the elements that the list should contain, in order * @return a new {@code ArrayList} containing those elements */ public static ArrayList newArrayList(Iterator elements) { ArrayList list = newArrayList(); while (elements.hasNext()) { list.add(elements.next()); } return list; } /** * Returns an unmodifiable list containing the specified first element and * the additional elements. */ public static ArrayList newArrayList(@Nullable E first, E[] rest) { ArrayList result = new ArrayList(rest.length + 1); result.add(first); for (E element : rest) { result.add(element); } return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy