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 net.jcip.annotations.Immutable;
27 import org.modeshape.common.util.CheckArg;
28 import org.modeshape.common.util.HashCode;
29
30 /**
31 * A constraint that evaluates to true when the defined operation evaluates to true.
32 */
33 @Immutable
34 public class Comparison implements Constraint {
35
36 private static final long serialVersionUID = 1L;
37
38 private final DynamicOperand operand1;
39 private final StaticOperand operand2;
40 private final Operator operator;
41 private final int hc;
42
43 public Comparison( DynamicOperand operand1,
44 Operator operator,
45 StaticOperand operand2 ) {
46 CheckArg.isNotNull(operand1, "operand1");
47 CheckArg.isNotNull(operator, "operator");
48 CheckArg.isNotNull(operand2, "operand2");
49 this.operand1 = operand1;
50 this.operand2 = operand2;
51 this.operator = operator;
52 this.hc = HashCode.compute(this.operand1, this.operand2, this.operator);
53 }
54
55 /**
56 * Get the dynamic operand of this comparison.
57 *
58 * @return the dynamic operand; never null
59 */
60 public DynamicOperand operand1() {
61 return operand1;
62 }
63
64 /**
65 * Get the dynamic operand of this comparison.
66 *
67 * @return the dynamic operand; never null
68 */
69 public StaticOperand operand2() {
70 return operand2;
71 }
72
73 /**
74 * Get the operator for this comparison
75 *
76 * @return the operator; never null
77 */
78 public final Operator operator() {
79 return operator;
80 }
81
82 /**
83 * {@inheritDoc}
84 *
85 * @see java.lang.Object#toString()
86 */
87 @Override
88 public String toString() {
89 return Visitors.readable(this);
90 }
91
92 /**
93 * {@inheritDoc}
94 *
95 * @see java.lang.Object#hashCode()
96 */
97 @Override
98 public int hashCode() {
99 return hc;
100 }
101
102 /**
103 * {@inheritDoc}
104 *
105 * @see java.lang.Object#equals(java.lang.Object)
106 */
107 @Override
108 public boolean equals( Object obj ) {
109 if (obj == this) return true;
110 if (obj instanceof Comparison) {
111 Comparison that = (Comparison)obj;
112 if (this.hc != that.hc) return false;
113 if (!this.operator.equals(that.operator)) return false;
114 if (!this.operand1.equals(that.operand1)) return false;
115 if (!this.operand2.equals(that.operand2)) return false;
116 return true;
117 }
118 return false;
119 }
120
121 /**
122 * {@inheritDoc}
123 *
124 * @see org.modeshape.graph.query.model.Visitable#accept(org.modeshape.graph.query.model.Visitor)
125 */
126 public void accept( Visitor visitor ) {
127 visitor.visit(this);
128 }
129 }