View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.query.model;
25  
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.Map;
29  import org.modeshape.common.util.CheckArg;
30  
31  /**
32   * 
33   */
34  public enum Operator {
35      EQUAL_TO("="),
36      NOT_EQUAL_TO("!="),
37      LESS_THAN("<"),
38      LESS_THAN_OR_EQUAL_TO("<="),
39      GREATER_THAN(">"),
40      GREATER_THAN_OR_EQUAL_TO(">="),
41      LIKE("LIKE");
42  
43      private static final Map<String, Operator> OPERATORS_BY_SYMBOL;
44      static {
45          Map<String, Operator> opsBySymbol = new HashMap<String, Operator>();
46          for (Operator operator : Operator.values()) {
47              opsBySymbol.put(operator.getSymbol().toUpperCase(), operator);
48          }
49          opsBySymbol.put("<>", NOT_EQUAL_TO);
50          OPERATORS_BY_SYMBOL = Collections.unmodifiableMap(opsBySymbol);
51      }
52  
53      private final String symbol;
54  
55      private Operator( String symbol ) {
56          this.symbol = symbol;
57      }
58  
59      /**
60       * Get the symbol for this operator
61       * 
62       * @return the symbolic representation; never null
63       */
64      public String getSymbol() {
65          return symbol;
66      }
67  
68      /**
69       * Get the equivalent operator if the operands are to be reversed.
70       * 
71       * @return the reverse operator; never null
72       */
73      public Operator getReverse() {
74          switch (this) {
75              case GREATER_THAN:
76                  return LESS_THAN;
77              case GREATER_THAN_OR_EQUAL_TO:
78                  return LESS_THAN_OR_EQUAL_TO;
79              case LESS_THAN:
80                  return GREATER_THAN;
81              case LESS_THAN_OR_EQUAL_TO:
82                  return GREATER_THAN_OR_EQUAL_TO;
83              case EQUAL_TO:
84              case LIKE:
85              case NOT_EQUAL_TO:
86              default:
87                  return this;
88          }
89      }
90  
91      /**
92       * Determine whether this operator is one that is used to define a range of values: {@link #LESS_THAN <},
93       * {@link #GREATER_THAN >}, {@link #LESS_THAN_OR_EQUAL_TO <=}, or {@link #GREATER_THAN_OR_EQUAL_TO >=}.
94       * 
95       * @return true if this operator is a range operator, or false otherwise
96       */
97      public boolean isRangeOperator() {
98          switch (this) {
99              case GREATER_THAN:
100             case GREATER_THAN_OR_EQUAL_TO:
101             case LESS_THAN:
102             case LESS_THAN_OR_EQUAL_TO:
103                 return true;
104             case EQUAL_TO:
105             case LIKE:
106             case NOT_EQUAL_TO:
107             default:
108                 return false;
109         }
110     }
111 
112     /**
113      * {@inheritDoc}
114      * 
115      * @see java.lang.Enum#toString()
116      */
117     @Override
118     public String toString() {
119         return symbol;
120     }
121 
122     /**
123      * Attempt to find the Operator given a symbol. The matching is done independent of case.
124      * 
125      * @param symbol the symbol
126      * @return the Operator having the supplied symbol, or null if there is no Operator with the supplied symbol
127      * @throws IllegalArgumentException if the symbol is null
128      */
129     public static Operator forSymbol( String symbol ) {
130         CheckArg.isNotNull(symbol, "symbol");
131         return OPERATORS_BY_SYMBOL.get(symbol.toUpperCase());
132     }
133 }