001: package org.andromda.translation.ocl.validation;
002:
003: import org.apache.commons.lang.BooleanUtils;
004:
005: /**
006: * Used to translate OCL expressions to their corresponding Java expressions.
007: */
008: public final class OCLExpressions {
009: public static boolean equal(final Object first, final Object second) {
010: return (first == null) ? (second == null) : first
011: .equals(second);
012: }
013:
014: public static boolean equal(final boolean first,
015: final boolean second) {
016: return first == second;
017: }
018:
019: public static boolean equal(final int first, final int second) {
020: return first == second;
021: }
022:
023: public static boolean equal(final short first, final short second) {
024: return first == second;
025: }
026:
027: public static boolean equal(final byte first, final byte second) {
028: return first == second;
029: }
030:
031: public static boolean equal(final char first, final char second) {
032: return first == second;
033: }
034:
035: public static boolean equal(final float first, final float second) {
036: return first == second;
037: }
038:
039: public static boolean equal(final double first, final double second) {
040: return first == second;
041: }
042:
043: public static boolean equal(final long first, final long second) {
044: return first == second;
045: }
046:
047: public static boolean equal(final Object first, final boolean second) {
048: return (second) ? Boolean.TRUE.equals(first)
049: || BooleanUtils.toBoolean(String.valueOf(first))
050: : Boolean.FALSE.equals(first)
051: || !BooleanUtils.toBoolean(String
052: .valueOf(first));
053: }
054:
055: public static boolean equal(final boolean first, final Object second) {
056: return equal(second, first);
057: }
058:
059: public static boolean notEqual(final Object first,
060: final Object second) {
061: return first == null ? second != null : !first.equals(second);
062: }
063:
064: public static boolean notEqual(final boolean first,
065: final boolean second) {
066: return first != second;
067: }
068:
069: public static boolean equal(final Object first, final long second) {
070: boolean equal = first instanceof Comparable;
071: if (equal) {
072: final Comparable comparable = (Comparable) first;
073: equal = comparable.equals(new Long(second));
074: }
075: return equal;
076: }
077:
078: public static boolean notEqual(final int first, final int second) {
079: return first != second;
080: }
081:
082: public static boolean notEqual(final short first, final short second) {
083: return first != second;
084: }
085:
086: public static boolean notEqual(final byte first, final byte second) {
087: return first != second;
088: }
089:
090: public static boolean notEqual(final char first, final char second) {
091: return first != second;
092: }
093:
094: public static boolean notEqual(final float first, final float second) {
095: return first != second;
096: }
097:
098: public static boolean notEqual(final double first,
099: final double second) {
100: return first != second;
101: }
102:
103: public static boolean notEqual(final long first, final long second) {
104: return first != second;
105: }
106:
107: public static boolean notEqual(final Object first,
108: final boolean second) {
109: return !equal(first, second);
110: }
111:
112: public static boolean notEqual(final boolean first,
113: final Object second) {
114: return notEqual(second, first);
115: }
116:
117: public static boolean less(final Comparable first,
118: final Comparable second) {
119: return first == null ? second == null
120: : first.compareTo(second) < 0;
121: }
122:
123: public static boolean less(final int first, final int second) {
124: return first < second;
125: }
126:
127: public static boolean less(final short first, final short second) {
128: return first < second;
129: }
130:
131: public static boolean less(final byte first, final byte second) {
132: return first < second;
133: }
134:
135: public static boolean less(final char first, final char second) {
136: return first < second;
137: }
138:
139: public static boolean less(final float first, final float second) {
140: return first < second;
141: }
142:
143: public static boolean less(final double first, final double second) {
144: return first < second;
145: }
146:
147: public static boolean less(final long first, final long second) {
148: return first < second;
149: }
150:
151: public static boolean lessOrEqual(final Comparable first,
152: final Comparable second) {
153: return first == null ? second == null : (first
154: .compareTo(second) <= 0);
155: }
156:
157: public static boolean lessOrEqual(final Object first,
158: final Object second) {
159: boolean lessOrEqual = first instanceof Comparable
160: && second instanceof Comparable;
161: if (lessOrEqual) {
162: lessOrEqual = lessOrEqual((Comparable) first,
163: (Comparable) second);
164: }
165: return lessOrEqual;
166: }
167:
168: public static boolean lessOrEqual(final int first, final int second) {
169: return first <= second;
170: }
171:
172: public static boolean lessOrEqual(final short first,
173: final short second) {
174: return first <= second;
175: }
176:
177: public static boolean lessOrEqual(final byte first,
178: final byte second) {
179: return first <= second;
180: }
181:
182: public static boolean lessOrEqual(final char first,
183: final char second) {
184: return first <= second;
185: }
186:
187: public static boolean lessOrEqual(final float first,
188: final float second) {
189: return first <= second;
190: }
191:
192: public static boolean lessOrEqual(final Object first,
193: final float second) {
194: boolean lessOrEqual = first instanceof Comparable;
195: if (lessOrEqual) {
196: lessOrEqual = lessOrEqual((Comparable) first, new Float(
197: second));
198: }
199: return lessOrEqual;
200: }
201:
202: public static boolean lessOrEqual(final float first,
203: final Object second) {
204: boolean lessOrEqual = second instanceof Comparable;
205: if (lessOrEqual) {
206: lessOrEqual = lessOrEqual(new Float(first),
207: (Comparable) second);
208: }
209: return lessOrEqual;
210: }
211:
212: public static boolean lessOrEqual(final double first,
213: final double second) {
214: return first <= second;
215: }
216:
217: public static boolean lessOrEqual(final Object first,
218: final double second) {
219: boolean lessOrEqual = first instanceof Comparable;
220: if (lessOrEqual) {
221: lessOrEqual = lessOrEqual((Comparable) first, new Double(
222: second));
223: }
224: return lessOrEqual;
225: }
226:
227: public static boolean lessOrEqual(final double first,
228: final Object second) {
229: boolean lessOrEqual = second instanceof Comparable;
230: if (lessOrEqual) {
231: lessOrEqual = lessOrEqual(new Double(first),
232: (Comparable) second);
233: }
234: return lessOrEqual;
235: }
236:
237: public static boolean lessOrEqual(final long first,
238: final long second) {
239: return first <= second;
240: }
241:
242: public static boolean lessOrEqual(final Object first,
243: final long second) {
244: boolean lessOrEqual = first instanceof Comparable;
245: if (lessOrEqual) {
246: lessOrEqual = lessOrEqual((Comparable) first, new Long(
247: second));
248: }
249: return lessOrEqual;
250: }
251:
252: public static boolean lessOrEqual(final Object first,
253: final int second) {
254: boolean lessOrEqual = first instanceof Comparable;
255: if (lessOrEqual) {
256: lessOrEqual = lessOrEqual((Comparable) first, new Integer(
257: second));
258: }
259: return lessOrEqual;
260: }
261:
262: public static boolean lessOrEqual(final Object first,
263: final short second) {
264: boolean lessOrEqual = first instanceof Comparable;
265: if (lessOrEqual) {
266: lessOrEqual = lessOrEqual((Comparable) first, new Short(
267: second));
268: }
269: return lessOrEqual;
270: }
271:
272: public static boolean lessOrEqual(final long first,
273: final Object second) {
274: boolean lessOrEqual = second instanceof Comparable;
275: if (lessOrEqual) {
276: lessOrEqual = lessOrEqual(new Long(first),
277: (Comparable) second);
278: }
279: return lessOrEqual;
280: }
281:
282: public static boolean lessOrEqual(final int first,
283: final Object second) {
284: boolean lessOrEqual = second instanceof Comparable;
285: if (lessOrEqual) {
286: lessOrEqual = lessOrEqual(new Integer(first),
287: (Comparable) second);
288: }
289: return lessOrEqual;
290: }
291:
292: public static boolean lessOrEqual(final short first,
293: final Object second) {
294: boolean lessOrEqual = second instanceof Comparable;
295: if (lessOrEqual) {
296: lessOrEqual = lessOrEqual(new Short(first),
297: (Comparable) second);
298: }
299: return lessOrEqual;
300: }
301:
302: public static boolean greater(final Comparable first,
303: final Comparable second) {
304: return first == null ? second == null
305: : first.compareTo(second) > 0;
306: }
307:
308: public static boolean greater(final Object first, final long second) {
309: boolean greater = first instanceof Comparable;
310: if (greater) {
311: final Comparable comparable = (Comparable) first;
312: greater = comparable.compareTo(new Long(second)) > 0;
313: }
314: return greater;
315: }
316:
317: public static boolean greater(final byte first, final byte second) {
318: return first > second;
319: }
320:
321: public static boolean greater(final char first, final char second) {
322: return first > second;
323: }
324:
325: public static boolean greater(final float first, final float second) {
326: return first > second;
327: }
328:
329: public static boolean greater(final double first,
330: final double second) {
331: return first > second;
332: }
333:
334: public static boolean greater(final long first, final long second) {
335: return first > second;
336: }
337:
338: public static boolean greaterOrEqual(final Comparable first,
339: final Comparable second) {
340: return first == null ? second == null
341: : first.compareTo(second) >= 0;
342: }
343:
344: public static boolean greaterOrEqual(final Object first,
345: final long second) {
346: boolean greaterOrEqual = first instanceof Comparable;
347: if (greaterOrEqual) {
348: greaterOrEqual = greaterOrEqual((Comparable) first,
349: new Long(second));
350: }
351: return greaterOrEqual;
352: }
353:
354: public static boolean greaterOrEqual(final Object first,
355: final int second) {
356: boolean greaterOrEqual = first instanceof Comparable;
357: if (greaterOrEqual) {
358: greaterOrEqual = greaterOrEqual((Comparable) first,
359: new Integer(second));
360: }
361: return greaterOrEqual;
362: }
363:
364: public static boolean greaterOrEqual(final Object first,
365: final short second) {
366: boolean greaterOrEqual = first instanceof Comparable;
367: if (greaterOrEqual) {
368: greaterOrEqual = greaterOrEqual((Comparable) first,
369: new Short(second));
370: }
371: return greaterOrEqual;
372: }
373:
374: public static boolean greaterOrEqual(final long first,
375: final Object second) {
376: boolean greaterOrEqual = second instanceof Comparable;
377: if (greaterOrEqual) {
378: greaterOrEqual = greaterOrEqual(new Long(first),
379: (Comparable) second);
380: }
381: return greaterOrEqual;
382: }
383:
384: public static boolean greaterOrEqual(final int first,
385: final Object second) {
386: boolean greaterOrEqual = second instanceof Comparable;
387: if (greaterOrEqual) {
388: greaterOrEqual = greaterOrEqual(new Integer(first),
389: (Comparable) second);
390: }
391: return greaterOrEqual;
392: }
393:
394: public static boolean greaterOrEqual(final short first,
395: final Object second) {
396: boolean greaterOrEqual = second instanceof Comparable;
397: if (greaterOrEqual) {
398: greaterOrEqual = greaterOrEqual(new Short(first),
399: (Comparable) second);
400: }
401: return greaterOrEqual;
402: }
403:
404: public static boolean greaterOrEqual(final Object first,
405: final Object second) {
406: boolean greaterOrEqual = first instanceof Comparable
407: && second instanceof Comparable;
408: if (greaterOrEqual) {
409: greaterOrEqual = greaterOrEqual((Comparable) first,
410: (Comparable) second);
411: }
412: return greaterOrEqual;
413: }
414:
415: public static boolean greaterOrEqual(final Object first,
416: final double second) {
417: boolean greaterOrEqual = first instanceof Comparable;
418: if (greaterOrEqual) {
419: greaterOrEqual = greaterOrEqual((Comparable) first,
420: new Double(second));
421: }
422: return greaterOrEqual;
423: }
424:
425: public static boolean greaterOrEqual(final Object first,
426: final float second) {
427: boolean greaterOrEqual = first instanceof Comparable;
428: if (greaterOrEqual) {
429: greaterOrEqual = greaterOrEqual((Comparable) first,
430: new Float(second));
431: }
432: return greaterOrEqual;
433: }
434:
435: public static boolean greaterOrEqual(final double first,
436: final Object second) {
437: boolean greaterOrEqual = second instanceof Comparable;
438: if (greaterOrEqual) {
439: greaterOrEqual = greaterOrEqual(new Double(first),
440: (Comparable) second);
441: }
442: return greaterOrEqual;
443: }
444:
445: public static boolean greaterOrEqual(final float first,
446: final Object second) {
447: boolean greaterOrEqual = second instanceof Comparable;
448: if (greaterOrEqual) {
449: greaterOrEqual = greaterOrEqual(new Float(first),
450: (Comparable) second);
451: }
452: return greaterOrEqual;
453: }
454:
455: public static boolean greaterOrEqual(final byte first,
456: final byte second) {
457: return first >= second;
458: }
459:
460: public static boolean greaterOrEqual(final char first,
461: final char second) {
462: return first >= second;
463: }
464:
465: public static boolean greaterOrEqual(final double first,
466: final double second) {
467: return first >= second;
468: }
469:
470: public static boolean greaterOrEqual(final long first,
471: final long second) {
472: return first >= second;
473: }
474: }
|