|
21 | 21 | import java.util.Map; |
22 | 22 |
|
23 | 23 | public class GeolocationCoordinates { |
24 | | - private final double latitude; |
25 | | - private final double longitude; |
26 | | - private final double accuracy; |
27 | | - private final Double altitude; |
28 | | - private final Double altitudeAccuracy; |
29 | | - private final Double heading; |
30 | | - private final Double speed; |
31 | | - |
32 | | - // Constructor with default accuracy = 1.0 |
33 | | - public GeolocationCoordinates( |
34 | | - double latitude, |
35 | | - double longitude, |
36 | | - Double altitude, |
37 | | - Double altitudeAccuracy, |
38 | | - Double heading, |
39 | | - Double speed) { |
40 | | - this(latitude, longitude, 1.0, altitude, altitudeAccuracy, heading, speed); |
41 | | - } |
42 | | - |
43 | | - // Constructor with no altitude, altitudeAccuracy, heading, or speed |
44 | | - public GeolocationCoordinates(double latitude, double longitude, double accuracy) { |
45 | | - this(latitude, longitude, accuracy, null, null, null, null); |
46 | | - } |
47 | | - |
48 | | - // Constructor with no altitudeAccuracy, heading, or speed |
49 | | - public GeolocationCoordinates( |
50 | | - double latitude, double longitude, double accuracy, Double altitude) { |
51 | | - this(latitude, longitude, accuracy, altitude, null, null, null); |
52 | | - } |
53 | | - |
54 | | - // Constructor no heading or speed |
55 | | - public GeolocationCoordinates( |
56 | | - double latitude, |
57 | | - double longitude, |
58 | | - double accuracy, |
59 | | - Double altitude, |
60 | | - Double altitudeAccuracy) { |
61 | | - this(latitude, longitude, accuracy, altitude, altitudeAccuracy, null, null); |
62 | | - } |
63 | | - |
64 | | - // Constructor with no speed |
65 | | - public GeolocationCoordinates( |
66 | | - double latitude, |
67 | | - double longitude, |
68 | | - double accuracy, |
69 | | - Double altitude, |
70 | | - Double altitudeAccuracy, |
71 | | - Double heading) { |
72 | | - this(latitude, longitude, accuracy, altitude, altitudeAccuracy, heading, null); |
73 | | - } |
74 | | - |
75 | | - public GeolocationCoordinates( |
76 | | - double latitude, |
77 | | - double longitude, |
78 | | - double accuracy, |
79 | | - Double altitude, |
80 | | - Double altitudeAccuracy, |
81 | | - Double heading, |
82 | | - Double speed) { |
| 24 | + private final Map<String, Object> map = new HashMap<>(); |
83 | 25 |
|
| 26 | + public GeolocationCoordinates(double latitude, double longitude) { |
84 | 27 | if (latitude < -90.0 || latitude > 90.0) { |
85 | 28 | throw new IllegalArgumentException("Latitude must be between -90.0 and 90.0"); |
86 | 29 | } |
87 | 30 | if (longitude < -180.0 || longitude > 180.0) { |
88 | 31 | throw new IllegalArgumentException("Longitude must be between -180.0 and 180.0"); |
89 | 32 | } |
| 33 | + |
| 34 | + map.put("latitude", latitude); |
| 35 | + map.put("longitude", longitude); |
| 36 | + map.put("accuracy", 1.0); // Default accuracy |
| 37 | + } |
| 38 | + |
| 39 | + public GeolocationCoordinates accuracy(double accuracy) { |
90 | 40 | if (accuracy < 0.0) { |
91 | 41 | throw new IllegalArgumentException("Accuracy must be >= 0.0"); |
92 | 42 | } |
93 | | - if (altitudeAccuracy != null && altitude == null) { |
94 | | - throw new IllegalArgumentException("altitudeAccuracy cannot be set without altitude"); |
95 | | - } |
96 | | - if (altitudeAccuracy != null && altitudeAccuracy < 0.0) { |
97 | | - throw new IllegalArgumentException("Altitude accuracy must be >= 0.0"); |
| 43 | + map.put("accuracy", accuracy); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public GeolocationCoordinates altitude(Double altitude) { |
| 48 | + if (altitude != null) { |
| 49 | + map.put("altitude", altitude); |
98 | 50 | } |
99 | | - if (heading != null && (heading < 0.0 || heading >= 360.0)) { |
100 | | - throw new IllegalArgumentException("Heading must be between 0.0 and 360.0"); |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public GeolocationCoordinates altitudeAccuracy(Double altitudeAccuracy) { |
| 55 | + if (altitudeAccuracy != null) { |
| 56 | + if (!map.containsKey("altitude")) { |
| 57 | + throw new IllegalArgumentException("altitudeAccuracy cannot be set without altitude"); |
| 58 | + } |
| 59 | + if (altitudeAccuracy < 0.0) { |
| 60 | + throw new IllegalArgumentException("Altitude accuracy must be >= 0.0"); |
| 61 | + } |
| 62 | + map.put("altitudeAccuracy", altitudeAccuracy); |
101 | 63 | } |
102 | | - if (speed != null && speed < 0.0) { |
103 | | - throw new IllegalArgumentException("Speed must be >= 0.0"); |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public GeolocationCoordinates heading(Double heading) { |
| 68 | + if (heading != null) { |
| 69 | + if (heading < 0.0 || heading >= 360.0) { |
| 70 | + throw new IllegalArgumentException("Heading must be between 0.0 and 360.0"); |
| 71 | + } |
| 72 | + map.put("heading", heading); |
104 | 73 | } |
| 74 | + return this; |
| 75 | + } |
105 | 76 |
|
106 | | - this.latitude = latitude; |
107 | | - this.longitude = longitude; |
108 | | - this.accuracy = accuracy; |
109 | | - this.altitude = altitude; |
110 | | - this.altitudeAccuracy = altitudeAccuracy; |
111 | | - this.heading = heading; |
112 | | - this.speed = speed; |
| 77 | + public GeolocationCoordinates speed(Double speed) { |
| 78 | + if (speed != null) { |
| 79 | + if (speed < 0.0) { |
| 80 | + throw new IllegalArgumentException("Speed must be >= 0.0"); |
| 81 | + } |
| 82 | + map.put("speed", speed); |
| 83 | + } |
| 84 | + return this; |
113 | 85 | } |
114 | 86 |
|
115 | 87 | public double getLatitude() { |
116 | | - return latitude; |
| 88 | + return (Double) map.get("latitude"); |
117 | 89 | } |
118 | 90 |
|
119 | 91 | public double getLongitude() { |
120 | | - return longitude; |
| 92 | + return (Double) map.get("longitude"); |
121 | 93 | } |
122 | 94 |
|
123 | 95 | public double getAccuracy() { |
124 | | - return accuracy; |
| 96 | + return (Double) map.get("accuracy"); |
125 | 97 | } |
126 | 98 |
|
127 | 99 | public Double getAltitude() { |
128 | | - return altitude; |
| 100 | + return (Double) map.get("altitude"); |
129 | 101 | } |
130 | 102 |
|
131 | 103 | public Double getAltitudeAccuracy() { |
132 | | - return altitudeAccuracy; |
| 104 | + return (Double) map.get("altitudeAccuracy"); |
133 | 105 | } |
134 | 106 |
|
135 | 107 | public Double getHeading() { |
136 | | - return heading; |
| 108 | + return (Double) map.get("heading"); |
137 | 109 | } |
138 | 110 |
|
139 | 111 | public Double getSpeed() { |
140 | | - return speed; |
| 112 | + return (Double) map.get("speed"); |
141 | 113 | } |
142 | 114 |
|
143 | 115 | public Map<String, Object> toMap() { |
144 | | - Map<String, Object> map = new HashMap<>(); |
145 | | - map.put("latitude", getLatitude()); |
146 | | - map.put("longitude", getLongitude()); |
147 | | - map.put("accuracy", getAccuracy()); |
148 | | - if (getAltitude() != null) { |
149 | | - map.put("altitude", getAltitude()); |
150 | | - } |
151 | | - if (getAltitudeAccuracy() != null) { |
152 | | - map.put("altitudeAccuracy", getAltitudeAccuracy()); |
153 | | - } |
154 | | - if (getHeading() != null) { |
155 | | - map.put("heading", getHeading()); |
156 | | - } |
157 | | - if (getSpeed() != null) { |
158 | | - map.put("speed", getSpeed()); |
159 | | - } |
160 | 116 | return Map.copyOf(map); |
161 | 117 | } |
162 | 118 | } |
0 commit comments