1 package org.molwind.model;
2
3 /*
4 * This file is part of Molwind.
5 *
6 * Molwind is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Molwind is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /**
21 * BaseRelationship specifies a basic relationship type.
22 *
23 * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
24 * @version 1.0
25 */
26 public class BaseRelationship implements Relationship, PartialMatch {
27
28 private WorldEntity left;
29 private WorldEntity right;
30
31 /**
32 * Creates a new relationship object.
33 *
34 * @param newLeft
35 * left argument world entity
36 * @param newRight
37 * right argument world entity
38 */
39 public BaseRelationship(final WorldEntity newLeft,
40 final WorldEntity newRight) {
41 this.left = newLeft;
42 this.right = newRight;
43 }
44
45 /**
46 * Creates a new relationship object where the left argument is assigned at
47 * a later stage.
48 *
49 * @param newRight
50 * right argument world entity
51 */
52 public BaseRelationship(final WorldEntity newRight) {
53 this(null, newRight);
54 }
55
56 /**
57 * Set the Left value.
58 *
59 * @param newLeft
60 * the new Left value
61 */
62 public void setLeft(final WorldEntity newLeft) {
63 this.left = newLeft;
64 }
65
66 /**
67 * Gets the left entity.
68 *
69 * @return
70 * the left entity
71 */
72 public WorldEntity getLeft() {
73 return left;
74 }
75
76 /**
77 * Set the Right value.
78 *
79 * @param newRight
80 * the new Right value
81 */
82 public void setRight(final WorldEntity newRight) {
83 this.right = newRight;
84 }
85
86 /**
87 * Gets the right entity.
88 *
89 * @return
90 * the right entity
91 */
92 public WorldEntity getRight() {
93 return right;
94 }
95
96 /**
97 * Returns whether this relationship supports transitivity.
98 *
99 * @return
100 * true if relationship type is transitive, otherwise false
101 */
102 public boolean isTransitive() {
103 return true;
104 }
105
106 /**
107 * Compares this relationship to another. A relationship is equal if it is
108 * of exactly the same class and left and right arguments are equal to each
109 * other.
110 *
111 * @param object
112 * the reference object with which to compare
113 * @return
114 * true if this object is the same as the object argument, false
115 * otherwise
116 */
117 public boolean equals(final Object object) {
118 if (!this.getClass().isInstance(object)) {
119 return false;
120 }
121
122 if (!this.getClass().equals(object.getClass())) {
123 return false;
124 }
125
126 WorldEntity objectLeft = ((BaseRelationship) object).getLeft();
127 WorldEntity objectRight = ((BaseRelationship) object).getRight();
128
129 boolean leftEqual = false;
130
131 if (objectLeft == null) {
132 leftEqual = (this.getLeft() == null);
133 } else {
134 leftEqual = objectLeft.equals(this.getLeft());
135 }
136
137 boolean rightEqual = false;
138
139 if (objectRight == null) {
140 rightEqual = (this.getRight() == null);
141 } else {
142 rightEqual = objectRight.equals(this.getRight());
143 }
144
145 return (leftEqual && rightEqual);
146 }
147
148 /**
149 * Returns a hash code value for this relationship. The method must be
150 * defined, because this class overrides <code>equals(Object)</code>.
151 * <p>
152 *
153 *
154 * @return
155 * a hash code value for this object
156 */
157 public int hashCode() {
158 String string = this.toString();
159
160 return string.hashCode();
161 }
162
163 /**
164 * Match the given relationship.
165 *
166 * @param relation
167 * the relationship template to match
168 * @return
169 * true if this relationship matches the given one
170 */
171 public boolean matches(final Relationship relation) {
172 if (relation == null) {
173 return false;
174 }
175
176 if (relation instanceof BaseRelationship) {
177 boolean match = true;
178
179 if (relation.getLeft() != null) {
180 match = match && relation.getLeft().equals(this.getLeft());
181 }
182
183 if (relation.getRight() != null) {
184 match = match && relation.getRight().equals(this.getRight());
185 }
186
187 return match;
188 }
189 //else if (relation instanceof PartialMatch) {
190 // return ((PartialMatch) relation).matches(this);
191 //}
192
193 return relation.equals(this);
194 }
195
196 /**
197 * Returns a new relationship object with the given entity assigned as the
198 * right argument.
199 *
200 * @param entity
201 * the world entity
202 * @return
203 * the relationship object
204 */
205 public static BaseRelationship right(final WorldEntity entity) {
206 return new BaseRelationship(entity);
207 }
208
209 /**
210 * Returns a new relationship object with the given entity assigned as the
211 * left argument.
212 *
213 * @param entity
214 * the world entity
215 * @return
216 * the relationship object
217 */
218 public static BaseRelationship left(final WorldEntity entity) {
219 return new BaseRelationship(entity, null);
220 }
221
222 }