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.Set;
27  import net.jcip.annotations.Immutable;
28  import org.modeshape.common.util.CheckArg;
29  import org.modeshape.common.util.HashCode;
30  
31  /**
32   * A dynamic operand that represents a (binary) arithmetic operation upon one or more other operands, used in {@link Comparison}
33   * and {@link Ordering} components.
34   */
35  @Immutable
36  public class ArithmeticOperand implements DynamicOperand {
37      private static final long serialVersionUID = 1L;
38  
39      private final Set<SelectorName> selectorNames;
40      private final ArithmeticOperator operator;
41      private final DynamicOperand left;
42      private final DynamicOperand right;
43      private final int hc;
44  
45      /**
46       * Create a arithmetic dynamic operand that operates upon the supplied operand(s).
47       * 
48       * @param left the left-hand-side operand
49       * @param operator the arithmetic operator; may not be null
50       * @param right the right-hand-side operand
51       * @throws IllegalArgumentException if any of the arguments is null
52       */
53      public ArithmeticOperand( DynamicOperand left,
54                                ArithmeticOperator operator,
55                                DynamicOperand right ) {
56          CheckArg.isNotNull(operator, "operator");
57          this.selectorNames = SelectorName.nameSetFrom(left.selectorNames(), right.selectorNames());
58          this.operator = operator;
59          this.left = left;
60          this.right = right;
61          this.hc = HashCode.compute(left, operator, right);
62      }
63  
64      /**
65       * {@inheritDoc}
66       * 
67       * @see org.modeshape.graph.query.model.DynamicOperand#selectorNames()
68       */
69      public Set<SelectorName> selectorNames() {
70          return selectorNames;
71      }
72  
73      /**
74       * Get the operator for this binary operand.
75       * 
76       * @return the operator; never null
77       */
78      public ArithmeticOperator operator() {
79          return operator;
80      }
81  
82      /**
83       * Get the left-hand operand.
84       * 
85       * @return the left-hand operator; never null
86       */
87      public DynamicOperand left() {
88          return left;
89      }
90  
91      /**
92       * Get the right-hand operand.
93       * 
94       * @return the right-hand operator; never null
95       */
96      public DynamicOperand right() {
97          return right;
98      }
99  
100     /**
101      * {@inheritDoc}
102      * 
103      * @see java.lang.Object#toString()
104      */
105     @Override
106     public String toString() {
107         return Visitors.readable(this);
108     }
109 
110     /**
111      * {@inheritDoc}
112      * 
113      * @see java.lang.Object#hashCode()
114      */
115     @Override
116     public int hashCode() {
117         return hc;
118     }
119 
120     /**
121      * {@inheritDoc}
122      * 
123      * @see java.lang.Object#equals(java.lang.Object)
124      */
125     @Override
126     public boolean equals( Object obj ) {
127         if (obj == this) return true;
128         if (obj instanceof ArithmeticOperand) {
129             ArithmeticOperand that = (ArithmeticOperand)obj;
130             return this.operator() == that.operator() && this.left().equals(that.left()) && this.right().equals(that.right());
131         }
132         return false;
133     }
134 
135     /**
136      * {@inheritDoc}
137      * 
138      * @see org.modeshape.graph.query.model.Visitable#accept(org.modeshape.graph.query.model.Visitor)
139      */
140     public void accept( Visitor visitor ) {
141         visitor.visit(this);
142     }
143 }