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

ix.internal.util.IxHelperFunctions Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright 2011-2016 David Karnok
 *
 * 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 ix.internal.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;

import rx.functions.Func0;
import rx.functions.Func1;
import rx.functions.Func2;

/**
 * Helper class with function types.
 */
public final class IxHelperFunctions {
    /**
     * Creates a function which returns always the same value.
     * @param  the parameter type, irrelevant
     * @param  the value type to return
     * @param value the value to return
     * @return the function
     */
    public static  Func1 constant(final Result value) {
        return new Func1() {
            @Override
            public Result call(Param1 param1) {
                return value;
            }
        };
    }
    /**
     * Creates a function which returns always the same value.
     * @param  the value type to return
     * @param value the value to return
     * @return the function
     */
    public static  Func0 constant0(final T value) {
        return new Func0() {
            @Override
            public T call() {
                return value;
            }
        };
    }
    
    /**
     * Returns a function which returns the greater of its parameters.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @return the function
     */
    public static > Func2 max() {
        return new Func2() {
            @Override
            public T call(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return param1.compareTo(param2) < 0 ? param2 : param1;
            }
        };
    }
    /**
     * Returns a function which returns the smaller of its parameters.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @return the function
     */
    public static > Func2 min() {
        return new Func2() {
            @Override
            public T call(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return param1.compareTo(param2) > 0 ? param2 : param1;
            }
        };
    }
    /** A helper function which returns its first parameter. */
    private static final Func2 IDENTITY_FIRST = new Func2() {
        @Override
        public Object call(Object param1, Object param2) {
            return param1;
        }
    };
    /** A helper function which returns its second parameter. */
    private static final Func2 IDENTITY_SECOND = new Func2() {
        @Override
        public Object call(Object param1, Object param2) {
            return param2;
        }
    };
    /**
     * Returns a helper function of two parameters which always returns its first parameter.
     * @param  the result and the first parameter type
     * @param  the second parameter type, irrelevant
     * @return the function
     */
    @SuppressWarnings("unchecked")
    public static  Func2 identityFirst() {
        return (Func2)IDENTITY_FIRST;
    }
    /**
     * Returns a helper function of two parameters which always returns its second parameter.
     * @param  the result and the second parameter type
     * @param  the first parameter type, irrelevant
     * @return the function
     */
    @SuppressWarnings("unchecked")
    public static  Func2 identitySecond() {
        return (Func2)IDENTITY_SECOND;
    }
    /**
     * Returns a function which returns the greater of its parameters in respect to the supplied Comparator.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @param comparator the value comparator
     * @return the function
     */
    public static  Func2 max(
            final Comparator comparator) {
        return new Func2() {
            @Override
            public T call(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return comparator.compare(param1, param2) < 0 ? param2 : param1;
            }
        };
    }
    /**
     * Returns a function which returns the smaller of its parameters in respect to the supplied Comparator.
     * If only one of the parameters is null, the other parameter is returned.
     * If both parameters are null, null is returned.
     * @param  the parameter types, which must be self-comparable
     * @param comparator the value comparator
     * @return the function
     */
    public static  Func2 min(
            final Comparator comparator) {
        return new Func2() {
            @Override
            public T call(T param1, T param2) {
                if (param1 == null || param2 == null) {
                    if (param2 == null) {
                        return param1;
                    }
                    return param2;
                }
                return comparator.compare(param1, param2) > 0 ? param2 : param1;
            }
        };
    }
    /**
     * Returns a convenience comparator which basically compares
     * objects which implement the Comparable
     * interface. The comparator is null safe in the manner,
     * that nulls are always less than any non-nulls.
     * To have a comparator which places nulls last, use the comparator0() method.
     * @param  the element types to compare
     * @return the comparator
     * @see IxHelperFunctions#comparator0()
     */
    public static > Comparator comparator() {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                if (o1 == null) {
                    if (o2 == null) {
                        return 0;
                    }
                    return -1;
                }
                if (o2 == null) {
                    return 1;
                }
                return o1.compareTo(o2);
            }
        };
    }
    /**
     * Returns a convenience comparator which basically compares objects which implement the Comparable
     * interface. The comparator is null safe in the manner, that nulls are always greater than any non-nulls.
     * To have a comparator which places nulls first, use the comparator() method.
     * @param  the element types to compare
     * @return the comparator
     */
    public static > Comparator comparator0() {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                if (o1 == null) {
                    if (o2 == null) {
                        return 0;
                    }
                    return 1;
                }
                if (o2 == null) {
                    return -1;
                }
                return o1.compareTo(o2);
            }
        };
    }
    /**
     * Creates a new comparator which reverses the order of the comparison.
     * @param  the element type, which must be self comparable
     * @return the new comparator
     */
    public static > Comparator comparatorReverse() {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                return o2.compareTo(o1);
            }
        };
    }
    /**
     * Creates a new comparator which reverses the order produced by the given
     * normal comparator.
     * @param  the element type
     * @param normal the normal comparator
     * @return the new comparator
     */
    public static  Comparator comparatorReverse(
            final Comparator normal) {
        return new Comparator() {
            @Override
            public int compare(T o1, T o2) {
                return normal.compare(o2, o1);
            }
        };
    }
    /** Function to sum integers in aggregators. */
    static final Func2 SUM_INTEGER = new Func2() {
        @Override
        public Integer call(Integer param1, Integer param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    static final Func2 SUM_FLOAT = new Func2() {
        @Override
        public Float call(Float param1, Float param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    static final Func2 SUM_DOUBLE = new Func2() {
        @Override
        public Double call(Double param1, Double param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    static final Func2 SUM_LONG = new Func2() {
        @Override
        public Long call(Long param1, Long param2) {
            return param1 != null ? param1 + param2 : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    static final Func2 SUM_BIGINTEGER = new Func2() {
        @Override
        public BigInteger call(BigInteger param1, BigInteger param2) {
            return param1 != null ? param1.add(param2) : param2;
        }
    };
    /** Function to sum integers in aggregators. */
    static final Func2 SUM_BIGDECIMAL = new Func2() {
        @Override
        public BigDecimal call(BigDecimal param1, BigDecimal param2) {
            return param1 != null ? param1.add(param2) : param2;
        }
    };
    /**
     * Retuns a function that adds two BigDecimal numbers and
     * returns a new one.
     * 

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ public static Func2 sumBigDecimal() { return SUM_BIGDECIMAL; } /** * Retuns a function that adds two BigInteger numbers and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ public static Func2 sumBigInteger() { return SUM_BIGINTEGER; } /** * Retuns a function that adds two Double number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ public static Func2 sumDouble() { return SUM_DOUBLE; } /** * Retuns a function that adds two Float number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ public static Func2 sumFloat() { return SUM_FLOAT; } /** * Retuns a function that adds two Integer number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ public static Func2 sumInteger() { return SUM_INTEGER; } /** * Retuns a function that adds two Long number and * returns a new one. *

If the first parameter is null, it returns the second parameter.

* @return Function to sum integers in aggregators. */ public static Func2 sumLong() { return SUM_LONG; } /** * A list creator factory. * @param the value type * @return a function which creates a new empty instance of the given concrete list implementation. */ public static Func0> arrayListProvider() { return new Func0>() { @Override public ArrayList call() { return new ArrayList(); } }; } /** * A list creator factory for Func1 that ignores the parameter. * @param the value type * @param the function parameter type, ignored * @return a function which creates a new empty instance of * the given concrete list implementation. */ public static Func1> arrayListProvider1() { return new Func1>() { @Override public ArrayList call(U ignored) { return new ArrayList(); } }; } /** * A list creator factory. * @param the value type * @return a function which creates a new empty instance of the given concrete list implementation. */ public static Func0> linkedListProvider() { return new Func0>() { @Override public LinkedList call() { return new LinkedList(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. */ public static Func0> hashMapProvider() { return new Func0>() { @Override public HashMap call() { return new HashMap(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. */ public static Func0> treeMapProvider() { return new Func0>() { @Override public TreeMap call() { return new TreeMap(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @param keyComparator the key comparator function * @return a function which creates a new empty instance of the given concrete map implementation. */ public static Func0> treeMapProvider(final Comparator keyComparator) { return new Func0>() { @Override public TreeMap call() { return new TreeMap(keyComparator); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. */ public static Func0> linkedHashMapProvider() { return new Func0>() { @Override public LinkedHashMap call() { return new LinkedHashMap(); } }; } /** * A map creator factory. * @param the key type * @param the value type * @return a function which creates a new empty instance of the given concrete map implementation. */ public static Func0> concurrentHashMapProvider() { return new Func0>() { @Override public ConcurrentHashMap call() { return new ConcurrentHashMap(); } }; } /** * A set creation provider. * @param the element type * @return the function which creates an empty instance of the set */ public static Func0> hashSetProvider() { return new Func0>() { @Override public HashSet call() { return new HashSet(); } }; } /** * A set creation provider. * @param the element type * @return the function which creates an empty instance of the set */ public static Func0> treeSetProvider() { return new Func0>() { @Override public TreeSet call() { return new TreeSet(); } }; } /** * A set creation provider. * @param the element type * @param elementComparator the custom element comparator * @return the function which creates an empty instance of the set */ public static Func0> treeSetProvider(final Comparator elementComparator) { return new Func0>() { @Override public TreeSet call() { return new TreeSet(elementComparator); } }; } /** * @return Returns a function that negates the incoming boolean value. */ public static Func1 negate() { return new Func1() { @Override public Boolean call(Boolean param1) { return param1 == Boolean.TRUE ? Boolean.FALSE : Boolean.TRUE; } }; } /** A function that returns its parameter. */ private static final Func1 IDENTITY = new Func1() { @Override public Object call(Object t1) { return t1; }; }; /** * @param the input and result type * @return a function which returns the single parameter value as its result. */ @SuppressWarnings("unchecked") public static Func1 identity() { return (Func1)IDENTITY; } /** Utility class. */ private IxHelperFunctions() { } }