/*
 * 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 int 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 IntArithmetic {

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

    // unary functions
    public static final Function ABS = new IntArithmetic.Abs ();

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

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

    // all functions and predicates
    public static final Function[]  ALL_FUNCTIONS  = {
        MAX, MIN, PLUS, MINUS, TIMES, POW, ABS
    };
    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 BinaryIntArithmeticFunction {

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

        public int compute(int x1, int x2) {
            return Math.max (x1, x2);
        }
    }

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

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

        public int compute(int x1, int x2) {
            return Math.min (x1, x2);
        }
    }

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

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

        public int compute(int x1, int x2) {
            return x1 + x2;
        }
    }

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

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

        public int compute(int x1, int x2) {
            return x1 * x2;
        }
    }

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

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

        public int compute(int x1, int x2) {
            return x1 - x2;
        }
    }

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

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

        public int compute(int x1, int x2) {
            return x1 ^ x2;
        }
    }

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

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

        public int compute(int x) {
            return Math.abs (x);
        }
    }

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

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

        public boolean evaluate(int x1, int x2) {
            return x1 == x2;
        }
    }

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

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

        public boolean evaluate(int x1, int x2) {
            return x1 != x2;
        }
    }

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

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

        public boolean evaluate(int x1, int x2) {
            return x1 > x2;
        }
    }

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

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

        public boolean evaluate(int x1, int x2) {
            return x1 < x2;
        }
    }

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

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

        public boolean evaluate(int x1, int x2) {
            return(x1 < x2) || (x1 == x2);
        }
    }

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

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

        public boolean evaluate(int x1, int x2) {
            return(x1 > x2) || (x1 == x2);
        }
    }

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

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

        public boolean evaluate(int x1, int x2, int x3) {
            return(x1 < x2) && (x2 < x3);
        }
    }

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

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

        public boolean evaluate(int x1, int x2, int x3) {
            return(x1 <= x2) && (x2 <= x3);
        }
    }
}
