001/** 002 * Copyright 2005-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krms.util; 017 018/** 019 * @author Kuali Student Team 020 */ 021public class ExpressionToken implements Cloneable { 022 023 public static final int OPERATOR_AND = 1; 024 public static final int OPERATOR_OR = 2; 025 public static final int PARENTHESIS_START = 3; 026 public static final int PARENTHESIS_END = 4; 027 public static final int CONDITION = 5; 028 029 private String value; 030 private int type; 031 private String tokenID = ""; 032 033 public String getTokenID() { 034 return tokenID; 035 } 036 037 public void setTokenID(String tokenID) { 038 this.tokenID = tokenID; 039 } 040 041 public int getType() { 042 return type; 043 } 044 045 public void setType(int type) { 046 this.type = type; 047 } 048 049 public String getValue() { 050 return value; 051 } 052 053 public void setValue(String value) { 054 this.value = value; 055 } 056 057 public static boolean isOperator(int type){ 058 if((type==ExpressionToken.OPERATOR_AND)||(type==ExpressionToken.OPERATOR_OR)){ 059 return true; 060 } 061 return false; 062 } 063 064 public static boolean isParenthesis(int type){ 065 if((type==ExpressionToken.PARENTHESIS_START)||(type==PARENTHESIS_END)){ 066 return true; 067 } 068 return false; 069 } 070 071 public static ExpressionToken createAndToken(){ 072 ExpressionToken t = new ExpressionToken(); 073 t.type = OPERATOR_AND; 074 t.value = "and"; 075 return t; 076 } 077 078 public static ExpressionToken createOrToken(){ 079 ExpressionToken t = new ExpressionToken(); 080 t.type = OPERATOR_OR; 081 t.value = "or"; 082 return t; 083 } 084 085 public ExpressionToken toggleAndOr(){ 086 ExpressionToken t = new ExpressionToken (); 087 if(type == OPERATOR_AND){ 088 t.type = OPERATOR_OR; 089 t.value = "Or"; 090 091 }else if(type == OPERATOR_OR){ 092 t.type = OPERATOR_AND; 093 t.value = "And"; 094 } 095 return t; 096 } 097 098 public boolean equals(Object obj){ 099 if(!(obj instanceof ExpressionToken)){ 100 return false; 101 } 102 ExpressionToken t = (ExpressionToken)obj; 103 if(t.value == null){ 104 return false; 105 } 106 if(t.value.equals(value) && t.type == type){ 107 return true; 108 } 109 return false; 110 } 111 112 public int hashCode(){ 113 int hash =1; 114 hash = hash * 31 + Integer.valueOf(type).hashCode(); 115 hash = hash * 31 + (value == null ? 0 : value.hashCode()); 116 return hash; 117 } 118 119 public ExpressionToken clone(){ 120 ExpressionToken t = new ExpressionToken(); 121 t.type = type; 122 t.value = value; 123 return t; 124 } 125 126 public String toString() { 127 if (type == OPERATOR_AND) { 128 return "and"; 129 } else if (type == OPERATOR_OR) { 130 return "or"; 131 } else if (type == PARENTHESIS_START) { 132 return "("; 133 } else if (type == PARENTHESIS_END) { 134 return ")"; 135 } else if (type == CONDITION) { 136 return value; 137 } 138 return ""; 139 } 140}