summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java
blob: 17ae49778fb5afd1bbfd4be7541cb04e93fda97e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.mapbox.mapboxsdk.annotations;


import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapbox.mapboxsdk.geometry.LatLng;

import java.util.ArrayList;
import java.util.List;

/**
 * Builder for composing {@link Polygon} objects.
 * @deprecated As of 7.0.0,
 * use <a href="https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation">
 *   Mapbox Annotation Plugin</a> instead
 */
@Deprecated
public final class PolygonOptions implements Parcelable {

  public static final Parcelable.Creator<PolygonOptions> CREATOR =
    new Parcelable.Creator<PolygonOptions>() {
      public PolygonOptions createFromParcel(@NonNull Parcel in) {
        return new PolygonOptions(in);
      }

      public PolygonOptions[] newArray(int size) {
        return new PolygonOptions[size];
      }
    };

  private PolygonOptions(Parcel in) {
    polygon = new Polygon();
    List<LatLng> pointsList = new ArrayList<>();
    in.readList(pointsList, LatLng.class.getClassLoader());
    addAll(pointsList);
    List<List<LatLng>> holes = new ArrayList<>();
    in.readList(holes, LatLng.class.getClassLoader());
    addAllHoles(holes);
    alpha(in.readFloat());
    fillColor(in.readInt());
    strokeColor(in.readInt());
  }

  /**
   * Describe the kinds of special objects contained in this Parcelable's
   * marshalled representation.
   *
   * @return integer 0.
   */
  @Override
  public int describeContents() {
    return 0;
  }

  /**
   * Flatten this object in to a Parcel.
   *
   * @param out   The Parcel in which the object should be written.
   * @param flags Additional flags about how the object should be written. May be 0 or
   *              {@link #PARCELABLE_WRITE_RETURN_VALUE}.
   */
  @Override
  public void writeToParcel(Parcel out, int flags) {
    out.writeList(getPoints());
    out.writeList(getHoles());
    out.writeFloat(getAlpha());
    out.writeInt(getFillColor());
    out.writeInt(getStrokeColor());
  }

  private Polygon polygon;

  /**
   * Defines options for a polygon.
   */
  public PolygonOptions() {
    polygon = new Polygon();
  }

  /**
   * Adds a vertex to the outline of the polygon being built.
   *
   * @param point {@link LatLng} point to be added to polygon geometry.
   * @return This {@link PolygonOptions} object with the given point added to the outline.
   */
  @NonNull
  public PolygonOptions add(LatLng point) {
    polygon.addPoint(point);
    return this;
  }

  /**
   * Adds vertices to the outline of the polygon being built.
   *
   * @param points {@link LatLng} points to be added to polygon geometry.
   * @return This {@link PolygonOptions} object with the given points added to the outline.
   */
  @NonNull
  public PolygonOptions add(LatLng... points) {
    for (LatLng point : points) {
      add(point);
    }
    return this;
  }

  /**
   * Adds vertices to the outline of the polygon being built.
   *
   * @param points {@link Iterable} list made up of {@link LatLng} points defining the polygon
   *               geometry
   * @return This {@link PolygonOptions} object with the given points added to the outline.
   */
  @NonNull
  public PolygonOptions addAll(Iterable<LatLng> points) {
    for (LatLng point : points) {
      add(point);
    }
    return this;
  }

  /**
   * Adds a hole to the outline of the polygon being built.
   *
   * @param hole {@link List} list made up of {@link LatLng} points defining the hole
   * @return This {@link PolygonOptions} object with the given hole added to the outline.
   */
  @NonNull
  public PolygonOptions addHole(List<LatLng> hole) {
    polygon.addHole(hole);
    return this;
  }

  /**
   * Adds holes to the outline of the polygon being built.
   *
   * @param holes {@link List} list made up of {@link LatLng} holes to be added to polygon geometry
   * @return This {@link PolygonOptions} object with the given holes added to the outline.
   */
  @NonNull
  public PolygonOptions addHole(List<LatLng>... holes) {
    for (List<LatLng> hole : holes) {
      addHole(hole);
    }
    return this;
  }

