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

com.codereligion.cherry.collect.OptimizedIterations 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 com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

final class OptimizedIterations {

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

    static > C createFrom(final Iterable inputIterable, final Function function, final C outputCollection) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                outputCollection.add(function.apply(list.get(i)));
            }
        } else {
            for (final F f : inputIterable) {
                outputCollection.add(function.apply(f));
            }
        }
        return outputCollection;
    }

    static > C createFrom(final Iterable inputIterable, final Predicate predicate, final C outputCollection) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E e = list.get(i);
                if (predicate.apply(e)) {
                    outputCollection.add(e);
                }
            }
        } else {
            for (final E e : inputIterable) {
                if (predicate.apply(e)) {
                    outputCollection.add(e);
                }
            }
        }
        return outputCollection;
    }

    static > C createFrom(final Iterable inputIterable,
                                                        final Predicate predicate,
                                                        final Function function,
                                                        final C outputCollection) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final F f = list.get(i);
                if (predicate.apply(f)) {
                    outputCollection.add(function.apply(f));
                }
            }
        } else {
            for (final F f : inputIterable) {
                if (predicate.apply(f)) {
                    outputCollection.add(function.apply(f));
                }
            }
        }
        return outputCollection;
    }

    static > B createFrom(final Iterable inputIterable,
                                                                         final Function function,
                                                                         final B builder) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                builder.add(function.apply(list.get(i)));
            }
        } else {
            for (final F f : inputIterable) {
                builder.add(function.apply(f));
            }
        }
        return builder;
    }

    static > B createFrom(final Iterable inputIterable, final Predicate predicate, final B builder) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E e = list.get(i);
                if (predicate.apply(e)) {
                    builder.add(e);
                }
            }
        } else {
            for (final E e : inputIterable) {
                if (predicate.apply(e)) {
                    builder.add(e);
                }
            }
        }
        return builder;
    }

    static > B createFrom(final Iterable inputIterable,
                                                                         final Predicate predicate,
                                                                         final Function function,
                                                                         final B builder) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final F f = list.get(i);
                if (predicate.apply(f)) {
                    builder.add(function.apply(f));
                }
            }
        } else {
            for (final F f : inputIterable) {
                if (predicate.apply(f)) {
                    builder.add(function.apply(f));
                }
            }
        }
        return builder;
    }

    static > M createFrom(final Iterable inputIterable, final Function keyFunction, final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final V entry = list.get(i);
                multimap.put(keyFunction.apply(entry), entry);
            }
        } else {
            for (final V entry : inputIterable) {
                multimap.put(keyFunction.apply(entry), entry);
            }
        }

        return multimap;
    }

    static > M createFrom(final Iterable inputIterable,
                                                       final Function keyFunction,
                                                       final Function valueFunction,
                                                       final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E entry = list.get(i);
                multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
            }
        } else {
            for (final E entry : inputIterable) {
                multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
            }
        }

        return multimap;
    }

    static > M createFrom(final Iterable inputIterable,
                                                    final Predicate predicate,
                                                    final Function keyFunction,
                                                    final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final V entry = list.get(i);
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), entry);
                }
            }
        } else {
            for (final V entry : inputIterable) {
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), entry);
                }
            }
        }

        return multimap;
    }

    static > M createFrom(final Iterable inputIterable,
                                                       final Predicate predicate,
                                                       final Function keyFunction,
                                                       final Function valueFunction,
                                                       final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E entry = list.get(i);
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
                }
            }
        } else {
            for (final E entry : inputIterable) {
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
                }
            }
        }

        return multimap;
    }

    static > M createFrom(final Iterable inputIterable, final Function keyFunction, final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final V entry = list.get(i);
                multimap.put(keyFunction.apply(entry), entry);
            }
        } else {
            for (final V entry : inputIterable) {
                multimap.put(keyFunction.apply(entry), entry);
            }
        }

        return multimap;
    }

    static > M createFrom(final Iterable inputIterable,
                                                            final Function keyFunction,
                                                            final Function valueFunction,
                                                            final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E entry = list.get(i);
                multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
            }
        } else {
            for (final E entry : inputIterable) {
                multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
            }
        }

        return multimap;
    }

    static > M createFrom(final Iterable inputIterable,
                                                         final Predicate predicate,
                                                         final Function keyFunction,
                                                         final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final V entry = list.get(i);
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), entry);
                }
            }
        } else {
            for (final V entry : inputIterable) {
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), entry);
                }
            }
        }

        return multimap;
    }

    static > M createFrom(final Iterable inputIterable,
                                                            final Predicate predicate,
                                                            final Function keyFunction,
                                                            final Function valueFunction,
                                                            final M multimap) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E entry = list.get(i);
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
                }
            }
        } else {
            for (final E entry : inputIterable) {
                if (predicate.apply(entry)) {
                    multimap.put(keyFunction.apply(entry), valueFunction.apply(entry));
                }
            }
        }

        return multimap;
    }

    static > B createFrom(final Iterable inputIterable,
                                                                     final Function keyFunction,
                                                                     final B builder) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final V entry = list.get(i);
                builder.put(keyFunction.apply(entry), entry);
            }
        } else {
            for (final V entry : inputIterable) {
                builder.put(keyFunction.apply(entry), entry);
            }
        }

        return builder;
    }

    static > B createFrom(final Iterable inputIterable,
                                                                        final Function keyFunction,
                                                                        final Function valueFunction,
                                                                        final B builder) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E entry = list.get(i);
                builder.put(keyFunction.apply(entry), valueFunction.apply(entry));
            }
        } else {
            for (final E entry : inputIterable) {
                builder.put(keyFunction.apply(entry), valueFunction.apply(entry));
            }
        }

        return builder;
    }

    static > B createFrom(final Iterable inputIterable,
                                                                     final Predicate predicate,
                                                                     final Function keyFunction,
                                                                     final B builder) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final V entry = list.get(i);
                if (predicate.apply(entry)) {
                    builder.put(keyFunction.apply(entry), entry);
                }
            }
        } else {
            for (final V entry : inputIterable) {
                if (predicate.apply(entry)) {
                    builder.put(keyFunction.apply(entry), entry);
                }
            }
        }

        return builder;
    }

    static > B createFrom(final Iterable inputIterable,
                                                                        final Predicate predicate,
                                                                        final Function keyFunction,
                                                                        final Function valueFunction,
                                                                        final B builder) {

        if (inputIterable instanceof ArrayList) {
            final List list = (List) inputIterable;
            for (int i = 0; i < list.size(); i++) {
                final E entry = list.get(i);
                if (predicate.apply(entry)) {
                    builder.put(keyFunction.apply(entry), valueFunction.apply(entry));
                }
            }
        } else {
            for (final E entry : inputIterable) {
                if (predicate.apply(entry)) {
                    builder.put(keyFunction.apply(entry), valueFunction.apply(entry));
                }
            }
        }

        return builder;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy