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     */
017    package org.apache.camel.language.simple.types;
018    
019    /**
020     * Types of binary operators supported
021     */
022    public enum BinaryOperatorType {
023    
024        EQ, GT, GTE, LT, LTE, NOT_EQ, CONTAINS, NOT_CONTAINS, REGEX, NOT_REGEX,
025        IN, NOT_IN, IS, NOT_IS, RANGE, NOT_RANGE;
026    
027        public static BinaryOperatorType asOperator(String text) {
028            if ("==".equals(text)) {
029                return EQ;
030            } else if (">".equals(text)) {
031                return GT;
032            } else if (">=".equals(text)) {
033                return GTE;
034            } else if ("<".equals(text)) {
035                return LT;
036            } else if ("<=".equals(text)) {
037                return LTE;
038            } else if ("!=".equals(text)) {
039                return NOT_EQ;
040            } else if ("contains".equals(text)) {
041                return CONTAINS;
042            } else if ("not contains".equals(text)) {
043                return NOT_CONTAINS;
044            } else if ("regex".equals(text)) {
045                return REGEX;
046            } else if ("not regex".equals(text)) {
047                return NOT_REGEX;
048            } else if ("in".equals(text)) {
049                return IN;
050            } else if ("not in".equals(text)) {
051                return NOT_IN;
052            } else if ("is".equals(text)) {
053                return IS;
054            } else if ("not is".equals(text)) {
055                return NOT_IS;
056            } else if ("range".equals(text)) {
057                return RANGE;
058            } else if ("not range".equals(text)) {
059                return NOT_RANGE;
060            }
061            throw new IllegalArgumentException("Operator not supported: " + text);
062        }
063    
064        public static String getOperatorText(BinaryOperatorType operator) {
065            if (operator == EQ) {
066                return "==";
067            } else if (operator == GT) {
068                return ">";
069            } else if (operator == GTE) {
070                return ">=";
071            } else if (operator == LT) {
072                return "<";
073            } else if (operator == LTE) {
074                return "<=";
075            } else if (operator == NOT_EQ) {
076                return "!=";
077            } else if (operator == CONTAINS) {
078                return "contains";
079            } else if (operator == NOT_CONTAINS) {
080                return "not contains";
081            } else if (operator == REGEX) {
082                return "regex";
083            } else if (operator == NOT_REGEX) {
084                return "not regex";
085            } else if (operator == IN) {
086                return "in";
087            } else if (operator == NOT_IN) {
088                return "not in";
089            } else if (operator == IS) {
090                return "is";
091            } else if (operator == NOT_IS) {
092                return "not is";
093            } else if (operator == RANGE) {
094                return "range";
095            } else if (operator == NOT_RANGE) {
096                return "not range";
097            }
098            return "";
099        }
100    
101        /**
102         * Parameter types a binary operator supports on the right hand side.
103         * <ul>
104         *     <li>Literal - Only literals enclosed by single quotes</li>
105         *     <li>LiteralWithFunction - literals which may have embedded functions enclosed by single quotes</li>
106         *     <li>Function - A function</li>
107         *     <li>NumericValue - A numeric value</li>
108         *     <li>BooleanValue - A boolean value</li>
109         *     <li>NullValue - A null value</li>
110         * </ul>
111         */
112        public enum ParameterType {
113            Literal, LiteralWithFunction, Function, NumericValue, BooleanValue, NullValue;
114    
115            public boolean isLiteralSupported() {
116                return this == Literal;
117            }
118    
119            public boolean isLiteralWithFunctionSupport() {
120                return this == LiteralWithFunction;
121            }
122    
123            public boolean isFunctionSupport() {
124                return this == Function;
125            }
126    
127            public boolean isNumericValueSupported() {
128                return this == NumericValue;
129            }
130    
131            public boolean isBooleanValueSupported() {
132                return this == BooleanValue;
133            }
134    
135            public boolean isNullValueSupported() {
136                return this == NullValue;
137            }
138        }
139    
140        /**
141         * Returns the types of right hand side parameters this operator supports.
142         *
143         * @param operator the operator
144         * @return <tt>null</tt> if accepting all types, otherwise the array of accepted types
145         */
146        public static ParameterType[] supportedParameterTypes(BinaryOperatorType operator) {
147            if (operator == EQ) {
148                return null;
149            } else if (operator == GT) {
150                return null;
151            } else if (operator == GTE) {
152                return null;
153            } else if (operator == LT) {
154                return null;
155            } else if (operator == LTE) {
156                return null;
157            } else if (operator == NOT_EQ) {
158                return null;
159            } else if (operator == CONTAINS) {
160                return null;
161            } else if (operator == NOT_CONTAINS) {
162                return null;
163            } else if (operator == REGEX) {
164                return new ParameterType[]{ParameterType.Literal, ParameterType.Function};
165            } else if (operator == NOT_REGEX) {
166                return new ParameterType[]{ParameterType.Literal, ParameterType.Function};
167            } else if (operator == IN) {
168                return null;
169            } else if (operator == NOT_IN) {
170                return null;
171            } else if (operator == IS) {
172                return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
173            } else if (operator == NOT_IS) {
174                return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
175            } else if (operator == RANGE) {
176                return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
177            } else if (operator == NOT_RANGE) {
178                return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
179            }
180            return null;
181        }
182    
183        @Override
184        public String toString() {
185            return getOperatorText(this);
186        }
187    
188    }