001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.language.simple.types;
018
019/**
020 * Types of binary operators supported
021 */
022public enum BinaryOperatorType {
023
024    EQ, EQ_IGNORE, GT, GTE, LT, LTE, NOT_EQ, CONTAINS, NOT_CONTAINS, 
025    CONTAINS_IGNORECASE, REGEX, NOT_REGEX,
026    IN, NOT_IN, IS, NOT_IS, RANGE, NOT_RANGE, STARTS_WITH, ENDS_WITH;
027
028    public static BinaryOperatorType asOperator(String text) {
029        if ("==".equals(text)) {
030            return EQ;
031        } else if ("=~".equals(text)) {
032            return EQ_IGNORE;
033        } else if (">".equals(text)) {
034            return GT;
035        } else if (">=".equals(text)) {
036            return GTE;
037        } else if ("<".equals(text)) {
038            return LT;
039        } else if ("<=".equals(text)) {
040            return LTE;
041        } else if ("!=".equals(text)) {
042            return NOT_EQ;
043        } else if ("contains".equals(text)) {
044            return CONTAINS;
045        } else if ("not contains".equals(text)) {
046            return NOT_CONTAINS;
047        } else if ("~~".equals(text)) {
048            return CONTAINS_IGNORECASE;
049        } else if ("regex".equals(text)) {
050            return REGEX;
051        } else if ("not regex".equals(text)) {
052            return NOT_REGEX;
053        } else if ("in".equals(text)) {
054            return IN;
055        } else if ("not in".equals(text)) {
056            return NOT_IN;
057        } else if ("is".equals(text)) {
058            return IS;
059        } else if ("not is".equals(text)) {
060            return NOT_IS;
061        } else if ("range".equals(text)) {
062            return RANGE;
063        } else if ("not range".equals(text)) {
064            return NOT_RANGE;
065        } else if ("starts with".equals(text)) {
066            return STARTS_WITH;
067        } else if ("ends with".equals(text)) {
068            return ENDS_WITH;
069        }
070        throw new IllegalArgumentException("Operator not supported: " + text);
071    }
072
073    public static String getOperatorText(BinaryOperatorType operator) {
074        if (operator == EQ) {
075            return "==";
076        } else if (operator == EQ_IGNORE) {
077            return "=~";
078        } else if (operator == GT) {
079            return ">";
080        } else if (operator == GTE) {
081            return ">=";
082        } else if (operator == LT) {
083            return "<";
084        } else if (operator == LTE) {
085            return "<=";
086        } else if (operator == NOT_EQ) {
087            return "!=";
088        } else if (operator == CONTAINS) {
089            return "contains";
090        } else if (operator == NOT_CONTAINS) {
091            return "not contains";
092        } else if (operator == CONTAINS_IGNORECASE) {
093            return "~~";
094        } else if (operator == REGEX) {
095            return "regex";
096        } else if (operator == NOT_REGEX) {
097            return "not regex";
098        } else if (operator == IN) {
099            return "in";
100        } else if (operator == NOT_IN) {
101            return "not in";
102        } else if (operator == IS) {
103            return "is";
104        } else if (operator == NOT_IS) {
105            return "not is";
106        } else if (operator == RANGE) {
107            return "range";
108        } else if (operator == NOT_RANGE) {
109            return "not range";
110        } else if (operator == STARTS_WITH) {
111            return "starts with";
112        } else if (operator == ENDS_WITH) {
113            return "ends with";
114        }
115        return "";
116    }
117
118    /**
119     * Parameter types a binary operator supports on the right hand side.
120     * <ul>
121     *     <li>Literal - Only literals enclosed by single quotes</li>
122     *     <li>LiteralWithFunction - literals which may have embedded functions enclosed by single quotes</li>
123     *     <li>Function - A function</li>
124     *     <li>NumericValue - A numeric value</li>
125     *     <li>BooleanValue - A boolean value</li>
126     *     <li>NullValue - A null value</li>
127     * </ul>
128     */
129    public enum ParameterType {
130        Literal, LiteralWithFunction, Function, NumericValue, BooleanValue, NullValue, MinusValue;
131
132        public boolean isLiteralSupported() {
133            return this == Literal;
134        }
135
136        public boolean isLiteralWithFunctionSupport() {
137            return this == LiteralWithFunction;
138        }
139
140        public boolean isFunctionSupport() {
141            return this == Function;
142        }
143
144        public boolean isNumericValueSupported() {
145            return this == NumericValue;
146        }
147
148        public boolean isBooleanValueSupported() {
149            return this == BooleanValue;
150        }
151
152        public boolean isNullValueSupported() {
153            return this == NullValue;
154        }
155        
156        public boolean isMinusValueSupported() {
157            return this == MinusValue;
158        }
159    }
160
161    /**
162     * Returns the types of right hand side parameters this operator supports.
163     *
164     * @param operator the operator
165     * @return <tt>null</tt> if accepting all types, otherwise the array of accepted types
166     */
167    public static ParameterType[] supportedParameterTypes(BinaryOperatorType operator) {
168        if (operator == EQ) {
169            return null;
170        } else if (operator == EQ_IGNORE) {
171            return null;
172        } else if (operator == GT) {
173            return null;
174        } else if (operator == GTE) {
175            return null;
176        } else if (operator == LT) {
177            return null;
178        } else if (operator == LTE) {
179            return null;
180        } else if (operator == NOT_EQ) {
181            return null;
182        } else if (operator == CONTAINS) {
183            return null;
184        } else if (operator == NOT_CONTAINS) {
185            return null;
186        } else if (operator == CONTAINS_IGNORECASE) {
187            return null;
188        } else if (operator == REGEX) {
189            return new ParameterType[]{ParameterType.Literal, ParameterType.Function};
190        } else if (operator == NOT_REGEX) {
191            return new ParameterType[]{ParameterType.Literal, ParameterType.Function};
192        } else if (operator == IN) {
193            return null;
194        } else if (operator == NOT_IN) {
195            return null;
196        } else if (operator == IS) {
197            return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
198        } else if (operator == NOT_IS) {
199            return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
200        } else if (operator == RANGE) {
201            return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
202        } else if (operator == NOT_RANGE) {
203            return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
204        } else if (operator == STARTS_WITH) {
205            return null;
206        } else if (operator == ENDS_WITH) {
207            return null;
208        }
209        return null;
210    }
211
212    @Override
213    public String toString() {
214        return getOperatorText(this);
215    }
216
217}