/*
 * 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.text;


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

/**
 * Class serving predicates and functions needed for text (String) arithmetic.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.4
 */
public class StringArithmetic {

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

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

        public boolean evaluate(String s1, String s2) {
            return s1.equals (s2);
        }
    }
    ;

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

        public String getName() {
            return "not equal";
        }

        public boolean evaluate(String s1, String s2) {
            return !s1.equals (s2);
        }
    }
    ;

    // class representing the greater than predicate (w.r.t. the lexicographical order)
    public static class GreaterThan extends BinaryStringPredicate {

        public String getName() {
            return "is greater than";
        }

        public boolean evaluate(String s1, String s2) {
            return 0 < s1.compareTo (s2);
        }
    }
    ;

    // class representing the less than predicate (w.r.t. the lexicographical order)
    public static class LessThan extends BinaryStringPredicate {

        public String getName() {
            return "is less than";
        }

        public boolean evaluate(String s1, String s2) {
            return 0 > s1.compareTo (s2);
        }
    }
    ;

    // class representing the less than or equals predicate (w.r.t. the lexicographical order)
    public static class LessThanOrEqual extends BinaryStringPredicate {

        public String getName() {
            return "is less than or equals";
        }

        public boolean evaluate(String s1, String s2) {
            return 0 >= s1.compareTo (s2);
        }
    }
    ;

    // class representing the greater than or equals predicate (w.r.t. the lexicographical order)
    public static class GreaterThanOrEqual extends BinaryStringPredicate {

        public String getName() {
            return "is greater than or equals";
        }

        public boolean evaluate(String s1, String s2) {
            return 0 <= s1.compareTo (s2);
        }
    }
    ;

    // class representing the "text1 contains text2" predicate
    public static class Contains extends BinaryStringPredicate {

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

        public boolean evaluate(String s1, String s2) {
            return s1.indexOf (s2) > -1;
        }
    }
    ;

    // class representing the "text1 ends with text2" predicate
    public static class EndsWith extends BinaryStringPredicate {

        public String getName() {
            return "ends with";
        }

        public boolean evaluate(String s1, String s2) {
            return s1.endsWith (s2);
        }
    }
    ;

    // class representing the equal predicate, the case is ignored
    public static class EqualIgnoreCase extends BinaryStringPredicate {

        public String getName() {
            return "equal (ignore case)";
        }

        public boolean evaluate(String s1, String s2) {
            return s1.equalsIgnoreCase (s2);
        }
    }
    ;

    // class representing the "text1 starts with text2" predicate
    public static class StartsWith extends BinaryStringPredicate {

        public String getName() {
            return "starts with";
        }

        public boolean evaluate(String s1, String s2) {
            return s1.startsWith (s2);
        }
    }
    ;

    // binary predicates
    public static final Predicate LESS_THAN =
        new StringArithmetic.LessThan ();
    public static final Predicate GREATER_THAN =
        new StringArithmetic.GreaterThan ();
    public static final Predicate EQUAL     = new StringArithmetic.Equal ();
    public static final Predicate NOT_EQUAL =
        new StringArithmetic.NotEqual ();
    public static final Predicate LESS_THAN_OR_EQUAL =
        new StringArithmetic.LessThanOrEqual ();
    public static final Predicate GREATER_THAN_OR_EQUAL =
        new StringArithmetic.GreaterThanOrEqual ();
    public static final Predicate CONTAINS  =
        new StringArithmetic.Contains ();
    public static final Predicate ENDS_WITH =
        new StringArithmetic.EndsWith ();
    public static final Predicate EQUAL_IGNORE_CASE =
        new StringArithmetic.EqualIgnoreCase ();
    public static final Predicate STARTS_WITH =
        new StringArithmetic.StartsWith ();

    // all functions and predicates
    public static final Function[]  ALL_FUNCTIONS  = {};
    public static final Predicate[] ALL_PREDICATES = {
        EQUAL, EQUAL_IGNORE_CASE, NOT_EQUAL, LESS_THAN, GREATER_THAN,
        LESS_THAN_OR_EQUAL, GREATER_THAN_OR_EQUAL, CONTAINS, STARTS_WITH,
        ENDS_WITH
    };
}
