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