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.HashCode;
29
30 /**
31 * A dynamic operand that evaluates to the value(s) of a single or any reference property on a selector, used in a
32 * {@link Comparison} constraint.
33 */
34 @Immutable
35 public class ReferenceValue implements DynamicOperand {
36 private static final long serialVersionUID = 1L;
37
38 private final Set<SelectorName> selectorNames;
39 private final String propertyName;
40 private final int hc;
41
42 /**
43 * Create a dynamic operand that evaluates to all of the reference values of the node identified by the selector.
44 *
45 * @param selectorName the name of the selector
46 * @throws IllegalArgumentException if the selector name is null
47 */
48 public ReferenceValue( SelectorName selectorName ) {
49 this.selectorNames = SelectorName.nameSetFrom(selectorName);
50 this.propertyName = null;
51 this.hc = HashCode.compute(selectorName, this.propertyName);
52 }
53
54 /**
55 * Create a dynamic operand that evaluates to the values of a single reference property of the node identified by the
56 * selector.
57 *
58 * @param selectorName the name of the selector
59 * @param propertyName the name of the property
60 * @throws IllegalArgumentException if the selector name is null
61 */
62 public ReferenceValue( SelectorName selectorName,
63 String propertyName ) {
64 this.selectorNames = SelectorName.nameSetFrom(selectorName);
65 this.propertyName = propertyName;
66 this.hc = HashCode.compute(selectorName, this.propertyName);
67 }
68
69 /**
70 * Get the selector symbol upon which this operand applies.
71 *
72 * @return the one selector names used by this operand; never null
73 */
74 public SelectorName selectorName() {
75 return selectorNames().iterator().next();
76 }
77
78 /**
79 * {@inheritDoc}
80 *
81 * @see org.modeshape.graph.query.model.DynamicOperand#selectorNames()
82 */
83 public Set<SelectorName> selectorNames() {
84 return selectorNames;
85 }
86
87 /**
88 * Get the name of the one reference property.
89 *
90 * @return the property name; or null if this operand applies to any reference property
91 */
92 public final String propertyName() {
93 return propertyName;
94 }
95
96 /**
97 * {@inheritDoc}
98 *
99 * @see java.lang.Object#toString()
100 */
101 @Override
102 public String toString() {
103 return Visitors.readable(this);
104 }
105
106 /**
107 * {@inheritDoc}
108 *
109 * @see java.lang.Object#hashCode()
110 */
111 @Override
112 public int hashCode() {
113 return hc;
114 }
115
116 /**
117 * {@inheritDoc}
118 *
119 * @see java.lang.Object#equals(java.lang.Object)
120 */
121 @Override
122 public boolean equals( Object obj ) {
123 if (obj == this) return true;
124 if (obj instanceof ReferenceValue) {
125 ReferenceValue that = (ReferenceValue)obj;
126 if (this.hc != that.hc) return false;
127 if (!this.selectorNames().equals(that.selectorNames())) return false;
128 if (this.propertyName != null) {
129 return this.propertyName.equals(that.propertyName);
130 }
131 return that.propertyName == null;
132 }
133 return false;
134 }
135
136 /**
137 * {@inheritDoc}
138 *
139 * @see org.modeshape.graph.query.model.Visitable#accept(org.modeshape.graph.query.model.Visitor)
140 */
141 public void accept( Visitor visitor ) {
142 visitor.visit(this);
143 }
144 }