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 import java.io.Serializable;
21
22 /**
23 * LayeredPosition provides an implementation of a world postion.
24 *
25 * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
26 * @version 1.0
27 */
28 public class LayeredPosition implements Place, Serializable {
29
30 private double x;
31 private double y;
32 private int layer;
33
34 private static final long serialVersionUID = -1580335735652561575L;
35
36 public static final LayeredPosition ORIGIN = new LayeredPosition(0, 0, 0);
37
38 /**
39 * LayeredPosition constructor.
40 *
41 * @param newX
42 * the X value
43 * @param newY
44 * the Y value
45 * @param newLayer
46 * the Layer value
47 */
48 public LayeredPosition(final double newX, final double newY,
49 final int newLayer) {
50 this.x = newX;
51 this.y = newY;
52 this.layer = newLayer;
53 }
54
55 /**
56 * Get the X value.
57 *
58 * @return
59 * the X value.
60 */
61 public double getX() {
62 return x;
63 }
64
65 /**
66 * Set the X value.
67 *
68 * @param newX
69 * the new X value
70 */
71 public void setX(final int newX) {
72 this.x = newX;
73 }
74
75 /**
76 * Get the Y value.
77 *
78 * @return
79 * the Y value
80 */
81 public double getY() {
82 return y;
83 }
84
85 /**
86 * Set the Y value.
87 *
88 * @param newY
89 * the new Y value
90 */
91 public void setY(final int newY) {
92 this.y = newY;
93 }
94
95 /**
96 * Get the Layer value.
97 *
98 * @return
99 * the Layer value
100 */
101 public int getLayer() {
102 return layer;
103 }
104
105 /**
106 * Set the Layer value.
107 *
108 * @param newLayer
109 * the new Layer value
110 */
111 public void setLayer(final int newLayer) {
112 this.layer = newLayer;
113 }
114
115 /**
116 * Tests whether this object represents the origin position (0,0,0).
117 *
118 * @return
119 * true if this object represents the origin, otherwise false
120 */
121 public boolean isOrigin() {
122 return equals(ORIGIN);
123 }
124
125 /**
126 * Indicates whether some other layered position is "equal to" this one. A
127 * layered position is equal if the x-axis, y-axis and layer is equal.
128 *
129 * @param object
130 * the reference object with which to compare
131 * @return
132 * true if object position equals this is, false otherwise
133 */
134 public boolean equals(final Object object) {
135 if (object == null) {
136 return false;
137 }
138
139 if (object instanceof LayeredPosition) {
140 return false;
141 }
142
143 LayeredPosition positionObject = (LayeredPosition) object;
144
145 return (
146 (this.getX() == positionObject.getX())
147 && (this.getY() == positionObject.getY())
148 && (this.getLayer() == positionObject.getLayer())
149 );
150 }
151
152 /**
153 * Returns a string representation of the object.
154 *
155 * @return
156 * a string representation of the object
157 */
158 public String toString() {
159 StringBuilder builder = new StringBuilder();
160
161 builder.append(String.valueOf(this.getX()));
162 builder.append("-");
163 builder.append(String.valueOf(this.getY()));
164 builder.append("-");
165 builder.append(String.valueOf(this.getLayer()));
166
167 return builder.toString();
168 }
169
170 /**
171 * Returns a hash code value for this layered position. It is computed by
172 * returning the hashcode of the output of <code>this.toString()</code>.
173 *
174 * @return
175 * a hash code value for this object
176 */
177 public int hashCode() {
178 String string = this.toString();
179 return string.hashCode();
180 }
181
182 }