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 *
32 */
33 @Immutable
34 public class Join implements Source {
35 private static final long serialVersionUID = 1L;
36
37 private final Source left;
38 private final Source right;
39 private final JoinType type;
40 private final JoinCondition joinCondition;
41 private final int hc;
42
43 /**
44 * Create a join of the left and right sources, using the supplied join condition. The outputs of the left and right sources
45 * are expected to be equivalent.
46 *
47 * @param left the left source being joined
48 * @param type the type of join
49 * @param right the right source being joined
50 * @param joinCondition the join condition
51 */
52 public Join( Source left,
53 JoinType type,
54 Source right,
55 JoinCondition joinCondition ) {
56 CheckArg.isNotNull(left, "left");
57 CheckArg.isNotNull(right, "right");
58 CheckArg.isNotNull(type, "type");
59 CheckArg.isNotNull(joinCondition, "joinCondition");
60 this.left = left;
61 this.right = right;
62 this.type = type;
63 this.joinCondition = joinCondition;
64 this.hc = HashCode.compute(this.left, this.right, this.type, this.joinCondition);
65 }
66
67 /**
68 * Get the source that represents the left-hand-side of the join.
69 *
70 * @return the left-side source; never null
71 */
72 public Source left() {
73 return left;
74 }
75
76 /**
77 * Get the source that represents the right-hand-side of the join.
78 *
79 * @return the right-side source; never null
80 */
81 public Source right() {
82 return right;
83 }
84
85 /**
86 * Get the type of join.
87 *
88 * @return the join type; never null
89 */
90 public final JoinType type() {
91 return type;
92 }
93
94 /**
95 * Get the join condition
96 *
97 * @return the join condition; never null
98 */
99 public JoinCondition joinCondition() {
100 return joinCondition;
101 }
102
103 /**
104 * {@inheritDoc}
105 *
106 * @see java.lang.Object#toString()
107 */
108 @Override
109 public String toString() {
110 return Visitors.readable(this);
111 }
112
113 /**
114 * {@inheritDoc}
115 *
116 * @see java.lang.Object#hashCode()
117 */
118 @Override
119 public int hashCode() {
120 return hc;
121 }
122
123 /**
124 * {@inheritDoc}
125 *
126 * @see java.lang.Object#equals(java.lang.Object)
127 */
128 @Override
129 public boolean equals( Object obj ) {
130 if (obj == this) return true;
131 if (obj instanceof Join) {
132 Join that = (Join)obj;
133 if (this.hc != that.hc) return false;
134 if (!this.type.equals(that.type)) return false;
135 if (!this.left.equals(that.left)) return false;
136 if (!this.right.equals(that.right)) return false;
137 if (!this.joinCondition.equals(that.joinCondition)) return false;
138 return true;
139 }
140 return false;
141 }
142
143 /**
144 * {@inheritDoc}
145 *
146 * @see org.modeshape.graph.query.model.Visitable#accept(org.modeshape.graph.query.model.Visitor)
147 */
148 public void accept( Visitor visitor ) {
149 visitor.visit(this);
150 }
151 }