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