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 * The arithmetic operators. 33 */ 34 public enum ArithmeticOperator { 35 36 ADD("+", Arity.BINARY, 20), 37 SUBTRACT("-", Arity.BINARY, 19), 38 MULTIPLY("*", Arity.BINARY, 18), 39 DIVIDE("/", Arity.BINARY, 17); 40 41 public static enum Arity { 42 UNARY, 43 BINARY; 44 } 45 46 private static final Map<String, ArithmeticOperator> OPERATORS_BY_SYMBOL; 47 static { 48 Map<String, ArithmeticOperator> opsBySymbol = new HashMap<String, ArithmeticOperator>(); 49 for (ArithmeticOperator operator : ArithmeticOperator.values()) { 50 opsBySymbol.put(operator.symbol(), operator); 51 } 52 OPERATORS_BY_SYMBOL = Collections.unmodifiableMap(opsBySymbol); 53 } 54 55 private final String symbol; 56 private final Arity arity; 57 private final int precedence; 58 59 private ArithmeticOperator( String symbol, 60 Arity arity, 61 int precedence ) { 62 this.symbol = symbol; 63 this.arity = arity; 64 this.precedence = precedence; 65 } 66 67 /** 68 * Get the symbol for this operator 69 * 70 * @return the symbolic representation; never null 71 */ 72 public String symbol() { 73 return symbol; 74 } 75 76 /** 77 * Get the 'arity' of the operator. 78 * 79 * @return the number of parameters required 80 * @see #isUnary() 81 * @see #isBinary() 82 */ 83 public Arity arity() { 84 return arity; 85 } 86 87 /** 88 * Return whether this is an unary operator. 89 * 90 * @return true if this operator is unary, or false otherwise 91 * @see #arity() 92 * @see #isBinary() 93 */ 94 public boolean isUnary() { 95 return arity == Arity.UNARY; 96 } 97 98 /** 99 * Return whether this is an binary operator. 100 * 101 * @return true if this operator is binary, or false otherwise 102 * @see #arity() 103 * @see #isUnary() 104 */ 105 public boolean isBinary() { 106 return arity == Arity.BINARY; 107 } 108 109 /** 110 * Determine whether this operator has a higher precedence than the supplied operator. 111 * 112 * @param operator the other operator; may not be null 113 * @return true if this operator has a higher precedence, or false otherwise 114 */ 115 public boolean precedes( ArithmeticOperator operator ) { 116 return this.precedence > operator.precedence; 117 } 118 119 /** 120 * {@inheritDoc} 121 * 122 * @see java.lang.Enum#toString() 123 */ 124 @Override 125 public String toString() { 126 return symbol; 127 } 128 129 /** 130 * Attempt to find the Operator given a symbol. The matching is done independent of case. 131 * 132 * @param symbol the symbol 133 * @return the Operator having the supplied symbol, or null if there is no Operator with the supplied symbol 134 * @throws IllegalArgumentException if the symbol is null 135 */ 136 public static ArithmeticOperator forSymbol( String symbol ) { 137 CheckArg.isNotNull(symbol, "symbol"); 138 return OPERATORS_BY_SYMBOL.get(symbol.toUpperCase()); 139 } 140 }