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

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

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

/**
 * Created by Bob on 16/05/14.
 *
 * Common use patterns written here so that we don't have to wrestle continuously with the clunkiness and waffle that is the Java syntax.
 */
public final class FunctionalHelper
{
    /**
     * Given an input sequence of {@link me.shaftesbury.utils.functional.Option} types return a new lazily-evaluated sequence of those which are {@link me.shaftesbury.utils.functional.Option#Some()}
     * @param input the input sequence
     * @param  the type of the element underlying the {@link me.shaftesbury.utils.functional.Option}
     * @return a sequence of {@link me.shaftesbury.utils.functional.Option} which are {@link me.shaftesbury.utils.functional.Option#Some()}
     */
    public static Iterable2> areSome(final Iterable> input)
    {
        return IterableHelper.create(input).filter(new Func, Boolean>() {
            @Override
            public Boolean apply(Option tOption) {
                return tOption.isSome();
            }
        });
    }
    /**
     * Given an input sequence of {@link me.shaftesbury.utils.functional.Option} types return a new lazily-evaluated sequence of those which are {@link me.shaftesbury.utils.functional.Option#None()}
     * @param input the input sequence
     * @param  the type of the element underlying the {@link me.shaftesbury.utils.functional.Option}
     * @return a sequence of {@link me.shaftesbury.utils.functional.Option} which are {@link me.shaftesbury.utils.functional.Option#None()}
     */
    public static Iterable2> areNone(final Iterable> input)
    {
        return IterableHelper.create(input).filter(new Func, Boolean>() {
            @Override
            public Boolean apply(Option tOption) {
                return tOption.isNone();
            }
        });
    }
    /**
     * Given an input sequence of {@link me.shaftesbury.utils.functional.Option} types return true if all the elements are {@link me.shaftesbury.utils.functional.Option#Some()}
     * @param input the input sequence
     * @param  the type of the element underlying the {@link me.shaftesbury.utils.functional.Option}
     * @return true if all the elements are {@link me.shaftesbury.utils.functional.Option#Some()}, false otherwise
     */
    public static boolean allSome(final Iterable> input)
    {
        return !IterableHelper.create(input).exists(new Func, Boolean>() {
            @Override
            public Boolean apply(Option tOption) {
                return tOption.isNone();
            }
        });
    }
    /**
     * Given an input sequence of {@link me.shaftesbury.utils.functional.Option} types return true if all the elements are {@link me.shaftesbury.utils.functional.Option#None()}
     * @param input the input sequence
     * @param  the type of the element underlying the {@link me.shaftesbury.utils.functional.Option}
     * @return true if all the elements are {@link me.shaftesbury.utils.functional.Option#None()}}, false otherwise
     */
    public static boolean allNone(final Iterable> input)
    {
        return !IterableHelper.create(input).exists(new Func, Boolean>() {
            @Override
            public Boolean apply(Option tOption) {
                return tOption.isSome();
            }
        });
    }

    /**
     * Given an input sequence of {@link me.shaftesbury.utils.functional.Option} return a lazily-evaluated sequence containing
     * the underlying data elements. Note that the proper way to perform this action is to use
     * {@link me.shaftesbury.utils.functional.Option#bind(Func)}
     * @param input the input sequence
     * @param  the type of the underlying data
     * @return a lazily-evaluated sequence containing the underlying data
     * @throws me.shaftesbury.utils.functional.OptionNoValueAccessException if {@link Option#isNone()} is true for any element in input
     */
    public static Iterable2 some(final Iterable> input)
    {
        return IterableHelper.create(input).map(new Func, T>() {
            @Override
            public T apply(Option tOption) {
                return tOption.Some();
            }
        });
    }

    private FunctionalHelper(){}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy