/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.lib.math;


import org.mandarax.kernel.Function;
import org.mandarax.kernel.Predicate;

/**
 * Class serving predicates and functions needed for double arithmetic.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.4  (in 1.3 in the renamed package org.mandarax.math)
 */
public class DoubleArithmetic {

    // binary functions
    public static final Function MAX   = new DoubleArithmetic.Max ();
    public static final Function MIN   = new DoubleArithmetic.Min ();
    public static final Function PLUS  = new DoubleArithmetic.Plus ();
    public static final Function MINUS = new DoubleArithmetic.Minus ();
    public static final Function TIMES = new DoubleArithmetic.Times ();
    public static final Function DIV   = new DoubleArithmetic.Div ();
    public static final Function AVG   = new DoubleArithmetic.Avg ();
    public static final Function POW   = new DoubleArithmetic.Pow ();

    // unary functions
    public static final Function SQRT  = new DoubleArithmetic.Sqrt ();
    public static final Function ABS   = new DoubleArithmetic.Abs ();
    public static final Function FLOOR = new DoubleArithmetic.Floor ();
    public static final Function CEIL  = new DoubleArithmetic.Ceil ();
    public static final Function ROUND = new DoubleArithmetic.Round ();

    // binary predicates
    public static final Predicate LESS_THAN =
        new DoubleArithmetic.LessThan ();
    public static final Predicate GREATER_THAN =
        new DoubleArithmetic.GreaterThan ();
    public static final Predicate EQUAL     = new DoubleArithmetic.Equal ();
    public static final Predicate NOT_EQUAL =
        new DoubleArithmetic.NotEqual ();
    public static final Predicate LESS_THAN_OR_EQUAL =
        new DoubleArithmetic.LessThanOrEqual ();
    public static final Predicate GREATER_THAN_OR_EQUAL =
        new DoubleArithmetic.GreaterThanOrEqual ();

    // ternary predicates
    public static final Predicate INCLUSIVE_BETWEEN =
        new DoubleArithmetic.InclusiveBetween ();
    public static final Predicate EXCLUSIVE_BETWEEN =
        new DoubleArithmetic.ExclusiveBetween ();

    // all functions and predicates
    public static final Function[] ALL_FUNCTIONS = {
        MAX, MIN, PLUS, MINUS, TIMES, DIV, AVG, POW, SQRT, ABS, FLOOR, CEIL,
        ROUND
    };
    public static final Predicate[] ALL_PREDICATES = {
        EQUAL, NOT_EQUAL, LESS_THAN, GREATER_THAN, LESS_THAN_OR_EQUAL,
        GREATER_THAN_OR_EQUAL, INCLUSIVE_BETWEEN, EXCLUSIVE_BETWEEN
    };

    // class representing the max function
    public static class Max extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "max";
        }

        public double compute(double d1, double d2) {
            return Math.max (d1, d2);
        }
    }

    // class representing the min function
    public static class Min extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "min";
        }

        public double compute(double d1, double d2) {
            return Math.min (d1, d2);
        }
    }

    // class representing the plus function
    public static class Plus extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "+";
        }

        public double compute(double d1, double d2) {
            return d1 + d2;
        }
    }

    // class representing the times function
    public static class Times extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "*";
        }

        public double compute(double d1, double d2) {
            return d1 * d2;
        }
    }

    // class representing the minus function
    public static class Minus extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "-";
        }

        public double compute(double d1, double d2) {
            return d1 - d2;
        }
    }

    // class representing the divide by function
    public static class Div extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "/";
        }

        public double compute(double d1, double d2) {
            return d1 / d2;
        }
    }

    // class representing the average function
    public static class Avg extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "avg";
        }

        public double compute(double d1, double d2) {
            return(d1 + d2) / 2;
        }
    }

    // class representing the to the power of function
    public static class Pow extends BinaryDoubleArithmeticFunction {

        public String getName() {
            return "pow";
        }

        public double compute(double d1, double d2) {
            return Math.pow (d1, d2);
        }
    }

    // class representing the square root function
    public static class Sqrt extends UnaryDoubleArithmeticFunction {

        public String getName() {
            return "sqrt";
        }

        public double compute(double d) {
            return Math.sqrt (d);
        }
    }

    // class representing the floor function
    public static class Floor extends UnaryDoubleArithmeticFunction {

        public String getName() {
            return "floor";
        }

        public double compute(double d) {
            return Math.floor (d);
        }
    }

    // class representing the ceil function
    public static class Ceil extends UnaryDoubleArithmeticFunction {

        public String getName() {
            return "ceil";
        }

        public double compute(double d) {
            return Math.ceil (d);
        }
    }

    // class representing the round function
    public static class Round extends UnaryDoubleArithmeticFunction {

        public String getName() {
            return "round";
        }

        public double compute(double d) {
            return Math.round (d);
        }
    }

    // class representing the abs function
    public static class Abs extends UnaryDoubleArithmeticFunction {

        public String getName() {
            return "abs";
        }

        public double compute(double d) {
            return Math.abs (d);
        }
    }

    // class representing the equals predicate
    public static class Equal extends BinaryDoubleArithmeticPredicate {

        public String getName() {
            return "=";
        }

        public boolean evaluate(double d1, double d2) {
            return d1 == d2;
        }
    }

    // class representing the equals not predicate
    public static class NotEqual extends BinaryDoubleArithmeticPredicate {

        public String getName() {
            return "!=";
        }

        public boolean evaluate(double d1, double d2) {
            return d1 != d2;
        }
    }

    // class representing the greater than predicate
    public static class GreaterThan extends BinaryDoubleArithmeticPredicate {

        public String getName() {
            return ">";
        }

        public boolean evaluate(double d1, double d2) {
            return d1 > d2;
        }
    }

    // class representing the less than predicate
    public static class LessThan extends BinaryDoubleArithmeticPredicate {

        public String getName() {
            return "<";
        }

        public boolean evaluate(double d1, double d2) {
            return d1 < d2;
        }
    }

    // class representing the less than or equals predicate
    public static class LessThanOrEqual
            extends BinaryDoubleArithmeticPredicate {

        public String getName() {
            return "=<";
        }

        public boolean evaluate(double d1, double d2) {
            return(d1 < d2) || (d1 == d2);
        }
    }

    // class representing the greater than or equals predicate
    public static class GreaterThanOrEqual
            extends BinaryDoubleArithmeticPredicate {

        public String getName() {
            return "=>";
        }

        public boolean evaluate(double d1, double d2) {
            return(d1 > d2) || (d1 == d2);
        }
    }

    // class representing the exclusive between predicate
    public static class ExclusiveBetween
            extends TernaryDoubleArithmeticPredicate {

        public String getName() {
            return "between (excl.)";
        }

        public boolean evaluate(double d1, double d2, double d3) {
            return(d1 < d2) && (d2 < d3);
        }
    }

    // class representing the inclusive between predicate
    public static class InclusiveBetween
            extends TernaryDoubleArithmeticPredicate {

        public String getName() {
            return "between (incl.)";
        }

        public boolean evaluate(double d1, double d2, double d3) {
            return(d1 <= d2) && (d2 <= d3);
        }
    }
}
