IntegerInterpreter | Line # 40 | 31 | 10 | 35% |
0.35
|
No Tests | |||
1 | /* | |
2 | * Copyright (c) 2000-2005, University of Salford | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * Neither the name of the University of Salford nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | package issrg.pba.rbac.xmlpolicy.ifstatement; | |
33 | ||
34 | /** | |
35 | * This is the interpreter for comparisons of integers in the IF-statements. | |
36 | * | |
37 | * @author A.Otenko | |
38 | */ | |
39 | ||
40 | public class IntegerInterpreter implements Interpreter { | |
41 | ||
42 | protected int mode; | |
43 | public final static int EQ_MODE=0; | |
44 | public final static int GE_MODE=1; | |
45 | public final static int LE_MODE=2; | |
46 | public final static int GT_MODE=3; | |
47 | public final static int LT_MODE=4; | |
48 | ||
49 | /** | |
50 | * Call this method to register the IntegerInterpreter with the relevant | |
51 | * expression nodes: EqNode, GeNode, GtNode, LeNode, LtNode. The interpreter | |
52 | * provides the conventional semantics of number comparison for those | |
53 | * operations. | |
54 | */ | |
55 | 21 | public static void register(){ |
56 | 21 | EqNode.registerInterpreter(new IntegerInterpreter(EQ_MODE)); |
57 | 21 | GeNode.registerInterpreter(new IntegerInterpreter(GE_MODE)); |
58 | 21 | LeNode.registerInterpreter(new IntegerInterpreter(LE_MODE)); |
59 | 21 | GtNode.registerInterpreter(new IntegerInterpreter(GT_MODE)); |
60 | 21 | LtNode.registerInterpreter(new IntegerInterpreter(LT_MODE)); |
61 | } | |
62 | ||
63 | 0 | protected IntegerInterpreter() { |
64 | } | |
65 | ||
66 | /** | |
67 | * This is the constructor used to build the interpreter for different types | |
68 | * of comparison operation: an interpreter for | |
69 | * GE, LE, GT, LT and EQ correspondingly. The evaluate method will return the | |
70 | * comparison result corresponding to this mode. | |
71 | * | |
72 | * @param mode stands for the mode of operation; it can be one of EQ_MODE, | |
73 | * GE_MODE, LE_MODE, | |
74 | * GT_MODE or LT_MODE | |
75 | */ | |
76 | 105 | protected IntegerInterpreter(int mode){ |
77 | 105 | this.mode = mode; |
78 | } | |
79 | ||
80 | /** | |
81 | * This method tells whether this interpreter can evaluate the expression, | |
82 | * which is only if there are only two Terms, and both are of type | |
83 | * Types.INTEGER_TYPE. | |
84 | * | |
85 | * @param t - the array of Terms to be evaluated | |
86 | * | |
87 | * @return Types.BOOLEAN_TYPE, if the expression can be evaluated; null | |
88 | * otherwise | |
89 | */ | |
90 | 72 | public String canEvaluate(Term [] t){ |
91 | 72 | if (t.length==2 && |
92 | t[0].getType().intern()==Types.INTEGER_TYPE && | |
93 | t[0].getType().intern()==t[1].getType().intern()){ | |
94 | 8 | return Types.BOOLEAN_TYPE; |
95 | } | |
96 | ||
97 | 64 | return null; |
98 | } | |
99 | ||
100 | /** | |
101 | * This method evaluates the comparison expression, depending on the mode | |
102 | * it was given at construction time. If the expression can be evaluated, | |
103 | * it returns a java.lang.Boolean object; otherwise a EvaluationException is | |
104 | * thrown. | |
105 | * | |
106 | * @param env - the Environment in which the evaluation occurs | |
107 | * @param t - the array of Terms to be evaluated; should contain two and only | |
108 | * two Terms, each of type Types.INTEGER_TYPE | |
109 | * | |
110 | * @return java.lang.Boolean object that contains the boolean result of | |
111 | * evaluation, as determined by the mode provided at construction time | |
112 | */ | |
113 | 0 | public Object evaluate(Environment env, Term [] t) throws EvaluationException{ |
114 | 0 | if (canEvaluate(t)==null){ |
115 | 0 | throw new EvaluationException("Cannot evaluate expression"); |
116 | } | |
117 | ||
118 | 0 | int r=((Integer)(t[0].evaluate(env))).compareTo((Integer)t[1].evaluate(env)); |
119 | 0 | boolean b=false; |
120 | ||
121 | 0 | switch(mode){ |
122 | 0 | case EQ_MODE: b=r==0; |
123 | 0 | break; |
124 | 0 | case GE_MODE: b=r>=0; |
125 | 0 | break; |
126 | 0 | case LE_MODE: b=r<=0; |
127 | 0 | break; |
128 | 0 | case GT_MODE: b=r>0; |
129 | 0 | break; |
130 | 0 | case LT_MODE: b=r<0; |
131 | 0 | break; |
132 | 0 | default: |
133 | } | |
134 | ||
135 | 0 | return new Boolean(b); |
136 | } | |
137 | } | |
138 |
|