1
2
3
4 package de.powerstat.validation.values;
5
6
7 import java.util.Objects;
8
9 import de.powerstat.validation.interfaces.IValueObject;
10
11
12
13
14
15
16
17
18
19
20
21 public final class WGS84Position implements Comparable<WGS84Position>, IValueObject
22 {
23
24
25
26
27
28
29
30
31 private static final double EPSILON = 0.000001D;
32
33
34
35
36 private static final String SEPARATOR = " ";
37
38
39
40
41
42
43 private final double latitude;
44
45
46
47
48
49
50 private final double longitude;
51
52
53
54
55 private final double altitude;
56
57
58
59
60
61
62
63
64
65 private WGS84Position(final double latitude, final double longitude, final double altitude)
66 {
67 super();
68 if ((latitude < -90.0) || (latitude > 90.0))
69 {
70 throw new IndexOutOfBoundsException("Latitude out of range");
71 }
72 if ((longitude < -180.0) || (longitude > 180.0))
73 {
74 throw new IndexOutOfBoundsException("Longitude out of range");
75 }
76 this.latitude = latitude;
77 this.longitude = longitude;
78 this.altitude = altitude;
79 }
80
81
82
83
84
85
86
87
88
89
90 public static WGS84Position of(final double latitude, final double longitude, final double altitude)
91 {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 return new WGS84Position(latitude, longitude, altitude);
107 }
108
109
110
111
112
113
114
115
116 public static WGS84Position of(final String value)
117 {
118 final String[] values = value.split(SEPARATOR);
119 if (values.length != 3)
120 {
121 throw new IllegalArgumentException("value not of expected format");
122 }
123 final double latitude = Double.parseDouble(values[0]);
124 final double longitude = Double.parseDouble(values[1]);
125 final double altitude = Double.parseDouble(values[2]);
126 return of(latitude, longitude, altitude);
127 }
128
129
130
131
132
133
134
135 public double getLatitude()
136 {
137 return this.latitude;
138 }
139
140
141
142
143
144
145
146 public double getLongitude()
147 {
148 return this.longitude;
149 }
150
151
152
153
154
155
156
157 public double getAltitude()
158 {
159 return this.altitude;
160 }
161
162
163
164
165
166
167
168 @Override
169 public String stringValue()
170 {
171 return this.latitude + SEPARATOR + this.longitude + SEPARATOR + this.altitude;
172 }
173
174
175
176
177
178
179
180
181 @Override
182 public int hashCode()
183 {
184 int result = Double.hashCode(this.latitude);
185 result = (31 * result) + Double.hashCode(this.longitude);
186 return (31 * result) + Double.hashCode(this.altitude);
187 }
188
189
190
191
192
193
194
195
196
197 @Override
198 public boolean equals(final Object obj)
199 {
200 if (this == obj)
201 {
202 return true;
203 }
204 if (!(obj instanceof WGS84Position))
205 {
206 return false;
207 }
208 final WGS84Position other = (WGS84Position)obj;
209 return (Math.abs(this.latitude - other.latitude) < WGS84Position.EPSILON) && (Math.abs(this.longitude - other.longitude) < WGS84Position.EPSILON) && (Math.abs(this.altitude - other.altitude) < WGS84Position.EPSILON);
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 @Override
224 public String toString()
225 {
226 final var builder = new StringBuilder(47);
227 builder.append("WGS84Position[latitude=").append(this.latitude).append(", longitude=").append(this.longitude).append(", altitude=").append(this.altitude).append(']');
228 return builder.toString();
229 }
230
231
232
233
234
235
236
237
238
239 @Override
240 public int compareTo(final WGS84Position obj)
241 {
242 Objects.requireNonNull(obj, "obj");
243 int result = Double.compare(this.latitude, obj.latitude);
244 if (result == 0)
245 {
246 result = Double.compare(this.longitude, obj.longitude);
247 if (result == 0)
248 {
249 result = Double.compare(this.altitude, obj.altitude);
250 }
251 }
252 return result;
253 }
254
255 }