1 package org.apache.torque.criteria;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.Serializable;
23
24 import org.apache.commons.lang.builder.EqualsBuilder;
25 import org.apache.commons.lang.builder.HashCodeBuilder;
26 import org.apache.torque.Column;
27
28 /**
29 * Data object to describe a join between two tables, for example
30 * <pre>
31 * table_a LEFT JOIN table_b ON table_a.id = table_b.a_id
32 * </pre>
33 */
34 public class Join implements Serializable
35 {
36 /** Version id for serializing. */
37 private static final long serialVersionUID = 1L;
38
39 /** The join condition, not null. */
40 private Criterion joinCondition = null;
41
42 /**
43 * The left table of the join,
44 * or null to be determined from join condition.
45 */
46 private PreparedStatementPart leftTable = null;
47
48 /**
49 * The right table of the join,
50 * or null to be determined from join condition.
51 */
52 private PreparedStatementPart rightTable = null;
53 /**
54 * The type of the join (LEFT JOIN, ...),
55 * or null for an implicit inner join.
56 */
57 private JoinType joinType = null;
58
59 /**
60 * Constructor with the comparison operator.
61 *
62 * @param leftColumn the left column of the join condition;
63 * might contain an alias name, not null.
64 * @param rightColumn the right column of the join condition
65 * might contain an alias name, not null.
66 * @param comparison the comparison, not null.
67 * The operator CUSTOM is not supported.
68 * @param joinType the type of the join, or null
69 * (adding the join condition to the where clause).
70 *
71 * @throws NullPointerException if leftColumn, comparison or rightColumn
72 * are null.
73 * @throws IllegalArgumentException if comparison id SqlEnum.CUSTOM
74 */
75 @SuppressWarnings("deprecation")
76 public Join(
77 final Column leftColumn,
78 final Column rightColumn,
79 final SqlEnum comparison,
80 final JoinType joinType)
81 {
82 if (leftColumn == null)
83 {
84 throw new NullPointerException("leftColumn is null");
85 }
86 if (rightColumn == null)
87 {
88 throw new NullPointerException("rightColumn is null");
89 }
90 if (comparison == null)
91 {
92 throw new NullPointerException("comparison is null");
93 }
94 if (comparison == SqlEnum.CUSTOM)
95 {
96 throw new IllegalArgumentException(
97 "The comparison SqlEnum.CUSTOM is not supported for Joins.");
98 }
99 this.joinCondition = new Criterion(leftColumn, rightColumn, comparison);
100 this.joinType = joinType;
101 }
102
103 /**
104 * Constructor.
105 *
106 * @param leftTable the left table of the join, might contain an alias name,
107 * or null to be determined from the join clause.
108 * @param rightTable the right table of the join, might contain an alias
109 * name, or null to be determined from the join clause.
110 * @param joinCondition the join condition, not null.
111 * @param joinType the type of the join, or null
112 * (adding the join condition to the where clause).
113 */
114 public Join(
115 final PreparedStatementPart leftTable,
116 final PreparedStatementPart rightTable,
117 final Criterion joinCondition,
118 final JoinType joinType)
119 {
120 if (joinCondition == null)
121 {
122 throw new NullPointerException("joinCondition is null");
123 }
124 this.leftTable = leftTable;
125 this.rightTable = rightTable;
126 this.joinCondition = joinCondition;
127 this.joinType = joinType;
128 }
129
130 /**
131 * @return the type of the join, i.e. SqlEnum.LEFT_JOIN, ...,
132 * or null for adding the join condition to the where Clause
133 */
134 public final Criterion getJoinCondition()
135 {
136 return joinCondition;
137 }
138
139 /**
140 * @return the type of the join, i.e. SqlEnum.LEFT_JOIN, ...,
141 * or null for adding the join condition to the where Clause
142 */
143 public final JoinType getJoinType()
144 {
145 return joinType;
146 }
147
148 /**
149 * @return the left table of the join condition.
150 */
151 public final PreparedStatementPart getLeftTable()
152 {
153 return leftTable;
154 }
155
156 /**
157 * @return the right table of the join condition.
158 */
159 public final PreparedStatementPart getRightTable()
160 {
161 return rightTable;
162 }
163
164 /**
165 * Returns a String representation of the class,
166 * mainly for debugging purposes.
167 *
168 * @return a String representation of the class
169 */
170 @Override
171 public String toString()
172 {
173
174 return joinType + "(" + leftTable + ", " + rightTable + "): "
175 + joinCondition.toString();
176 }
177
178 /**
179 * This method checks another Criteria.Join to see if they contain the
180 * same attributes.
181 */
182 @Override
183 public boolean equals(Object obj)
184 {
185 if (this == obj)
186 {
187 return true;
188 }
189
190 if ((obj == null) || !(obj instanceof Join))
191 {
192 return false;
193 }
194
195 Join join = (Join) obj;
196 return new EqualsBuilder()
197 .append(leftTable, join.leftTable)
198 .append(rightTable, join.rightTable)
199 .append(joinCondition, join.joinCondition)
200 .append(joinType, join.getJoinType())
201 .isEquals();
202 }
203
204 /**
205 * Returns the hash code value for this Join.
206 *
207 * @return a hash code value for this object.
208 */
209 @Override
210 public int hashCode()
211 {
212 return new HashCodeBuilder()
213 .append(leftTable)
214 .append(rightTable)
215 .append(joinCondition)
216 .append(joinType)
217 .toHashCode();
218 }
219 }