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
29 /**
30 * A constraint requiring that the selected node is a descendant of the node reachable by the supplied absolute path
31 */
32 @Immutable
33 public class DescendantNode extends Constraint {
34 private static final long serialVersionUID = 1L;
35
36 private final SelectorName selectorName;
37 private final String ancestorPath;
38
39 /**
40 * Create a constraint requiring that the node identified by the selector is a descendant of the node reachable by the
41 * supplied absolute path.
42 *
43 * @param selectorName the name of the selector
44 * @param ancestorPath the absolute path to the ancestor
45 */
46 public DescendantNode( SelectorName selectorName,
47 String ancestorPath ) {
48 CheckArg.isNotNull(selectorName, "selectorName");
49 CheckArg.isNotNull(ancestorPath, "ancestorPath");
50 this.selectorName = selectorName;
51 this.ancestorPath = ancestorPath;
52 }
53
54 /**
55 * Get the name of the selector for the node.
56 *
57 * @return the selector name; never null
58 */
59 public final SelectorName getSelectorName() {
60 return selectorName;
61 }
62
63 /**
64 * Get the path of the node that is to be the ancestor of the target node.
65 *
66 * @return the path of the ancestor node; never null
67 */
68 public final String getAncestorPath() {
69 return ancestorPath;
70 }
71
72 /**
73 * {@inheritDoc}
74 *
75 * @see java.lang.Object#toString()
76 */
77 @Override
78 public String toString() {
79 return Visitors.readable(this);
80 }
81
82 /**
83 * {@inheritDoc}
84 *
85 * @see java.lang.Object#hashCode()
86 */
87 @Override
88 public int hashCode() {
89 return getSelectorName().hashCode();
90 }
91
92 /**
93 * {@inheritDoc}
94 *
95 * @see java.lang.Object#equals(java.lang.Object)
96 */
97 @Override
98 public boolean equals( Object obj ) {
99 if (obj == this) return true;
100 if (obj instanceof DescendantNode) {
101 DescendantNode that = (DescendantNode)obj;
102 if (!this.selectorName.equals(that.selectorName)) return false;
103 if (!this.ancestorPath.equals(that.ancestorPath)) return false;
104 return true;
105 }
106 return false;
107 }
108
109 /**
110 * {@inheritDoc}
111 *
112 * @see org.modeshape.graph.query.model.Visitable#accept(org.modeshape.graph.query.model.Visitor)
113 */
114 public void accept( Visitor visitor ) {
115 visitor.visit(this);
116 }
117 }