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

rx.functions.Functions Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/**
 * Copyright 2014 Netflix, Inc.
 *
 * 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 rx.functions;

public final class Functions {
    private Functions() {
        throw new IllegalStateException("No instances!");
    }

    /**
     * Converts a {@link Func0} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the result type
     * @param f
     *          the {@code Func0} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func0 f) {
        return new FuncN() {

            @Override
            public R call(Object... args) {
                if (args.length != 0) {
                    throw new IllegalArgumentException("Func0 expecting 0 arguments.");
                }
                return f.call();
            }

        };
    }

    /**
     * Converts a {@link Func1} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the result type
     * @param f
     *          the {@code Func1} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func1 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 1) {
                    throw new IllegalArgumentException("Func1 expecting 1 argument.");
                }
                return f.call((T0) args[0]);
            }

        };
    }

    /**
     * Converts a {@link Func2} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the result type
     * @param f
     *          the {@code Func2} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func2 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 2) {
                    throw new IllegalArgumentException("Func2 expecting 2 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1]);
            }

        };
    }

    /**
     * Converts a {@link Func3} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param  the result type
     * @param f
     *          the {@code Func3} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func3 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 3) {
                    throw new IllegalArgumentException("Func3 expecting 3 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1], (T2) args[2]);
            }

        };
    }

    /**
     * Converts a {@link Func4} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param  the fourth argument type
     * @param  the result type
     * @param f
     *          the {@code Func4} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func4 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 4) {
                    throw new IllegalArgumentException("Func4 expecting 4 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3]);
            }

        };
    }

    /**
     * Converts a {@link Func5} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param  the fourth argument type
     * @param  the fifth argument type
     * @param  the result type
     * @param f
     *          the {@code Func5} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func5 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 5) {
                    throw new IllegalArgumentException("Func5 expecting 5 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4]);
            }

        };
    }

    /**
     * Converts a {@link Func6} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param  the fourth argument type
     * @param  the fifth argument type
     * @param  the sixth argument type
     * @param  the result type
     * @param f
     *          the {@code Func6} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func6 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 6) {
                    throw new IllegalArgumentException("Func6 expecting 6 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5]);
            }

        };
    }

    /**
     * Converts a {@link Func7} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param  the fourth argument type
     * @param  the fifth argument type
     * @param  the sixth argument type
     * @param  the seventh argument type
     * @param  the result type
     * @param f
     *          the {@code Func7} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func7 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 7) {
                    throw new IllegalArgumentException("Func7 expecting 7 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6]);
            }

        };
    }

    /**
     * Converts a {@link Func8} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param  the fourth argument type
     * @param  the fifth argument type
     * @param  the sixth argument type
     * @param  the seventh argument type
     * @param  the eighth argument type
     * @param  the result type
     * @param f
     *          the {@code Func8} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func8 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 8) {
                    throw new IllegalArgumentException("Func8 expecting 8 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6], (T7) args[7]);
            }

        };
    }

    /**
     * Converts a {@link Func9} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param  the fourth argument type
     * @param  the fifth argument type
     * @param  the sixth argument type
     * @param  the seventh argument type
     * @param  the eighth argument type
     * @param  the ninth argument type
     * @param  the result type
     * @param f
     *          the {@code Func9} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromFunc(final Func9 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public R call(Object... args) {
                if (args.length != 9) {
                    throw new IllegalArgumentException("Func9 expecting 9 arguments.");
                }
                return f.call((T0) args[0], (T1) args[1], (T2) args[2], (T3) args[3], (T4) args[4], (T5) args[5], (T6) args[6], (T7) args[7], (T8) args[8]);
            }

        };
    }

    /**
     * Converts an {@link Action0} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param f
     *          the {@code Action0} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static FuncN fromAction(final Action0 f) {
        return new FuncN() {

            @Override
            public Void call(Object... args) {
                if (args.length != 0) {
                    throw new IllegalArgumentException("Action0 expecting 0 arguments.");
                }
                f.call();
                return null;
            }

        };
    }

    /**
     * Converts an {@link Action1} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param f
     *          the {@code Action1} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromAction(final Action1 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public Void call(Object... args) {
                if (args.length != 1) {
                    throw new IllegalArgumentException("Action1 expecting 1 argument.");
                }
                f.call((T0) args[0]);
                return null;
            }

        };
    }

    /**
     * Converts an {@link Action2} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param f
     *          the {@code Action2} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromAction(final Action2 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public Void call(Object... args) {
                if (args.length != 2) {
                    throw new IllegalArgumentException("Action3 expecting 2 arguments.");
                }
                f.call((T0) args[0], (T1) args[1]);
                return null;
            }

        };
    }

    /**
     * Converts an {@link Action3} to a {@link FuncN} to allow heterogeneous handling of functions with different arities.
     *
     * @param  the first argument type
     * @param  the second argument type
     * @param  the third argument type
     * @param f
     *          the {@code Action3} to convert
     * @return a {@link FuncN} representation of {@code f}
     */
    public static  FuncN fromAction(final Action3 f) {
        return new FuncN() {

            @SuppressWarnings("unchecked")
            @Override
            public Void call(Object... args) {
                if (args.length != 3) {
                    throw new IllegalArgumentException("Action3 expecting 3 arguments.");
                }
                f.call((T0) args[0], (T1) args[1], (T2) args[2]);
                return null;
            }

        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy