11package com .mapbox .geojson ;
22
33import androidx .annotation .Keep ;
4+ import androidx .annotation .NonNull ;
45
56import com .google .gson .TypeAdapter ;
67import com .google .gson .stream .JsonReader ;
78import com .google .gson .stream .JsonToken ;
89import com .google .gson .stream .JsonWriter ;
9- import com .mapbox .geojson .exception .GeoJsonException ;
1010import com .mapbox .geojson .shifter .CoordinateShifterManager ;
1111import com .mapbox .geojson .utils .GeoJsonUtils ;
1212
1313import java .io .IOException ;
14- import java .util .ArrayList ;
15- import java .util .List ;
1614
1715/**
18- * Base class for converting {@code T} instance of coordinates to JSON and
19- * JSON to instance of {@code T}.
16+ * Base class for converting {@code T} instance of coordinates to JSON and
17+ * JSON to instance of {@code T}.
2018 *
2119 * @param <T> Type of coordinates
2220 * @since 4.6.0
2523abstract class BaseCoordinatesTypeAdapter <T > extends TypeAdapter <T > {
2624
2725
28- protected void writePoint (JsonWriter out , Point point ) throws IOException {
26+ protected void writePoint (JsonWriter out , Point point ) throws IOException {
2927 if (point == null ) {
3028 return ;
3129 }
32- writePointList (out , point .coordinates ());
30+ writePointList (out , point .flattenCoordinates ());
3331 }
3432
3533 protected Point readPoint (JsonReader in ) throws IOException {
36-
37- List <Double > coordinates = readPointList (in );
38- if (coordinates != null && coordinates .size () > 1 ) {
39- return new Point ("Point" ,null , coordinates );
40- }
41-
42- throw new GeoJsonException (" Point coordinates should be non-null double array" );
34+ return new Point ("Point" , null , readPointList (in ));
4335 }
4436
4537
46- protected void writePointList (JsonWriter out , List < Double > value ) throws IOException {
38+ protected void writePointList (JsonWriter out , double [] value ) throws IOException {
4739
4840 if (value == null ) {
4941 return ;
@@ -52,38 +44,52 @@ protected void writePointList(JsonWriter out, List<Double> value) throws IOExcep
5244 out .beginArray ();
5345
5446 // Unshift coordinates
55- List < Double > unshiftedCoordinates =
56- CoordinateShifterManager . getCoordinateShifter (). unshiftPoint (value );
47+ double [] unshiftedCoordinates = CoordinateShifterManager . getCoordinateShifter ()
48+ . unshiftPointArray (value );
5749
58- out .value (GeoJsonUtils .trim (unshiftedCoordinates . get ( 0 ) ));
59- out .value (GeoJsonUtils .trim (unshiftedCoordinates . get ( 1 ) ));
50+ out .value (GeoJsonUtils .trim (unshiftedCoordinates [ 0 ] ));
51+ out .value (GeoJsonUtils .trim (unshiftedCoordinates [ 1 ] ));
6052
6153 // Includes altitude
62- if (value .size () > 2 ) {
63- out .value (unshiftedCoordinates . get ( 2 ) );
54+ if (value .length > 2 ) {
55+ out .value (unshiftedCoordinates [ 2 ] );
6456 }
6557 out .endArray ();
6658 }
6759
68- protected List < Double > readPointList ( JsonReader in ) throws IOException {
69-
60+ @ NonNull
61+ protected double [] readPointList ( JsonReader in ) throws IOException {
7062 if (in .peek () == JsonToken .NULL ) {
7163 throw new NullPointerException ();
7264 }
7365
74- List <Double > coordinates = new ArrayList <Double >(3 );
66+ double lon ;
67+ double lat ;
68+ double altitude ;
7569 in .beginArray ();
76- while (in .hasNext ()) {
77- coordinates .add (in .nextDouble ());
70+ if (in .hasNext ()) {
71+ lon = in .nextDouble ();
72+ } else {
73+ throw new IndexOutOfBoundsException ("Point coordinates should contain at least two values" );
7874 }
79- in .endArray ();
80-
81- if (coordinates .size () > 2 ) {
82- return CoordinateShifterManager .getCoordinateShifter ()
83- .shiftLonLatAlt (coordinates .get (0 ), coordinates .get (1 ), coordinates .get (2 ));
75+ if (in .hasNext ()) {
76+ lat = in .nextDouble ();
77+ } else {
78+ throw new IndexOutOfBoundsException ("Point coordinates should contain at least two values" );
79+ }
80+ if (in .hasNext ()) {
81+ altitude = in .nextDouble ();
82+ // Consume any extra value but don't store it
83+ while (in .hasNext ()) {
84+ in .skipValue ();
85+ }
86+ in .endArray ();
87+ return CoordinateShifterManager .getCoordinateShifter ().shift (lon , lat , altitude );
88+ } else {
89+ in .endArray ();
90+ return CoordinateShifterManager .getCoordinateShifter ().shift (lon , lat );
8491 }
85- return CoordinateShifterManager .getCoordinateShifter ()
86- .shiftLonLat (coordinates .get (0 ), coordinates .get (1 ));
92+
8793 }
8894
8995}
0 commit comments