001/*
002        $Id: Operator.java 3207 2009-04-09 06:48:11Z gregory $
003
004        Copyright (C) 2006 Gregory Vincic, Olle Mansson
005        Copyright (C) 2007 Gregory Vincic
006
007        This file is part of Proteios.
008        Available at http://www.proteios.org/
009
010        Proteios is free software; you can redistribute it and/or modify it
011        under the terms of the GNU General Public License as published by
012        the Free Software Foundation; either version 2 of the License, or
013        (at your option) any later version.
014
015        Proteios is distributed in the hope that it will be useful, but
016        WITHOUT ANY WARRANTY; without even the implied warranty of
017        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018        General Public License for more details.
019
020        You should have received a copy of the GNU General Public License
021        along with this program; if not, write to the Free Software
022        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
023        02111-1307, USA.
024*/
025package org.proteios.core;
026
027import org.proteios.core.query.Expression;
028import org.proteios.core.query.Restriction;
029import org.proteios.core.query.Restrictions;
030import java.util.HashMap;
031import java.util.Map;
032
033/**
034        @author Nicklas
035        @version 2.0
036*/
037public enum Operator
038{
039        AND(1, "AND", "AND", false)
040        {
041                @Override
042                public Restriction getRestriction(Expression lvalue, Expression rvalue)
043                        throws InvalidDataException
044                {
045                        throw new InvalidDataException("Not allowed");
046                }
047        },
048
049        OR(2, "OR", "OR", false)
050        {
051                @Override
052                public Restriction getRestriction(Expression lvalue, Expression rvalue)
053                        throws InvalidDataException
054                {
055                        throw new InvalidDataException("Not allowed");
056                }
057        },
058
059        NOT(3, "NOT", "NOT", false)
060        {
061                @Override
062                public Restriction getRestriction(Expression lvalue, Expression rvalue)
063                        throws InvalidDataException
064                {
065                        throw new InvalidDataException("Not allowed");
066                }
067        },
068
069        EQ(4, "=", "=", true)
070        {
071                @Override
072                public Restriction getRestriction(Expression lvalue, Expression rvalue)
073                        throws InvalidDataException
074                {
075                        return Restrictions.eq(lvalue, rvalue);
076                }
077        },
078
079        NEQ(5, "!=", "!=", true)
080        {
081                @Override
082                public Restriction getRestriction(Expression lvalue, Expression rvalue)
083                        throws InvalidDataException
084                {
085                        return Restrictions.neq(lvalue, rvalue);
086                }
087        },
088
089        LT(6, "<", "<", true)
090        {
091                @Override
092                public Restriction getRestriction(Expression lvalue, Expression rvalue)
093                        throws InvalidDataException
094                {
095                        return Restrictions.lt(lvalue, rvalue);
096                }
097        },
098
099        LTEQ(7, "<=", "<=", true)
100        {
101                @Override
102                public Restriction getRestriction(Expression lvalue, Expression rvalue)
103                        throws InvalidDataException
104                {
105                        return Restrictions.lteq(lvalue, rvalue);
106                }
107        },
108
109        GT(8, ">", ">", true)
110        {
111                @Override
112                public Restriction getRestriction(Expression lvalue, Expression rvalue)
113                        throws InvalidDataException
114                {
115                        return Restrictions.gt(lvalue, rvalue);
116                }
117        },
118
119        GTEQ(9, ">=", ">=", true)
120        {
121                @Override
122                public Restriction getRestriction(Expression lvalue, Expression rvalue)
123                        throws InvalidDataException
124                {
125                        return Restrictions.gteq(lvalue, rvalue);
126                }
127        },
128
129        LIKE(10, "LIKE", "LIKE", true)
130        {
131                @Override
132                public Restriction getRestriction(Expression lvalue, Expression rvalue)
133                        throws InvalidDataException
134                {
135                        return Restrictions.like(lvalue, rvalue);
136                }
137        };
138
139        private static final Map<Integer, Operator> valueMapping = new HashMap<Integer, Operator>();
140        
141        static
142        {
143                for(Operator op : Operator.values())
144                {
145                        Operator o = valueMapping.put(op.getValue(), op);
146                        assert o == null : "Another operator with the value "+op.getValue()+" already exists";
147                }
148        }
149
150        private final int value;
151        private final String symbol;
152        private final String sqlSymbol;
153        private final boolean expressionOperator;
154
155        private Operator(int value, String symbol, String sqlSymbol, boolean expressionOperator)
156        {
157                this.value = value;
158                this.symbol = symbol;
159                this.sqlSymbol = sqlSymbol;
160                this.expressionOperator = expressionOperator;
161        }
162
163        /**
164                Get the integer value that is used when storing an operator in the database.
165        */
166        public int getValue()
167        {
168                return value;
169        }
170        
171        /**
172                Get the symbol for this operator, useful for display in client applications.
173        */
174        public String getSymbol()
175        {
176                return symbol;
177        }
178
179        /**
180                Get the SQL symbol for this operator. This can be used when creating 
181                SQL/HQL queries.
182        */
183        public String getSqlSymbol()
184        {
185                return sqlSymbol;
186        }
187        
188        public boolean isExpressionOperator()
189        {
190                return expressionOperator;
191        }
192        
193        public abstract Restriction getRestriction(Expression lvalue, Expression rvalue)
194                throws InvalidDataException;
195
196        /**
197                Get the <code>Operator</code> object when you know the integer code.
198        */
199        public static Operator fromValue(int value)
200        {
201                Operator op = valueMapping.get(value);
202                assert op != null : "operator == null for value "+value;
203                return op;
204        }
205
206}