Java List Create createOrGrow(List list, int minCapacity)

Here you can find the source of createOrGrow(List list, int minCapacity)

Description

Grows the given list to the given capacity, or creates a new list if the given list is null.

License

Open Source License

Parameter

Parameter Description
list The list to be grown, or null.
minCapacity The capacity to grow the list to.
E The component type of the list.

Return

Either the given list grown to the given capacity, or a new list.

Declaration

private static <E> List<E> createOrGrow(List<E> list, int minCapacity) 

Method Source Code


//package com.java2s;
/*//from   ww w  .  j a v a  2s. c om
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Grows the given list to the given capacity, or creates a new list if the given list is null.
     *
     * @param list          The list to be grown, or null.
     * @param minCapacity   The capacity to grow the list to.
     * @param <E>           The component type of the list.
     * @return              Either the given list grown to the given capacity, or a new list.
     */
    private static <E> List<E> createOrGrow(List<E> list, int minCapacity) {
        if (list == null) {
            list = create(minCapacity);
        } else if (list instanceof ArrayList) {
            ((ArrayList) list).ensureCapacity(minCapacity);
        }
        return list;
    }

    /**
     * Creates a new list.
     *
     * @param <E>             The component type of the list.
     * @return                A new list.
     */
    public static <E> List<E> create() {
        return new ArrayList<E>();
    }

    /**
     * Creates a new list with the given initial capacity.
     *
     * @param initialCapacity The initial capacity of the new list.
     * @param <E>             The component type of the list.
     * @return                A new list.
     */
    public static <E> List<E> create(int initialCapacity) {
        return new ArrayList<E>(initialCapacity);
    }
}

Related

  1. createNullList(int numberOfElements)
  2. CreateObjectList(Object... values)
  3. createOneElementList(T element)
  4. createOrderedQuery(String attributeName, List ids)
  5. createOrgAttributeList()
  6. createOutputTuple(List> tuples)
  7. createPostBody(final List required)
  8. createPrimaryKeyList(String primaryKeyStr)
  9. createPrimaryKeysQuery(String tableName, List primaryKeys)