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