001: /*
002: * Operation.java
003: *
004: * Created on May 27, 2003, 4:27 PM
005: */
006:
007: package com.sun.portal.rproxy.configservlet.server;
008:
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.LinkedList;
012: import java.util.StringTokenizer;
013:
014: /**
015: *
016: * @author mm132998
017: * @version
018: */
019: class Operation {
020: /*
021: * Operations supported are of following form : " < value" , "> value" ,
022: * "value1 , value2" , value , "*"
023: */
024: public static final int INVALID = -1;
025:
026: public static final int LESS_THAN = 1;
027:
028: public static final int GREATER_THAN = 2;
029:
030: public static final int RANGE = 3;
031:
032: public static final int EXACT_VALUE = 4;
033:
034: public static final int MATCH_ALL = 5;
035:
036: public static final String LESS_THAN_STR = "<";
037:
038: public static final String GREATER_THAN_STR = ">";
039:
040: public static final String RANGE_STR = ",";
041:
042: public static final String MATCH_ALL_STR = "*";
043:
044: private static final LinkedList operators = new LinkedList();
045:
046: private static final HashMap operatorsMap = new HashMap();
047:
048: private static final HashMap unaryMap = new HashMap();
049:
050: private int operator = INVALID;
051:
052: private int leftOperand = -1;
053:
054: private int rightOperand = -1;
055:
056: private boolean unary = false;
057:
058: private int unaryOperand = -1;
059:
060: private String expr = "";
061:
062: static {
063: operators.add(LESS_THAN_STR);
064: operators.add(GREATER_THAN_STR);
065: operators.add(RANGE_STR);
066:
067: operatorsMap.put(LESS_THAN_STR, new Integer(LESS_THAN));
068: operatorsMap.put(GREATER_THAN_STR, new Integer(GREATER_THAN));
069: operatorsMap.put(RANGE_STR, new Integer(RANGE));
070:
071: unaryMap.put(LESS_THAN_STR, new Boolean(true));
072: unaryMap.put(GREATER_THAN_STR, new Boolean(true));
073: unaryMap.put(RANGE_STR, new Boolean(false));
074: }
075:
076: public Operation(String str) {
077:
078: if (str != null) {
079: str = str.trim();
080: expr = str;
081: Iterator iter = operators.iterator();
082: int operation = INVALID;
083: String stringOperation = null;
084: int index = -1;
085:
086: while (iter.hasNext()) {
087: stringOperation = iter.next().toString();
088: index = str.indexOf(stringOperation);
089: if (index != -1) {
090: operation = ((Integer) operatorsMap
091: .get(stringOperation)).intValue();
092: break;
093: }
094: }
095:
096: operator = operation;
097:
098: if (operation != INVALID) {
099: // Ok parse out whether this is a
100: // unary operation , and get its operands.
101: unary = ((Boolean) unaryMap.get(stringOperation))
102: .booleanValue();
103:
104: if (unary) {
105: str = str.substring(index + 1).trim();
106: try {
107: unaryOperand = Integer.parseInt(str);
108: } catch (NumberFormatException nfEx) {
109: if (UserProfileCache.doDebug) {
110: UserProfileCache
111: .writeLog("Unable to parse operation : "
112: + expr);
113: UserProfileCache.writeLog(nfEx);
114: }
115: }
116: } else {
117: StringTokenizer stk = new StringTokenizer(str,
118: stringOperation);
119:
120: if (stk.countTokens() == 2) {
121: // Ok get the operands.
122: try {
123: leftOperand = Integer.parseInt(stk
124: .nextToken().trim());
125: rightOperand = Integer.parseInt(stk
126: .nextToken().trim());
127: } catch (NumberFormatException nfEx) {
128: if (UserProfileCache.doDebug) {
129: UserProfileCache
130: .writeLog("Unable to parse operation : "
131: + expr);
132: UserProfileCache.writeLog(nfEx);
133: }
134: }
135: } else {
136: if (UserProfileCache.doDebug) {
137: UserProfileCache
138: .writeLog("Unable to parse operation : "
139: + expr);
140: UserProfileCache
141: .writeLog("binary Operation expects two operands !\n");
142: }
143: operator = INVALID;
144: }
145: }
146: } else {
147: // Check whether this is Star operation.
148: if (expr.equals(MATCH_ALL_STR)) {
149: operator = MATCH_ALL;
150: } else {
151: // Can be an exact match.
152: try {
153: unaryOperand = Integer.parseInt(str);
154: operator = EXACT_VALUE;
155: } catch (NumberFormatException nfEx) {
156: if (UserProfileCache.doDebug) {
157: UserProfileCache
158: .writeLog("Unable to parse operation : "
159: + expr);
160: UserProfileCache.writeLog(nfEx);
161: }
162: }
163: }
164: }
165: }
166: }
167:
168: public boolean isValid() {
169: return INVALID != getOperator();
170: }
171:
172: public int getOperator() {
173: return operator;
174: }
175:
176: public int getLeftOperand() {
177: return leftOperand;
178: }
179:
180: public int getRightOperand() {
181: return rightOperand;
182: }
183:
184: public boolean isUnary() {
185: return unary;
186: }
187:
188: public int getUnaryOperand() {
189: return unaryOperand;
190: }
191:
192: public boolean evaluate(int value) {
193: if (isValid()) {
194: switch (getOperator()) {
195:
196: case LESS_THAN:
197: if (value < unaryOperand) {
198: return true;
199: }
200: break;
201:
202: case GREATER_THAN:
203: if (value > unaryOperand) {
204: return true;
205: }
206: break;
207:
208: case EXACT_VALUE:
209: if (value == unaryOperand) {
210: return true;
211: }
212: break;
213:
214: case MATCH_ALL:
215: return true;
216:
217: case RANGE:
218: if (value >= leftOperand && value <= rightOperand) {
219: return true;
220: }
221: break;
222: default:
223: break;
224: }
225: }
226: return false;
227: }
228: }
|