33
44package com .azure .core .experimental .geojson ;
55
6+ import com .azure .core .annotation .Immutable ;
67import com .azure .core .util .logging .ClientLogger ;
78
89import java .util .AbstractList ;
910import java .util .Collection ;
1011import java .util .Comparator ;
11- import java .util .Iterator ;
1212import java .util .List ;
13- import java .util .ListIterator ;
14- import java .util .NoSuchElementException ;
1513import java .util .Objects ;
1614import java .util .function .Predicate ;
1715import java .util .function .UnaryOperator ;
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}
0 commit comments