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

com.codereligion.cherry.collect.ArrayLists Maven / Gradle / Ivy

/**
 * Copyright 2014 www.codereligion.com
 *
 * 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.codereligion.cherry.collect;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import java.util.ArrayList;
import static com.google.common.base.Preconditions.checkArgument;

/**
 * Factory for {@link java.util.ArrayList ArrayLists}.
 *
 * @author Sebastian Gröbler
 * @since 28.12.2014
 */
public final class ArrayLists {

    private ArrayLists() {
        throw new IllegalAccessError("This class is a utility class and must not be instantiated.");
    }

    /**
     * Creates a new instance from the given {@code iterable} by converting each entry with the given {@code function}.
     *
     * @param iterable the entries to be transformed
     * @param function the function to transform the entries with
     * @param       the type of the entries of the given {@code iterable}
     * @param       the type of the resulting entries
     * @return a {@link java.util.ArrayList}, might be empty
     * @throws IllegalArgumentException when any of the given parameters are {@code null}
     */
    public static  ArrayList createFrom(final Iterable iterable, final Function function) {

        checkArgument(iterable != null, "iterable must not be null.");
        checkArgument(function != null, "function must not be null.");

        return OptimizedIterations.createFrom(iterable, function, new ArrayList());
    }

    /**
     * Creates a new instance from the given {@code iterable} by only using the entries to which the given {@code predicate} applies.
     *
     * @param iterable  the entries to be filtered
     * @param predicate the predicate to filter the entries with
     * @param        the type of the entries to be filtered
     * @return a {@link java.util.ArrayList}, might be empty
     * @throws IllegalArgumentException when any of the given parameters are {@code null}
     */
    public static  ArrayList createFrom(final Iterable iterable, final Predicate predicate) {

        checkArgument(iterable != null, "iterable must not be null.");
        checkArgument(predicate != null, "predicate must not be null.");

        return OptimizedIterations.createFrom(iterable, predicate, new ArrayList());
    }

    /**
     * Creates a new instance from the given {@code iterable} by converting each entry with the given {@code function}, if the given {@code predicate} applies.
     *
     * @param iterable  the entries to be filtered and transformed
     * @param predicate the predicate to filter the entries with
     * @param function  the function to transform the entries with
     * @param        the type of the entries of the given {@code iterable}
     * @param        the type of the resulting entries
     * @return a {@link java.util.ArrayList}, might be empty
     * @throws IllegalArgumentException when any of the given parameters are {@code null}
     */
    public static  ArrayList createFrom(final Iterable iterable, final Predicate predicate, final Function function) {

        checkArgument(iterable != null, "iterable must not be null.");
        checkArgument(predicate != null, "predicate must not be null.");
        checkArgument(function != null, "function must not be null.");

        return OptimizedIterations.createFrom(iterable, predicate, function, new ArrayList());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy