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

com.codereligion.cherry.collect.HashMaps 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.HashMap;
import static com.google.common.base.Preconditions.checkArgument;

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

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

    /**
     * Creates a new instance from the given {@code iterable}. Each iterable entry is mapped by its {@code keyFunction} result.
     *
     * @param iterable    the entries to be mapped
     * @param keyFunction the function to retrieve the map key from an entry
     * @param          the type of the keys of the resulting map
     * @param          the type of the values of the resulting map
     * @return a {@link java.util.HashMap}, might be empty
     * @throws IllegalArgumentException when any of the given parameters are {@code null}
     */
    public static  HashMap createFrom(final Iterable iterable, final Function keyFunction) {

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

        return OptimizedIterations.createFrom(iterable, keyFunction, new HashMap());
    }

    /**
     * Creates a new instance from the given {@code iterable}. Each iterable entry is mapped from its {@code keyFunction} result to its {@code valueFunction}
     * result.
     *
     * @param iterable      the entries to be mapped
     * @param keyFunction   the function to retrieve the map key from an entry
     * @param valueFunction the function to retrieve the map value from an entry
     * @param            the type of the entries of the given {@code iterable}
     * @param            the type of the keys of the resulting map
     * @param            the type of the values of the resulting map
     * @return a {@link java.util.HashMap}, might be empty
     * @throws IllegalArgumentException when any of the given parameters are {@code null}
     */
    public static  HashMap createFrom(final Iterable iterable,
                                                     final Function keyFunction,
                                                     final Function valueFunction) {

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

        return OptimizedIterations.createFrom(iterable, keyFunction, valueFunction, new HashMap());
    }


    /**
     * Creates a new instance from the given {@code iterable}. Each iterable entry is mapped by its {@code keyFunction} result, if the given {@code predicate}
     * applies.
     *
     * @param iterable    the entries to be filtered and mapped
     * @param predicate   the predicate to filter the entries with
     * @param keyFunction the function to retrieve the map key from an entry
     * @param          the type of the keys of the resulting map
     * @param          the type of the values of the resulting map
     * @return a {@link java.util.HashMap}, might be empty
     * @throws IllegalArgumentException when any of the given parameters are {@code null}
     */
    public static  HashMap createFrom(final Iterable iterable, final Predicate predicate, final Function keyFunction) {

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

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

    /**
     * Creates a new instance from the given {@code iterable}. Each iterable entry is mapped from its {@code keyFunction} result to its {@code valueFunction}
     * result, if the given {@code predicate} applies.
     *
     * @param iterable      the entries to be filtered and mapped
     * @param predicate     the predicate to filter the entries with
     * @param keyFunction   the function to retrieve the map key from an entry
     * @param valueFunction the function to retrieve the map value from an entry
     * @param            the type of the entries of the given {@code iterable}
     * @param            the type of the keys of the resulting map
     * @param            the type of the values of the resulting map
     * @return a {@link java.util.HashMap}, might be empty
     * @throws IllegalArgumentException when any of the given parameters are {@code null}
     */
    public static  HashMap createFrom(final Iterable iterable,
                                                     final Predicate predicate,
                                                     final Function keyFunction,
                                                     final Function valueFunction) {

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

        return OptimizedIterations.createFrom(iterable, predicate, keyFunction, valueFunction, new HashMap());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy