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.math;
019:
020: import org.mandarax.kernel.Function;
021: import org.mandarax.kernel.Predicate;
022:
023: /**
024: * Class serving predicates and functions needed for int 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 (in 1.3 in the renamed package org.mandarax.math)
028: */
029: public class IntArithmetic {
030:
031: // binary functions
032: public static final Function MAX = new IntArithmetic.Max();
033: public static final Function MIN = new IntArithmetic.Min();
034: public static final Function PLUS = new IntArithmetic.Plus();
035: public static final Function MINUS = new IntArithmetic.Minus();
036: public static final Function TIMES = new IntArithmetic.Times();
037: public static final Function POW = new IntArithmetic.Pow();
038:
039: // unary functions
040: public static final Function ABS = new IntArithmetic.Abs();
041:
042: // binary predicates
043: public static final Predicate LESS_THAN = new IntArithmetic.LessThan();
044: public static final Predicate GREATER_THAN = new IntArithmetic.GreaterThan();
045: public static final Predicate EQUAL = new IntArithmetic.Equal();
046: public static final Predicate NOT_EQUAL = new IntArithmetic.NotEqual();
047: public static final Predicate LESS_THAN_OR_EQUAL = new IntArithmetic.LessThanOrEqual();
048: public static final Predicate GREATER_THAN_OR_EQUAL = new IntArithmetic.GreaterThanOrEqual();
049:
050: // ternary predicates
051: public static final Predicate INCLUSIVE_BETWEEN = new IntArithmetic.InclusiveBetween();
052: public static final Predicate EXCLUSIVE_BETWEEN = new IntArithmetic.ExclusiveBetween();
053:
054: // all functions and predicates
055: public static final Function[] ALL_FUNCTIONS = { MAX, MIN, PLUS,
056: MINUS, TIMES, POW, ABS };
057: public static final Predicate[] ALL_PREDICATES = { EQUAL,
058: NOT_EQUAL, LESS_THAN, GREATER_THAN, LESS_THAN_OR_EQUAL,
059: GREATER_THAN_OR_EQUAL, INCLUSIVE_BETWEEN, EXCLUSIVE_BETWEEN };
060:
061: // class representing the max function
062: public static class Max extends BinaryIntArithmeticFunction {
063:
064: public String getName() {
065: return "max";
066: }
067:
068: public int compute(int x1, int x2) {
069: return Math.max(x1, x2);
070: }
071: }
072:
073: // class representing the min function
074: public static class Min extends BinaryIntArithmeticFunction {
075:
076: public String getName() {
077: return "min";
078: }
079:
080: public int compute(int x1, int x2) {
081: return Math.min(x1, x2);
082: }
083: }
084:
085: // class representing the plus function
086: public static class Plus extends BinaryIntArithmeticFunction {
087:
088: public String getName() {
089: return "+";
090: }
091:
092: public int compute(int x1, int x2) {
093: return x1 + x2;
094: }
095: }
096:
097: // class representing the times function
098: public static class Times extends BinaryIntArithmeticFunction {
099:
100: public String getName() {
101: return "*";
102: }
103:
104: public int compute(int x1, int x2) {
105: return x1 * x2;
106: }
107: }
108:
109: // class representing the minus function
110: public static class Minus extends BinaryIntArithmeticFunction {
111:
112: public String getName() {
113: return "-";
114: }
115:
116: public int compute(int x1, int x2) {
117: return x1 - x2;
118: }
119: }
120:
121: // class representing the to the power of function
122: public static class Pow extends BinaryIntArithmeticFunction {
123:
124: public String getName() {
125: return "pow";
126: }
127:
128: public int compute(int x1, int x2) {
129: return x1 ^ x2;
130: }
131: }
132:
133: // class representing the abs function
134: public static class Abs extends UnaryIntArithmeticFunction {
135:
136: public String getName() {
137: return "abs";
138: }
139:
140: public int compute(int x) {
141: return Math.abs(x);
142: }
143: }
144:
145: // class representing the equals predicate
146: public static class Equal extends BinaryIntArithmeticPredicate {
147:
148: public String getName() {
149: return "=";
150: }
151:
152: public boolean evaluate(int x1, int x2) {
153: return x1 == x2;
154: }
155: }
156:
157: // class representing the equals not predicate
158: public static class NotEqual extends BinaryIntArithmeticPredicate {
159:
160: public String getName() {
161: return "!=";
162: }
163:
164: public boolean evaluate(int x1, int x2) {
165: return x1 != x2;
166: }
167: }
168:
169: // class representing the greater than predicate
170: public static class GreaterThan extends
171: BinaryIntArithmeticPredicate {
172:
173: public String getName() {
174: return ">";
175: }
176:
177: public boolean evaluate(int x1, int x2) {
178: return x1 > x2;
179: }
180: }
181:
182: // class representing the less than predicate
183: public static class LessThan extends BinaryIntArithmeticPredicate {
184:
185: public String getName() {
186: return "<";
187: }
188:
189: public boolean evaluate(int x1, int x2) {
190: return x1 < x2;
191: }
192: }
193:
194: // class representing the less than or equals predicate
195: public static class LessThanOrEqual extends
196: BinaryIntArithmeticPredicate {
197:
198: public String getName() {
199: return "=<";
200: }
201:
202: public boolean evaluate(int x1, int x2) {
203: return (x1 < x2) || (x1 == x2);
204: }
205: }
206:
207: // class representing the greater than or equals predicate
208: public static class GreaterThanOrEqual extends
209: BinaryIntArithmeticPredicate {
210:
211: public String getName() {
212: return "=>";
213: }
214:
215: public boolean evaluate(int x1, int x2) {
216: return (x1 > x2) || (x1 == x2);
217: }
218: }
219:
220: // class representing the exclusive between predicate
221: public static class ExclusiveBetween extends
222: TernaryIntArithmeticPredicate {
223:
224: public String getName() {
225: return "between (excl.)";
226: }
227:
228: public boolean evaluate(int x1, int x2, int x3) {
229: return (x1 < x2) && (x2 < x3);
230: }
231: }
232:
233: // class representing the inclusive between predicate
234: public static class InclusiveBetween extends
235: TernaryIntArithmeticPredicate {
236:
237: public String getName() {
238: return "between (incl.)";
239: }
240:
241: public boolean evaluate(int x1, int x2, int x3) {
242: return (x1 <= x2) && (x2 <= x3);
243: }
244: }
245: }
|