View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.util;
17  
18  /**
19   * @author Kuali Student Team
20   */
21  public class ExpressionToken implements Cloneable {
22  
23      public static final int OPERATOR_AND = 1;
24      public static final int OPERATOR_OR = 2;
25      public static final int PARENTHESIS_START = 3;
26      public static final int PARENTHESIS_END = 4;
27      public static final int CONDITION = 5;
28  
29      private String value;
30      private int type;
31      private String tokenID = "";
32  
33      public String getTokenID() {
34          return tokenID;
35      }
36  
37      public void setTokenID(String tokenID) {
38          this.tokenID = tokenID;
39      }
40  
41      public int getType() {
42          return type;
43      }
44  
45      public void setType(int type) {
46          this.type = type;
47      }
48  
49      public String getValue() {
50          return value;
51      }
52  
53      public void setValue(String value) {
54          this.value = value;
55      }
56  
57      public static ExpressionToken createAndToken(){
58          ExpressionToken t = new ExpressionToken();
59          t.type = OPERATOR_AND;
60          t.value = "and";
61          return t;
62      }
63  
64      public static ExpressionToken createOrToken(){
65          ExpressionToken t = new ExpressionToken();
66          t.type = OPERATOR_OR;
67          t.value = "or";
68          return t;
69      }
70  
71      public ExpressionToken toggleAndOr(){
72          ExpressionToken t = new ExpressionToken ();
73          if(type == OPERATOR_AND){
74              t.type = OPERATOR_OR;
75              t.value = "Or";
76  
77          }else if(type == OPERATOR_OR){
78              t.type = OPERATOR_AND;
79              t.value = "And";
80          }
81          return t;
82      }
83      
84      public boolean equals(Object obj){
85          if(obj instanceof ExpressionToken == false){
86              return false;
87          }
88          ExpressionToken t = (ExpressionToken)obj;
89          if(t.value == null){
90              return false;
91          }
92          if(t.value.equals(value) && t.type == type){
93              return true;
94          }
95          return false;
96      }
97      
98      public int hashCode(){
99      	int hash =1;
100     	hash = hash * 31 + new Integer(type).hashCode();
101     	hash = hash * 31 + (value == null ? 0 : value.hashCode());
102     	return hash;
103     }
104     
105     public ExpressionToken clone(){
106         ExpressionToken t = new ExpressionToken();
107         t.type = type;
108         t.value = value;
109         return t;
110     }
111 
112     public String toString() {
113         if (type == OPERATOR_AND) {
114             return "and";
115         } else if (type == OPERATOR_OR) {
116             return "or";
117         } else if (type == PARENTHESIS_START) {
118             return "(";
119         } else if (type == PARENTHESIS_END) {
120             return ")";
121         } else if (type == CONDITION) {
122             return value;
123         }
124         return "";
125     }
126 }