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

me.shaftesbury.utils.functional.GenericCollections Maven / Gradle / Ivy

There is a newer version: 1.17
Show newest version
package me.shaftesbury.utils.functional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

/**
 * Created by Bob on 10/12/13.
 */
public final class GenericCollections
{
    public interface Generator
    {
        Collection initialiseEmptyContainer();
        Collection createUnmodifiableContainer(Collection c);
    }

    public static final Generator createArrayListGenerator() { return new GenericCollections().new ArrayListGenerator(); }

    public class ArrayListGenerator implements Generator
    {
        @Override
        public Collection initialiseEmptyContainer() {
            return new ArrayList();
        }

        @Override
        public Collection createUnmodifiableContainer(Collection c) {
            return java.util.Collections.unmodifiableList(Functional.toList(c));
        }
    }

    public static final Generator createHashSetGenerator() { return new GenericCollections().new HashSetGenerator(); }

    public class HashSetGenerator implements Generator
    {
        @Override
        public Collection initialiseEmptyContainer() {
            return new HashSet();
        }

        @Override
        public Collection createUnmodifiableContainer(Collection c) {
            return java.util.Collections.unmodifiableSet(Functional.toSet(c));
        }
    }

    public final static Iterable filter(final Func pred, final Generator generator, final Iterable input)
    {
        final Collection output = generator.initialiseEmptyContainer();
        for(final A element : input)
            if(pred.apply(element))
                output.add(element);

        return generator.createUnmodifiableContainer(output);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy