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;
018    
019    /**
020     * Operators supported by simple language
021     * <ul>
022     *   <li>== : equlas</li>
023     *   <li>> : greather than</li>
024     *   <li>>= : greather than or equals</li>
025     *   <li>< : less than</li>
026     *   <li><= : less than or equals</li>
027     *   <li>!= : not</li>
028     *   <li>contains : tested for if it contains the value</li>
029     *   <li>not contains : tested for if it does not contain the value</li>
030     *   <li>regex : matching a regular expression</li>
031     *   <li>not regex : not matching a regular expression</li>
032     *   <li>in : tested for in a list of values separated by comma</li>
033     *   <li>not in : tested for not in a list of values separated by comma</li>
034     *   <li>is : tested for if type is an instanceof the given type</li>
035     *   <li>not is: tested for not if type is an instanceof the given type</li>
036     *   <li>range : tested for if it is within the provided range</li>
037     *   <li>not range : tested for not if it is within the provided range</li>
038     *   <li>and : and operator to combine two groups of expressions</li>
039     *   <li>or : or operator to combine two groups of expressions</li>
040     * </ul>
041     * <p/>
042     * The <tt>and</tt> and <tt>or</tt> operator is special as they are used as optional operator to combine two expressions.
043     * This allows you to build combiled expressions. Currently only one and/or operator is supported, but this might change
044     * in the future.
045     * <br/>
046     * For example we can create this compound expression that has two groups that is combined with the and operator:
047     * <tt>${in.header.action} == 'login' and ${in.header.password} != null</tt>
048     * <br/>
049     */
050    public enum SimpleLangaugeOperator {
051    
052        EQ, GT, GTE, LT, LTE, NOT, CONTAINS, NOT_CONTAINS, REGEX, NOT_REGEX,
053        IN, NOT_IN, IS, NOT_IS, RANGE, NOT_RANGE, AND, OR;
054    
055        public static SimpleLangaugeOperator asOperator(String text) {
056            if ("==".equals(text)) {
057                return EQ;
058            } else if (">".equals(text)) {
059                return GT;
060            } else if (">=".equals(text)) {
061                return GTE;
062            } else if ("<".equals(text)) {
063                return LT;
064            } else if ("<=".equals(text)) {
065                return LTE;
066            } else if ("!=".equals(text)) {
067                return NOT;
068            } else if ("contains".equals(text)) {
069                return CONTAINS;
070            } else if ("not contains".equals(text)) {
071                return NOT_CONTAINS;
072            } else if ("regex".equals(text)) {
073                return REGEX;
074            } else if ("not regex".equals(text)) {
075                return NOT_REGEX;
076            } else if ("in".equals(text)) {
077                return IN;
078            } else if ("not in".equals(text)) {
079                return NOT_IN;
080            } else if ("is".equals(text)) {
081                return IS;
082            } else if ("not is".equals(text)) {
083                return NOT_IS;
084            } else if ("range".equals(text)) {
085                return RANGE;
086            } else if ("not range".equals(text)) {
087                return NOT_RANGE;
088            } else if ("and".equals(text)) {
089                return AND;
090            } else if ("or".equals(text)) {
091                return OR;
092            }
093            throw new IllegalArgumentException("Operator not supported: " + text);
094        }
095    
096        public String getOperatorText(SimpleLangaugeOperator operator) {
097            if (operator == EQ) {
098                return "==";
099            } else if (operator == GT) {
100                return ">";
101            } else if (operator == GTE) {
102                return ">=";
103            } else if (operator == LT) {
104                return "<";
105            } else if (operator == LTE) {
106                return "<=";
107            } else if (operator == NOT) {
108                return "!=";
109            } else if (operator == CONTAINS) {
110                return "contains";
111            } else if (operator == NOT_CONTAINS) {
112                return "not contains";
113            } else if (operator == REGEX) {
114                return "regex";
115            } else if (operator == NOT_REGEX) {
116                return "not regex";
117            } else if (operator == IN) {
118                return "in";
119            } else if (operator == NOT_IN) {
120                return "not in";
121            } else if (operator == IS) {
122                return "is";
123            } else if (operator == NOT_IS) {
124                return "not is";
125            } else if (operator == RANGE) {
126                return "range";
127            } else if (operator == NOT_RANGE) {
128                return "not range";
129            } else if (operator == AND) {
130                return "and";
131            } else if (operator == OR) {
132                return "or";
133            }
134            return "";
135        }
136    
137        @Override
138        public String toString() {
139            return getOperatorText(this);
140        }
141    }