Skip to content

Commit a4f9c7f

Browse files
authored
Update GeoJSON APIs (Azure#19927)
API and implementation updates for GeoJSON
1 parent 200a1aa commit a4f9c7f

21 files changed

+201
-272
lines changed

sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/geojson/GeoArray.java

Lines changed: 14 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33

44
package com.azure.core.experimental.geojson;
55

6+
import com.azure.core.annotation.Immutable;
67
import com.azure.core.util.logging.ClientLogger;
78

89
import java.util.AbstractList;
910
import java.util.Collection;
1011
import java.util.Comparator;
11-
import java.util.Iterator;
1212
import java.util.List;
13-
import java.util.ListIterator;
14-
import java.util.NoSuchElementException;
1513
import java.util.Objects;
1614
import java.util.function.Predicate;
1715
import java.util.function.UnaryOperator;
@@ -21,7 +19,9 @@
2119
*
2220
* @param <T> The type of geometry coordinates.
2321
*/
24-
public class GeoArray<T> extends AbstractList<T> {
22+
@Immutable
23+
final class GeoArray<T> extends AbstractList<T> {
24+
private static final String NO_MUTATION_MESSAGE = "GeoArray cannot be mutated.";
2525
private final ClientLogger logger = new ClientLogger(GeoArray.class);
2626

2727
private final Object container;
@@ -37,8 +37,8 @@ public T get(int index) {
3737
return (T) ((List<?>) container).get(index);
3838
} else if (container instanceof GeoPointCollection) {
3939
return (T) ((GeoPointCollection) container).getPoints().get(index).getCoordinates();
40-
} else if (container instanceof GeoLineCollection) {
41-
return (T) ((GeoLineCollection) container).getLines().get(index).getCoordinates();
40+
} else if (container instanceof GeoLineStringCollection) {
41+
return (T) ((GeoLineStringCollection) container).getLines().get(index).getCoordinates();
4242
} else if (container instanceof GeoPolygon) {
4343
return (T) ((GeoPolygon) container).getRings().get(index).getCoordinates();
4444
} else if (container instanceof GeoPolygonCollection) {
@@ -54,8 +54,8 @@ public int size() {
5454
return ((List<?>) container).size();
5555
} else if (container instanceof GeoPointCollection) {
5656
return ((GeoPointCollection) container).getPoints().size();
57-
} else if (container instanceof GeoLineCollection) {
58-
return ((GeoLineCollection) container).getLines().size();
57+
} else if (container instanceof GeoLineStringCollection) {
58+
return ((GeoLineStringCollection) container).getLines().size();
5959
} else if (container instanceof GeoPolygon) {
6060
return ((GeoPolygon) container).getRings().size();
6161
} else if (container instanceof GeoPolygonCollection) {
@@ -65,88 +65,6 @@ public int size() {
6565
}
6666
}
6767

68-
@Override
69-
public Iterator<T> iterator() {
70-
return new GeoArrayIterator();
71-
}
72-
73-
@Override
74-
public ListIterator<T> listIterator(int index) {
75-
return new GeoArrayListIterator(index);
76-
}
77-
78-
/**
79-
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
80-
*
81-
* @param t The element that would be added.
82-
* @return Throws an exception.
83-
* @throws UnsupportedOperationException GeoArray doesn't support mutation.
84-
*/
85-
@Override
86-
public boolean add(T t) {
87-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
88-
}
89-
90-
/**
91-
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
92-
*
93-
* @param index The index where the element would be added.
94-
* @param element The element that would be added.
95-
* @throws UnsupportedOperationException GeoArray doesn't support mutation.
96-
*/
97-
@Override
98-
public void add(int index, T element) {
99-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
100-
}
101-
102-
/**
103-
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
104-
*
105-
* @param index The index where the element would be added.
106-
* @param element The element that would be added.
107-
* @return Throws an exception.
108-
* @throws UnsupportedOperationException GeoArray doesn't support mutation.
109-
*/
110-
@Override
111-
public T set(int index, T element) {
112-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
113-
}
114-
115-
/**
116-
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
117-
*
118-
* @param index The index where the element would be removed.
119-
* @return Throws an exception.
120-
* @throws UnsupportedOperationException GeoArray doesn't support mutation.
121-
*/
122-
@Override
123-
public T remove(int index) {
124-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
125-
}
126-
127-
/**
128-
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
129-
*
130-
* @throws UnsupportedOperationException GeoArray doesn't support mutation.
131-
*/
132-
@Override
133-
public void clear() {
134-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
135-
}
136-
137-
/**
138-
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
139-
*
140-
* @param index The index where the element would be added.
141-
* @param c The collection of elements that would be added.
142-
* @return Throws an exception.
143-
* @throws UnsupportedOperationException GeoArray doesn't support mutation.
144-
*/
145-
@Override
146-
public boolean addAll(int index, Collection<? extends T> c) {
147-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
148-
}
149-
15068
/**
15169
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
15270
*
@@ -156,19 +74,7 @@ public boolean addAll(int index, Collection<? extends T> c) {
15674
*/
15775
@Override
15876
public boolean remove(Object o) {
159-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
160-
}
161-
162-
/**
163-
* Throws {@link UnsupportedOperationException} as GeoArray doesn't support mutation.
164-
*
165-
* @param c The collection of elements that would be added.
166-
* @return Throws an exception.
167-
* @throws UnsupportedOperationException GeoArray doesn't support mutation.
168-
*/
169-
@Override
170-
public boolean addAll(Collection<? extends T> c) {
171-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
77+
throw logger.logExceptionAsError(new UnsupportedOperationException(NO_MUTATION_MESSAGE));
17278
}
17379

17480
/**
@@ -180,7 +86,7 @@ public boolean addAll(Collection<? extends T> c) {
18086
*/
18187
@Override
18288
public boolean removeAll(Collection<?> c) {
183-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
89+
throw logger.logExceptionAsError(new UnsupportedOperationException(NO_MUTATION_MESSAGE));
18490
}
18591

18692
/**
@@ -192,7 +98,7 @@ public boolean removeAll(Collection<?> c) {
19298
*/
19399
@Override
194100
public boolean retainAll(Collection<?> c) {
195-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
101+
throw logger.logExceptionAsError(new UnsupportedOperationException(NO_MUTATION_MESSAGE));
196102
}
197103

198104
/**
@@ -203,7 +109,7 @@ public boolean retainAll(Collection<?> c) {
203109
*/
204110
@Override
205111
public void replaceAll(UnaryOperator<T> operator) {
206-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
112+
throw logger.logExceptionAsError(new UnsupportedOperationException(NO_MUTATION_MESSAGE));
207113
}
208114

209115
/**
@@ -214,7 +120,7 @@ public void replaceAll(UnaryOperator<T> operator) {
214120
*/
215121
@Override
216122
public void sort(Comparator<? super T> c) {
217-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
123+
throw logger.logExceptionAsError(new UnsupportedOperationException(NO_MUTATION_MESSAGE));
218124
}
219125

220126
/**
@@ -226,7 +132,7 @@ public void sort(Comparator<? super T> c) {
226132
*/
227133
@Override
228134
public boolean removeIf(Predicate<? super T> filter) {
229-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
135+
throw logger.logExceptionAsError(new UnsupportedOperationException(NO_MUTATION_MESSAGE));
230136
}
231137

232138
/**
@@ -260,75 +166,4 @@ public boolean equals(Object o) {
260166
public int hashCode() {
261167
return Objects.hashCode(container);
262168
}
263-
264-
private class GeoArrayIterator implements Iterator<T> {
265-
transient int cursor;
266-
267-
@Override
268-
public boolean hasNext() {
269-
return cursor != size();
270-
}
271-
272-
@Override
273-
public T next() {
274-
try {
275-
int i = cursor;
276-
T value = get(i);
277-
cursor = i + 1;
278-
279-
return value;
280-
} catch (IndexOutOfBoundsException ex) {
281-
throw logger.logExceptionAsError(new NoSuchElementException());
282-
}
283-
}
284-
}
285-
286-
private final class GeoArrayListIterator extends GeoArrayIterator implements ListIterator<T> {
287-
private GeoArrayListIterator(int index) {
288-
cursor = index;
289-
}
290-
291-
@Override
292-
public boolean hasPrevious() {
293-
return cursor != 0;
294-
}
295-
296-
@Override
297-
public T previous() {
298-
try {
299-
int i = cursor - 1;
300-
T value = get(i);
301-
cursor = i;
302-
303-
return value;
304-
} catch (IndexOutOfBoundsException ex) {
305-
throw logger.logExceptionAsError(new NoSuchElementException());
306-
}
307-
}
308-
309-
@Override
310-
public int nextIndex() {
311-
return cursor;
312-
}
313-
314-
@Override
315-
public int previousIndex() {
316-
return cursor - 1;
317-
}
318-
319-
@Override
320-
public void remove() {
321-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
322-
}
323-
324-
@Override
325-
public void set(T t) {
326-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
327-
}
328-
329-
@Override
330-
public void add(T t) {
331-
throw logger.logExceptionAsError(new UnsupportedOperationException("GeoArray cannot be mutated."));
332-
}
333-
}
334169
}

sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/geojson/GeoBoundingBox.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33

44
package com.azure.core.experimental.geojson;
55

6+
import com.azure.core.annotation.Immutable;
7+
import com.azure.core.util.logging.ClientLogger;
8+
69
import java.util.Objects;
710

811
/**
912
* Represents a geometric bounding box.
1013
*/
14+
@Immutable
1115
public final class GeoBoundingBox {
16+
private final ClientLogger logger = new ClientLogger(GeoBoundingBox.class);
17+
1218
private final double west;
1319
private final double south;
1420
private final double east;
@@ -26,7 +32,7 @@ public final class GeoBoundingBox {
2632
* @param north North latitudinal boundary.
2733
*/
2834
public GeoBoundingBox(double west, double south, double east, double north) {
29-
this(west, south, east, north, null, null);
35+
this(west, south, east, north, null, null, null);
3036
}
3137

3238
/**
@@ -39,8 +45,16 @@ public GeoBoundingBox(double west, double south, double east, double north) {
3945
* @param minAltitude Minimum altitude boundary.
4046
* @param maxAltitude Maximum altitude boundary.
4147
*/
42-
public GeoBoundingBox(double west, double south, double east, double north, Double minAltitude,
43-
Double maxAltitude) {
48+
public GeoBoundingBox(double west, double south, double east, double north, double minAltitude,
49+
double maxAltitude) {
50+
this(west, south, east, north, minAltitude, maxAltitude, null);
51+
}
52+
53+
/*
54+
* This constructor allows the one above to require both min altitude and max altitude to be non-null.
55+
*/
56+
private GeoBoundingBox(double west, double south, double east, double north, Double minAltitude,
57+
Double maxAltitude, String ignored) {
4458
this.west = west;
4559
this.south = south;
4660
this.east = east;
@@ -119,11 +133,48 @@ public boolean equals(Object obj) {
119133
}
120134

121135
GeoBoundingBox other = (GeoBoundingBox) obj;
122-
return west == other.west
123-
&& south == other.south
124-
&& east == other.east
125-
&& north == other.north
136+
return Double.compare(west, other.west) == 0
137+
&& Double.compare(south, other.south) == 0
138+
&& Double.compare(east, other.east) == 0
139+
&& Double.compare(north, other.north) == 0
126140
&& Objects.equals(minAltitude, other.minAltitude)
127141
&& Objects.equals(maxAltitude, other.maxAltitude);
128142
}
143+
144+
/**
145+
* Accesses the coordinates of the {@link GeoBoundingBox} as if it were in a JSON representation.
146+
*
147+
* @param i Index to access.
148+
* @return The double value of the index.
149+
* @throws IndexOutOfBoundsException If the {@link GeoBoundingBox} doesn't have altitude coordinates and {@code i}
150+
* is greater than {@code 3} or {@link GeoBoundingBox} has altitude coordinates and {@code i} is greater than
151+
*/
152+
public double get(int i) {
153+
if (minAltitude != null && maxAltitude != null) {
154+
switch (i) {
155+
case 0: return west;
156+
case 1: return south;
157+
case 2: return minAltitude;
158+
case 3: return east;
159+
case 4: return north;
160+
case 5: return maxAltitude;
161+
default: throw logger.logExceptionAsWarning(new IndexOutOfBoundsException("Index out of range: " + i));
162+
}
163+
} else {
164+
switch (i) {
165+
case 0: return west;
166+
case 1: return south;
167+
case 2: return east;
168+
case 3: return north;
169+
default: throw logger.logExceptionAsWarning(new IndexOutOfBoundsException("Index out of range: " + i));
170+
}
171+
}
172+
}
173+
174+
@Override
175+
public String toString() {
176+
return (minAltitude != null && maxAltitude != null)
177+
? String.format("[%s, %s, %s, %s, %s, %s]", west, south, minAltitude, east, north, maxAltitude)
178+
: String.format("[%s, %s, %s, %s]", west, south, east, north);
179+
}
129180
}

0 commit comments

Comments
 (0)