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

hu.akarnokd.rxjava2.internal.functions.Functions Maven / Gradle / Ivy

/**
 * Copyright 2015 David Karnok and 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 hu.akarnokd.rxjava2.internal.functions;

import java.util.Comparator;

import hu.akarnokd.rxjava2.functions.*;

/**
 * Utility methods to convert the Function3..Function9 instances to Function of Object array.
 */
public enum Functions {
    ;
    
    @SuppressWarnings("unchecked")
    public static  Function toFunction(final BiFunction biFunction) {
        Objects.requireNonNull(biFunction, "biFunction is null");
        return new Function() {
            @Override
            public R apply(Object[] a) {
                if (a.length != 2) {
                    throw new IllegalArgumentException("Array of size 2 expected but got " + a.length);
                }
                return ((BiFunction)biFunction).apply(a[0], a[1]);
            }
        };
    }
    
    public static  Function toFunction(final Function3 f) {
        Objects.requireNonNull(f, "f is null");
        return new Function() {
            @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) {
                if (a.length != 3) {
                    throw new IllegalArgumentException("Array of size 3 expected but got " + a.length);
                }
                return f.apply((T1)a[0], (T2)a[1], (T3)a[2]);
            }
        };
    }
    
    public static  Function toFunction(final Function4 f) {
        Objects.requireNonNull(f, "f is null");
        return new Function() {
            @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) {
                if (a.length != 4) {
                    throw new IllegalArgumentException("Array of size 4 expected but got " + a.length);
                }
                return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3]);
            }
        };
    }
    
    public static  Function toFunction(final Function5 f) {
        Objects.requireNonNull(f, "f is null");
        return new Function() {
            @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) {
                if (a.length != 5) {
                    throw new IllegalArgumentException("Array of size 5 expected but got " + a.length);
                }
                return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4]);
            }
        };
    }
    
    public static  Function toFunction(
            final Function6 f) {
        Objects.requireNonNull(f, "f is null");
        return new Function() {
            @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) {
                if (a.length != 6) {
                    throw new IllegalArgumentException("Array of size 6 expected but got " + a.length);
                }
                return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5]);
            }
        };
    }
    
    public static  Function toFunction(
            final Function7 f) {
        Objects.requireNonNull(f, "f is null");
        return new Function() {
            @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) {
                if (a.length != 7) {
                    throw new IllegalArgumentException("Array of size 7 expected but got " + a.length);
                }
                return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5], (T7)a[6]);
            }
        };
    }
    
    public static  Function toFunction(
            final Function8 f) {
        Objects.requireNonNull(f, "f is null");
        return new Function() {
            @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) {
                if (a.length != 8) {
                    throw new IllegalArgumentException("Array of size 8 expected but got " + a.length);
                }
                return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5], (T7)a[6], (T8)a[7]);
            }
        };
    }
    
    public static  Function toFunction(
            final Function9 f) {
        Objects.requireNonNull(f, "f is null");
        return new Function() {
            @SuppressWarnings("unchecked")
            @Override
            public R apply(Object[] a) {
                if (a.length != 9) {
                    throw new IllegalArgumentException("Array of size 9 expected but got " + a.length);
                }
                return f.apply((T1)a[0], (T2)a[1], (T3)a[2], (T4)a[3], (T5)a[4], (T6)a[5], (T7)a[6], (T8)a[7], (T9)a[8]);
            }
        };
    }
    
    /** A singleton identity function. */
    static final Function IDENTITY = new Function() {
        @Override
        public Object apply(Object v) {
            return v;
        }
    };
    
    /**
     * Returns an identity function that simply returns its argument.
     * @param  the input and output value type
     * @return the identity function
     */
    @SuppressWarnings("unchecked")
    public static  Function identity() {
        return (Function)IDENTITY;
    }
    
    static final Runnable EMPTY = new Runnable() {
        @Override
        public void run() { }
    };
    
    /**
     * Returns an empty runnable that does nothing.
     * @return an empty runnable that does nothing
     */
    public static Runnable emptyRunnable() {
        return EMPTY;
    }
    
    static final Consumer EMPTY_CONSUMER = new Consumer() {
        @Override
        public void accept(Object v) { }
    };
    
    /**
     * Returns an empty consumer that does nothing.
     * @param  the consumed value type, the value is ignored
     * @return an empty consumer that does nothing.
     */
    @SuppressWarnings("unchecked")
    public static  Consumer emptyConsumer() {
        return (Consumer)EMPTY_CONSUMER;
    }
    
    static final LongConsumer EMPTY_LONGCONSUMER = new LongConsumer() {
        @Override
        public void accept(long v) { }
    };
    
    /**
     * Returns an empty long consumer that does nothing.
     * @return an empty long consumer that does nothing.
     */
    public static LongConsumer emptyLongConsumer() {
        return EMPTY_LONGCONSUMER;
    }
    
    static final Predicate ALWAYS_TRUE = new Predicate() {
        @Override
        public boolean test(Object o) {
            return true;
        }
    };

    static final Predicate ALWAYS_FALSE = new Predicate() {
        @Override
        public boolean test(Object o) {
            return true;
        }
    };
    
    static final Supplier NULL_SUPPLIER = new Supplier() {
        @Override
        public Object get() {
            return null;
        }
    };
    
    static final Comparator NATURAL_COMPARATOR = new Comparator() {
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @Override
        public int compare(Object a, Object b) {
            return ((Comparable)a).compareTo(b);
        }
    };
    
    @SuppressWarnings("unchecked")
    public static  Predicate alwaysTrue() {
        return (Predicate)ALWAYS_TRUE;
    }

    @SuppressWarnings("unchecked")
    public static  Predicate alwaysFalse() {
        return (Predicate)ALWAYS_FALSE;
    }

    @SuppressWarnings("unchecked")
    public static  Supplier nullSupplier() {
        return (Supplier)NULL_SUPPLIER;
    }
    
    /**
     * Returns a natural order comparator which casts the parameters to Comparable.
     * @param  the value type
     * @return a natural order comparator which casts the parameters to Comparable
     */
    @SuppressWarnings("unchecked")
    public static  Comparator naturalOrder() {
        return (Comparator)NATURAL_COMPARATOR;
    }
}