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