1 9 10 package org.lsmp.djep.groupJep.function; 11 12 import java.util.*; 13 import org.nfunk.jep.*; 14 import org.nfunk.jep.function.*; 15 import org.lsmp.djep.groupJep.*; 16 import org.lsmp.djep.groupJep.interfaces.*; 17 18 25 public class GComparative extends PostfixMathCommand 26 { 27 int id; 28 GroupI group; 29 30 public GComparative(GroupI group,int id_in) 31 { 32 id = id_in; 33 numberOfParameters = 2; 34 this.group = group; 35 } 36 37 public void run(Stack inStack) 38 throws ParseException 39 { 40 checkStack(inStack); 42 Object param2 = inStack.pop(); 43 Object param1 = inStack.pop(); 44 45 if ((param1 instanceof Number ) && (param2 instanceof Number )) 46 { 47 Number num1 = (Number )param1; 48 Number num2 = (Number )param2; 49 boolean flag; 50 if(group instanceof OrderedSetI) 51 { 52 int comp = ((OrderedSetI) group).compare(num1,num2); 53 switch (id) 54 { 55 case Comparative.LT: 56 flag = comp < 0; 57 break; 58 case Comparative.GT: 59 flag = comp > 0; 60 break; 61 case Comparative.LE: 62 flag = comp <= 0; 63 break; 64 case Comparative.GE: 65 flag = comp >= 0; 66 break; 67 case Comparative.NE: 68 flag = comp != 0; 69 break; 70 case Comparative.EQ: 71 flag = comp == 0; 72 break; 73 default: 74 throw new ParseException("Unknown relational operator"); 75 } 76 } 77 else 78 { 79 switch (id) 80 { 81 case Comparative.NE: 82 flag = !group.equals(num1,num2); 83 break; 84 case Comparative.EQ: 85 flag = group.equals(num1,num2); 86 break; 87 default: 88 throw new ParseException("Unknown relational operator"); 89 } 90 } 91 inStack.push(flag ? Boolean.TRUE : Boolean.FALSE); } 93 else if ((param1 instanceof Boolean ) && (param2 instanceof Boolean )) 94 { 95 boolean num1 = ((Boolean )param1).booleanValue(); 96 boolean num2 = ((Boolean )param2).booleanValue(); 97 boolean flag; 98 switch (id) 99 { 100 case Comparative.NE: 101 flag = num1 != num2; 102 break; 103 case Comparative.EQ: 104 flag = num1 == num2; 105 break; 106 default: 107 throw new ParseException("Unknown relational operator"); 108 } 109 inStack.push(flag ? Boolean.TRUE : Boolean.FALSE); } 111 else throw new ParseException("Invalid parameters for comparitive op"); 112 113 } 114 } 115 | Popular Tags |