001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.lib.text;
019:
020: import org.mandarax.kernel.Function;
021: import org.mandarax.kernel.Predicate;
022:
023: /**
024: * Class serving predicates and functions needed for text (String) arithmetic.
025: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
026: * @version 3.4 <7 March 05>
027: * @since 1.4
028: */
029: public class StringArithmetic {
030:
031: // class representing the equals predicate
032: public static class Equal extends BinaryStringPredicate {
033:
034: public String getName() {
035: return "equal";
036: }
037:
038: public boolean evaluate(String s1, String s2) {
039: return s1.equals(s2);
040: }
041: };
042:
043: // class representing the not equals predicate
044: public static class NotEqual extends BinaryStringPredicate {
045:
046: public String getName() {
047: return "not equal";
048: }
049:
050: public boolean evaluate(String s1, String s2) {
051: return !s1.equals(s2);
052: }
053: };
054:
055: // class representing the greater than predicate (w.r.t. the lexicographical order)
056: public static class GreaterThan extends BinaryStringPredicate {
057:
058: public String getName() {
059: return "is greater than";
060: }
061:
062: public boolean evaluate(String s1, String s2) {
063: return 0 < s1.compareTo(s2);
064: }
065: };
066:
067: // class representing the less than predicate (w.r.t. the lexicographical order)
068: public static class LessThan extends BinaryStringPredicate {
069:
070: public String getName() {
071: return "is less than";
072: }
073:
074: public boolean evaluate(String s1, String s2) {
075: return 0 > s1.compareTo(s2);
076: }
077: };
078:
079: // class representing the less than or equals predicate (w.r.t. the lexicographical order)
080: public static class LessThanOrEqual extends BinaryStringPredicate {
081:
082: public String getName() {
083: return "is less than or equals";
084: }
085:
086: public boolean evaluate(String s1, String s2) {
087: return 0 >= s1.compareTo(s2);
088: }
089: };
090:
091: // class representing the greater than or equals predicate (w.r.t. the lexicographical order)
092: public static class GreaterThanOrEqual extends
093: BinaryStringPredicate {
094:
095: public String getName() {
096: return "is greater than or equals";
097: }
098:
099: public boolean evaluate(String s1, String s2) {
100: return 0 <= s1.compareTo(s2);
101: }
102: };
103:
104: // class representing the "text1 contains text2" predicate
105: public static class Contains extends BinaryStringPredicate {
106:
107: public String getName() {
108: return "contains";
109: }
110:
111: public boolean evaluate(String s1, String s2) {
112: return s1.indexOf(s2) > -1;
113: }
114: };
115:
116: // class representing the "text1 ends with text2" predicate
117: public static class EndsWith extends BinaryStringPredicate {
118:
119: public String getName() {
120: return "ends with";
121: }
122:
123: public boolean evaluate(String s1, String s2) {
124: return s1.endsWith(s2);
125: }
126: };
127:
128: // class representing the equal predicate, the case is ignored
129: public static class EqualIgnoreCase extends BinaryStringPredicate {
130:
131: public String getName() {
132: return "equal (ignore case)";
133: }
134:
135: public boolean evaluate(String s1, String s2) {
136: return s1.equalsIgnoreCase(s2);
137: }
138: };
139:
140: // class representing the "text1 starts with text2" predicate
141: public static class StartsWith extends BinaryStringPredicate {
142:
143: public String getName() {
144: return "starts with";
145: }
146:
147: public boolean evaluate(String s1, String s2) {
148: return s1.startsWith(s2);
149: }
150: };
151:
152: // binary predicates
153: public static final Predicate LESS_THAN = new StringArithmetic.LessThan();
154: public static final Predicate GREATER_THAN = new StringArithmetic.GreaterThan();
155: public static final Predicate EQUAL = new StringArithmetic.Equal();
156: public static final Predicate NOT_EQUAL = new StringArithmetic.NotEqual();
157: public static final Predicate LESS_THAN_OR_EQUAL = new StringArithmetic.LessThanOrEqual();
158: public static final Predicate GREATER_THAN_OR_EQUAL = new StringArithmetic.GreaterThanOrEqual();
159: public static final Predicate CONTAINS = new StringArithmetic.Contains();
160: public static final Predicate ENDS_WITH = new StringArithmetic.EndsWith();
161: public static final Predicate EQUAL_IGNORE_CASE = new StringArithmetic.EqualIgnoreCase();
162: public static final Predicate STARTS_WITH = new StringArithmetic.StartsWith();
163:
164: // all functions and predicates
165: public static final Function[] ALL_FUNCTIONS = {};
166: public static final Predicate[] ALL_PREDICATES = { EQUAL,
167: EQUAL_IGNORE_CASE, NOT_EQUAL, LESS_THAN, GREATER_THAN,
168: LESS_THAN_OR_EQUAL, GREATER_THAN_OR_EQUAL, CONTAINS,
169: STARTS_WITH, ENDS_WITH };
170: }
|