001package org.andromda.translation.ocl.validation;
002
003import org.apache.commons.lang.BooleanUtils;
004
005
006/**
007 * Used to translate OCL expressions to their corresponding Java expressions.
008 */
009public final class OCLExpressions
010{
011    /**
012     * @param first
013     * @param second
014     * @return first.equals(second)
015     */
016    public static boolean equal(
017            final Object first,
018            final Object second)
019    {
020        return (first == null) ? (second == null) : first.equals(second);
021    }
022
023    /**
024     * @param first
025     * @param second
026     * @return first == second
027     */
028    public static boolean equal(
029            final boolean first,
030            final boolean second)
031    {
032        return first == second;
033    }
034
035    /**
036     * @param first
037     * @param second
038     * @return first == second
039     */
040    public static boolean equal(
041            final int first,
042            final int second)
043    {
044        return first == second;
045    }
046
047    /**
048     * @param first
049     * @param second
050     * @return first == second
051     */
052    public static boolean equal(
053            final short first,
054            final short second)
055    {
056        return first == second;
057    }
058
059    /**
060     * @param first
061     * @param second
062     * @return first == second
063     */
064    public static boolean equal(
065            final byte first,
066            final byte second)
067    {
068        return first == second;
069    }
070
071    /**
072     * @param first
073     * @param second
074     * @return first == second
075     */
076    public static boolean equal(
077            final char first,
078            final char second)
079    {
080        return first == second;
081    }
082
083    /**
084     * @param first
085     * @param second
086     * @return first == second
087     */
088    public static boolean equal(
089            final float first,
090            final float second)
091    {
092        return first == second;
093    }
094
095    /**
096     * @param first
097     * @param second
098     * @return first == second
099     */
100    public static boolean equal(
101            final double first,
102            final double second)
103    {
104        return first == second;
105    }
106
107    /**
108     * @param first
109     * @param second
110     * @return first == second
111     */
112    public static boolean equal(
113            final long first,
114            final long second)
115    {
116        return first == second;
117    }
118
119    /**
120     * @param first
121     * @param second
122     * @return first == second
123     */
124    public static boolean equal(
125            final Object first,
126            final boolean second)
127    {
128        return (second) ? Boolean.TRUE.equals(first) || BooleanUtils.toBoolean(String.valueOf(first))
129                : Boolean.FALSE.equals(first) || !BooleanUtils.toBoolean(String.valueOf(first));
130    }
131
132    /**
133     * @param first
134     * @param second
135     * @return equal(second, first)
136     */
137    public static boolean equal(
138            final boolean first,
139            final Object second)
140    {
141        return equal(second, first);
142    }
143
144    /**
145     * @param first
146     * @param second
147     * @return !first.equals(second)
148     */
149    public static boolean notEqual(
150            final Object first,
151            final Object second)
152    {
153        return first == null ? second != null : !first.equals(second);
154    }
155
156    /**
157     * @param first
158     * @param second
159     * @return first != second
160     */
161    public static boolean notEqual(
162            final boolean first,
163            final boolean second)
164    {
165        return first != second;
166    }
167
168    /**
169     * @param first
170     * @param second
171     * @return equal
172     */
173    public static boolean equal(
174            final Object first,
175            final long second)
176    {
177        boolean equal = first instanceof Comparable;
178        if (equal)
179        {
180            final Comparable comparable = (Comparable) first;
181            if (comparable instanceof Integer)
182            {
183                equal = comparable.compareTo(Integer.valueOf((int) second)) > 0;
184            }
185            else
186            {
187                equal = comparable.equals(Long.valueOf(second));
188            }
189        }
190        return equal;
191    }
192
193    /**
194     * @param first
195     * @param second
196     * @return equal
197     */
198    public static boolean equal(
199            final Object first,
200            final int second)
201    {
202        boolean equal = first instanceof Comparable;
203        if (equal)
204        {
205            final Comparable comparable = (Comparable) first;
206            equal = comparable.equals(Integer.valueOf(second));
207        }
208        return equal;
209    }
210
211    /**
212     * @param first
213     * @param second
214     * @return first != second
215     */
216    public static boolean notEqual(
217            final int first,
218            final int second)
219    {
220        return first != second;
221    }
222
223    /**
224     * @param first
225     * @param second
226     * @return first != second
227     */
228    public static boolean notEqual(
229            final short first,
230            final short second)
231    {
232        return first != second;
233    }
234
235    /**
236     * @param first
237     * @param second
238     * @return first != second
239     */
240    public static boolean notEqual(
241            final byte first,
242            final byte second)
243    {
244        return first != second;
245    }
246
247    /**
248     * @param first
249     * @param second
250     * @return first != second
251     */
252    public static boolean notEqual(
253            final char first,
254            final char second)
255    {
256        return first != second;
257    }
258
259    /**
260     * @param first
261     * @param second
262     * @return first != second
263     */
264    public static boolean notEqual(
265            final float first,
266            final float second)
267    {
268        return first != second;
269    }
270
271    /**
272     * @param first
273     * @param second
274     * @return first != second
275     */
276    public static boolean notEqual(
277            final double first,
278            final double second)
279    {
280        return first != second;
281    }
282
283    /**
284     * @param first
285     * @param second
286     * @return first != second
287     */
288    public static boolean notEqual(
289            final long first,
290            final long second)
291    {
292        return first != second;
293    }
294
295    /**
296     * @param first
297     * @param second
298     * @return !equal(first, second)
299     */
300    public static boolean notEqual(
301            final Object first,
302            final boolean second)
303    {
304        return !equal(first, second);
305    }
306
307    /**
308     * @param first
309     * @param second
310     * @return notEqual(second, first)
311     */
312    public static boolean notEqual(
313            final boolean first,
314            final Object second)
315    {
316        return notEqual(second, first);
317    }
318
319    /**
320     * @param first
321     * @param second
322     * @return first.compareTo(second) < 0
323     */
324    public static boolean less(
325            final Comparable first,
326            final Comparable second)
327    {
328        return first == null ? second == null : first.compareTo(second) < 0;
329    }
330
331    /**
332     * @param first
333     * @param second
334     * @return first < second
335     */
336    public static boolean less(
337            final int first,
338            final int second)
339    {
340        return first < second;
341    }
342
343    /**
344     * @param first
345     * @param second
346     * @return first < second
347     */
348    public static boolean less(
349            final short first,
350            final short second)
351    {
352        return first < second;
353    }
354
355    /**
356     * @param first
357     * @param second
358     * @return first < second
359     */
360    public static boolean less(
361            final byte first,
362            final byte second)
363    {
364        return first < second;
365    }
366
367    /**
368     * @param first
369     * @param second
370     * @return first < second
371     */
372    public static boolean less(
373            final char first,
374            final char second)
375    {
376        return first < second;
377    }
378
379    /**
380     * @param first
381     * @param second
382     * @return first < second
383     */
384    public static boolean less(
385            final float first,
386            final float second)
387    {
388        return first < second;
389    }
390
391    /**
392     * @param first
393     * @param second
394     * @return first < second
395     */
396    public static boolean less(
397            final double first,
398            final double second)
399    {
400        return first < second;
401    }
402
403    /**
404     * @param first
405     * @param second
406     * @return first < second
407     */
408    public static boolean less(
409            final long first,
410            final long second)
411    {
412        return first < second;
413    }
414
415    /**
416     * @param first
417     * @param second
418     * @return first.compareTo(second) <= 0
419     */
420    public static boolean lessOrEqual(
421            final Comparable first,
422            final Comparable second)
423    {
424        return first == null ? second == null : (first.compareTo(second) <= 0);
425    }
426
427    /**
428     * @param first
429     * @param second
430     * @return lessOrEqual((Comparable) first, (Comparable) second)
431     */
432    public static boolean lessOrEqual(
433            final Object first,
434            final Object second)
435    {
436        boolean lessOrEqual = first instanceof Comparable && second instanceof Comparable;
437        if (lessOrEqual)
438        {
439            lessOrEqual = lessOrEqual((Comparable) first, (Comparable) second);
440        }
441        return lessOrEqual;
442    }
443
444    /**
445     * @param first
446     * @param second
447     * @return first <= second
448     */
449    public static boolean lessOrEqual(
450            final int first,
451            final int second)
452    {
453        return first <= second;
454    }
455
456    /**
457     * @param first
458     * @param second
459     * @return first <= second
460     */
461    public static boolean lessOrEqual(
462            final short first,
463            final short second)
464    {
465        return first <= second;
466    }
467
468    /**
469     * @param first
470     * @param second
471     * @return first <= second
472     */
473    public static boolean lessOrEqual(
474            final byte first,
475            final byte second)
476    {
477        return first <= second;
478    }
479
480    /**
481     * @param first
482     * @param second
483     * @return first <= second
484     */
485    public static boolean lessOrEqual(
486            final char first,
487            final char second)
488    {
489        return first <= second;
490    }
491
492    /**
493     * @param first
494     * @param second
495     * @return first <= second
496     */
497    public static boolean lessOrEqual(
498            final float first,
499            final float second)
500    {
501        return first <= second;
502    }
503
504    /**
505     * @param first
506     * @param second
507     * @return lessOrEqual((Comparable) first, Float.valueOf(second))
508     */
509    public static boolean lessOrEqual(
510            final Object first,
511            final float second)
512    {
513        boolean lessOrEqual = first instanceof Comparable;
514        if (lessOrEqual)
515        {
516            lessOrEqual = lessOrEqual((Comparable) first, Float.valueOf(second));
517        }
518        return lessOrEqual;
519    }
520
521    /**
522     * @param first
523     * @param second
524     * @return lessOrEqual(new Float(first), (Comparable) second)
525     */
526    public static boolean lessOrEqual(
527            final float first,
528            final Object second)
529    {
530        boolean lessOrEqual = second instanceof Comparable;
531        if (lessOrEqual)
532        {
533            lessOrEqual = lessOrEqual(new Float(first), (Comparable) second);
534        }
535        return lessOrEqual;
536    }
537
538    /**
539     * @param first
540     * @param second
541     * @return first <= second
542     */
543    public static boolean lessOrEqual(
544            final double first,
545            final double second)
546    {
547        return first <= second;
548    }
549
550    /**
551     * @param first
552     * @param second
553     * @return lessOrEqual((Comparable) first, Double.valueOf(second))
554     */
555    public static boolean lessOrEqual(
556            final Object first,
557            final double second)
558    {
559        boolean lessOrEqual = first instanceof Comparable;
560        if (lessOrEqual)
561        {
562            lessOrEqual = lessOrEqual((Comparable) first, Double.valueOf(second));
563        }
564        return lessOrEqual;
565    }
566
567    /**
568     * @param first
569     * @param second
570     * @return lessOrEqual(Double.valueOf(first), (Comparable) second)
571     */
572    public static boolean lessOrEqual(
573            final double first,
574            final Object second)
575    {
576        boolean lessOrEqual = second instanceof Comparable;
577        if (lessOrEqual)
578        {
579            lessOrEqual = lessOrEqual(Double.valueOf(first), (Comparable) second);
580        }
581        return lessOrEqual;
582    }
583
584    /**
585     * @param first
586     * @param second
587     * @return first <= second
588     */
589    public static boolean lessOrEqual(
590            final long first,
591            final long second)
592    {
593        return first <= second;
594    }
595
596    /**
597     * @param first
598     * @param second
599     * @return lessOrEqual((Comparable) first, Long.valueOf(second))
600     */
601    public static boolean lessOrEqual(
602            final Object first,
603            final long second)
604    {
605        boolean lessOrEqual = first instanceof Comparable;
606        if (lessOrEqual)
607        {
608            if (first instanceof Integer)
609            {
610                lessOrEqual = lessOrEqual((Comparable) first, Integer.valueOf((int) second));
611            }
612            else
613            {
614                lessOrEqual = lessOrEqual((Comparable) first, Long.valueOf(second));
615            }
616        }
617        return lessOrEqual;
618    }
619
620    /**
621     * @param first
622     * @param second
623     * @return lessOrEqual((Comparable) first, Integer.valueOf(second))
624     */
625    public static boolean lessOrEqual(
626            final Object first,
627            final int second)
628    {
629        boolean lessOrEqual = first instanceof Comparable;
630        if (lessOrEqual)
631        {
632            lessOrEqual = lessOrEqual((Comparable) first, Integer.valueOf(second));
633        }
634        return lessOrEqual;
635    }
636
637    /**
638     * @param first
639     * @param second
640     * @return lessOrEqual((Comparable) first, Short.valueOf(second))
641     */
642    public static boolean lessOrEqual(
643            final Object first,
644            final short second)
645    {
646        boolean lessOrEqual = first instanceof Comparable;
647        if (lessOrEqual)
648        {
649            lessOrEqual = lessOrEqual((Comparable) first, Short.valueOf(second));
650        }
651        return lessOrEqual;
652    }
653
654    /**
655     * @param first
656     * @param second
657     * @return lessOrEqual = lessOrEqual(Long.valueOf(first), (Comparable) second)
658     */
659    public static boolean lessOrEqual(
660            final long first,
661            final Object second)
662    {
663        boolean lessOrEqual = second instanceof Comparable;
664        if (lessOrEqual)
665        {
666            if (second instanceof Integer)
667            {
668                lessOrEqual = lessOrEqual(Integer.valueOf((int) first), (Comparable) second);
669            }
670            else
671            {
672                lessOrEqual = lessOrEqual(Long.valueOf(first), (Comparable) second);
673            }
674        }
675        return lessOrEqual;
676    }
677
678    /**
679     * @param first
680     * @param second
681     * @return lessOrEqual(new Integer(first), (Comparable) second)
682     */
683    public static boolean lessOrEqual(
684            final int first,
685            final Object second)
686    {
687        boolean lessOrEqual = second instanceof Comparable;
688        if (lessOrEqual)
689        {
690            lessOrEqual = lessOrEqual(new Integer(first), (Comparable) second);
691        }
692        return lessOrEqual;
693    }
694
695    /**
696     * @param first
697     * @param second
698     * @return lessOrEqual(new Short(first), (Comparable) second)
699     */
700    public static boolean lessOrEqual(
701            final short first,
702            final Object second)
703    {
704        boolean lessOrEqual = second instanceof Comparable;
705        if (lessOrEqual)
706        {
707            lessOrEqual = lessOrEqual(new Short(first), (Comparable) second);
708        }
709        return lessOrEqual;
710    }
711
712    /**
713     * @param first
714     * @param second
715     * @return first.compareTo(second) > 0
716     */
717    public static boolean greater(
718            final Comparable first,
719            final Comparable second)
720    {
721        return first == null ? second == null : first.compareTo(second) > 0;
722    }
723
724    /**
725     * @param first
726     * @param second
727     * @return comparable.compareTo(Long.valueOf(second)) > 0
728     */
729    public static boolean greater(
730            final Object first,
731            final long second)
732    {
733        boolean greater = first instanceof Comparable;
734        if (greater)
735        {
736            final Comparable comparable = (Comparable) first;
737            if (comparable instanceof Integer)
738            {
739                greater = comparable.compareTo(Integer.valueOf((int) second)) > 0;
740            } else
741            {
742                greater = comparable.compareTo(Long.valueOf(second)) > 0;
743            }
744        }
745        return greater;
746    }
747
748    /**
749     * @param first
750     * @param second
751     * @return comparable.compareTo(Long.valueOf(second)) > 0
752     */
753    public static boolean greater(
754            final Object first,
755            final int second)
756    {
757        boolean greater = first instanceof Comparable;
758        if (greater)
759        {
760            final Comparable comparable = (Comparable) first;
761            if (comparable instanceof Integer)
762            {
763                greater = comparable.compareTo(Integer.valueOf(second)) > 0;
764            } else
765            {
766                greater = comparable.compareTo(Long.valueOf(second)) > 0;
767            }
768        }
769        return greater;
770    }
771
772    /**
773     * @param first
774     * @param second
775     * @return first > second
776     */
777    public static boolean greater(
778            final byte first,
779            final byte second)
780    {
781        return first > second;
782    }
783
784    /**
785     * @param first
786     * @param second
787     * @return first > second
788     */
789    public static boolean greater(
790            final char first,
791            final char second)
792    {
793        return first > second;
794    }
795
796    /**
797     * @param first
798     * @param second
799     * @return first > second
800     */
801    public static boolean greater(
802            final float first,
803            final float second)
804    {
805        return first > second;
806    }
807
808    /**
809     * @param first
810     * @param second
811     * @return first > second
812     */
813    public static boolean greater(
814            final double first,
815            final double second)
816    {
817        return first > second;
818    }
819
820    /**
821     * @param first
822     * @param second
823     * @return first > second
824     */
825    public static boolean greater(
826            final long first,
827            final long second)
828    {
829        return first > second;
830    }
831
832    /**
833     * @param first
834     * @param second
835     * @return first.compareTo(second) >= 0
836     */
837    public static boolean greaterOrEqual(
838            final Comparable first,
839            final Comparable second)
840    {
841        return first == null ? second == null : first.compareTo(second) >= 0;
842    }
843
844    /**
845     * @param first
846     * @param second
847     * @return greaterOrEqual((Comparable) first, Long.valueOf(second))
848     */
849    public static boolean greaterOrEqual(
850            final Object first,
851            final long second)
852    {
853        boolean greaterOrEqual = first instanceof Comparable;
854        if (greaterOrEqual)
855        {
856            if (first instanceof Integer)
857            {
858                greaterOrEqual = greaterOrEqual((Comparable) first, Integer.valueOf((int) second));
859            } else
860            {
861                greaterOrEqual = greaterOrEqual((Comparable) first, Long.valueOf(second));
862            }
863        }
864        return greaterOrEqual;
865    }
866
867    /**
868     * @param first
869     * @param second
870     * @return greaterOrEqual((Comparable) first, Integer.valueOf(second))
871     */
872    public static boolean greaterOrEqual(
873            final Object first,
874            final int second)
875    {
876        boolean greaterOrEqual = first instanceof Comparable;
877        if (greaterOrEqual)
878        {
879            greaterOrEqual = greaterOrEqual((Comparable) first, Integer.valueOf(second));
880        }
881        return greaterOrEqual;
882    }
883
884    /**
885     * @param first
886     * @param second
887     * @return greaterOrEqual((Comparable) first, Short.valueOf(second))
888     */
889    public static boolean greaterOrEqual(
890            final Object first,
891            final short second)
892    {
893        boolean greaterOrEqual = first instanceof Comparable;
894        if (greaterOrEqual)
895        {
896            greaterOrEqual = greaterOrEqual((Comparable) first, Short.valueOf(second));
897        }
898        return greaterOrEqual;
899    }
900
901    /**
902     * @param first
903     * @param second
904     * @return greaterOrEqual(Long.valueOf(first), (Comparable) second)
905     */
906    public static boolean greaterOrEqual(
907            final long first,
908            final Object second)
909    {
910        boolean greaterOrEqual = second instanceof Comparable;
911        if (greaterOrEqual)
912        {
913            if (second instanceof Integer)
914            {
915                greaterOrEqual = greaterOrEqual(Integer.valueOf((int) first), (Integer) second);
916            } else
917            {
918                greaterOrEqual = greaterOrEqual(Long.valueOf(first), (Comparable) second);
919            }
920        }
921        return greaterOrEqual;
922    }
923
924    /**
925     * @param first
926     * @param second
927     * @return greaterOrEqual(Integer.valueOf(first), (Comparable) second)
928     */
929    public static boolean greaterOrEqual(
930            final int first,
931            final Object second)
932    {
933        boolean greaterOrEqual = second instanceof Comparable;
934        if (greaterOrEqual)
935        {
936            greaterOrEqual = greaterOrEqual(Integer.valueOf(first), (Comparable) second);
937        }
938        return greaterOrEqual;
939    }
940
941    /**
942     * @param first
943     * @param second
944     * @return greaterOrEqual(Short.valueOf(first), (Comparable) second)
945     */
946    public static boolean greaterOrEqual(
947            final short first,
948            final Object second)
949    {
950        boolean greaterOrEqual = second instanceof Comparable;
951        if (greaterOrEqual)
952        {
953            greaterOrEqual = greaterOrEqual(Short.valueOf(first), (Comparable) second);
954        }
955        return greaterOrEqual;
956    }
957
958    /**
959     * @param first
960     * @param second
961     * @return greaterOrEqual((Comparable) first, (Comparable) second)
962     */
963    public static boolean greaterOrEqual(
964            final Object first,
965            final Object second)
966    {
967        boolean greaterOrEqual = first instanceof Comparable && second instanceof Comparable;
968        if (greaterOrEqual)
969        {
970            greaterOrEqual = greaterOrEqual((Comparable) first, (Comparable) second);
971        }
972        return greaterOrEqual;
973    }
974
975    /**
976     * @param first
977     * @param second
978     * @return greaterOrEqual((Comparable) first, Double.valueOf(second))
979     */
980    public static boolean greaterOrEqual(
981            final Object first,
982            final double second)
983    {
984        boolean greaterOrEqual = first instanceof Comparable;
985        if (greaterOrEqual)
986        {
987            greaterOrEqual = greaterOrEqual((Comparable) first, Double.valueOf(second));
988        }
989        return greaterOrEqual;
990    }
991
992    /**
993     * @param first
994     * @param second
995     * @return greaterOrEqual((Comparable) first, Float.valueOf(second))
996     */
997    public static boolean greaterOrEqual(
998            final Object first,
999            final float second)
1000    {
1001        boolean greaterOrEqual = first instanceof Comparable;
1002        if (greaterOrEqual)
1003        {
1004            greaterOrEqual = greaterOrEqual((Comparable) first, Float.valueOf(second));
1005        }
1006        return greaterOrEqual;
1007    }
1008
1009    /**
1010     * @param first
1011     * @param second
1012     * @return greaterOrEqual(new Double(first), (Comparable) second
1013     */
1014    public static boolean greaterOrEqual(
1015            final double first,
1016            final Object second)
1017    {
1018        boolean greaterOrEqual = second instanceof Comparable;
1019        if (greaterOrEqual)
1020        {
1021            greaterOrEqual = greaterOrEqual(new Double(first), (Comparable) second);
1022        }
1023        return greaterOrEqual;
1024    }
1025
1026    /**
1027     * @param first
1028     * @param second
1029     * @return greaterOrEqual(new Float(first), (Comparable) second)
1030     */
1031    public static boolean greaterOrEqual(
1032            final float first,
1033            final Object second)
1034    {
1035        boolean greaterOrEqual = second instanceof Comparable;
1036        if (greaterOrEqual)
1037        {
1038            greaterOrEqual = greaterOrEqual(new Float(first), (Comparable) second);
1039        }
1040        return greaterOrEqual;
1041    }
1042
1043    /**
1044     * @param first
1045     * @param second
1046     * @return first >= second
1047     */
1048    public static boolean greaterOrEqual(
1049            final byte first,
1050            final byte second)
1051    {
1052        return first >= second;
1053    }
1054
1055    /**
1056     * @param first
1057     * @param second
1058     * @return first >= second
1059     */
1060    public static boolean greaterOrEqual(
1061            final char first,
1062            final char second)
1063    {
1064        return first >= second;
1065    }
1066
1067    /**
1068     * @param first
1069     * @param second
1070     * @return first >= second
1071     */
1072    public static boolean greaterOrEqual(
1073            final double first,
1074            final double second)
1075    {
1076        return first >= second;
1077    }
1078
1079    /**
1080     * @param first
1081     * @param second
1082     * @return first >= second
1083     */
1084    public static boolean greaterOrEqual(
1085            final long first,
1086            final long second)
1087    {
1088        return first >= second;
1089    }
1090}