  /**
   * Adds holes to the outline of the polygon being built.
   *
   * @param holes {@link Iterable} list made up of {@link List} list of {@link LatLng} holes defining the hole geometry
   * @return This {@link PolygonOptions} object with the given holes added to the outline.
   */
  @NonNull
  public PolygonOptions addAllHoles(Iterable<List<LatLng>> holes) {
    for (List<LatLng> hole : holes) {
      addHole(hole);
    }
    return this;
  }

  /**
   * Set the alpha value of the polyline.
   *
   * @param alpha float value between 0 (not visible) and 1.
   * @return This {@link PolygonOptions} object with the given polygon alpha value.
   */
  @NonNull
  public PolygonOptions alpha(float alpha) {
    polygon.setAlpha(alpha);
    return this;
  }

  /**
   * Gets the alpha set for this {@link PolygonOptions} object.
   *
   * @return float value between 0 and 1 defining the alpha.
   */
  public float getAlpha() {
    return polygon.getAlpha();
  }

  /**
   * Specifies the polygon's fill color, as 32-bit ARGB. The default color is black.
   *
   * @param color 32-bit ARGB color.
   * @return This {@link PolylineOptions} object with a new color set.
   */
  @NonNull
  public PolygonOptions fillColor(int color) {
    polygon.setFillColor(color);
    return this;
  }

  /**
   * Gets the fill color set for this {@link PolygonOptions} object.
   *
   * @return The fill color of the polygon in ARGB format.
   */
  public int getFillColor() {
    return polygon.getFillColor();
  }

  /**
   * Do not use this method. Used internally by the SDK.
   *
   * @return Polygon the Polygon to return
   */
  public Polygon getPolygon() {
    return polygon;
  }

  /**
   * Specifies the polygon's stroke color, as 32-bit ARGB. The default color is black.
   *
   * @param color 32-bit ARGB color.
   * @return This {@link PolygonOptions} object with a new stroke color set.
   */
  @NonNull
  public PolygonOptions strokeColor(int color) {
    polygon.setStrokeColor(color);
    return this;
  }

  /**
   * Gets the stroke color set for this {@link PolygonOptions} object.
   *
   * @return The stroke color of the polygon in ARGB format.
   */
  public int getStrokeColor() {
    return polygon.getStrokeColor();
  }

  /**
   * Gets the points set for this {@link PolygonOptions} object.
   *
   * @return The list made up of {@link LatLng} points defining the polygon.
   */
  public List<LatLng> getPoints() {
    // the getter gives us a copy, which is the safe thing to do...
    return polygon.getPoints();
  }

  /**
   * Gets the holes set for this {@link PolygonOptions} object.
   *
   * @return The list made up of {@link List} of {@link List} of {@link LatLng} points defining the holes.
   */
  public List<List<LatLng>> getHoles() {
    return polygon.getHoles();
  }


  /**
   * Compares this {@link PolygonOptions} object with another {@link PolygonOptions} and
   * determines if their color, alpha, stroke color, and vertices match.
   *
   * @param o Another {@link PolygonOptions} to compare with this object.
   * @return True if color, alpha, stroke color, vertices and holes match this {@link PolygonOptions}
   * {@link PolygonOptions} object. Else, false.
   */
  @Override
  public boolean equals(@Nullable Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    PolygonOptions polygon = (PolygonOptions) o;

    if (Float.compare(polygon.getAlpha(), getAlpha()) != 0) {
      return false;
    }
    if (getFillColor() != polygon.getFillColor()) {
      return false;
    }
    if (getStrokeColor() != polygon.getStrokeColor()) {
      return false;
    }
    if (getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null) {
      return false;
    }
    return !(getHoles() != null ? !getHoles().equals(polygon.getHoles()) : polygon.getHoles() != null);
  }

  /**
   * Gives an integer which can be used as the bucket number for storing elements of the set/map.
   * This bucket number is the address of the element inside the set/map. There's no guarantee
   * that this hash value will be consistent between different Java implementations, or even
   * between different execution runs of the same program.
   *
   * @return integer value you can use for storing element.
   */
  @Override
  public int hashCode() {
    int result = 1;
    result = 31 * result + (getAlpha() != +0.0f ? Float.floatToIntBits(getAlpha()) : 0);
    result = 31 * result + getFillColor();
    result = 31 * result + getStrokeColor();
    result = 31 * result + (getPoints() != null ? getPoints().hashCode() : 0);
    result = 31 * result + (getHoles() != null ? getHoles().hashCode() : 0);
    return result;
  }
}