summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java
blob: c716d10edf9c59b34cb6e3a5f91112109c43a003 (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
package com.mapbox.mapboxsdk.annotations;


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

import com.mapbox.mapboxsdk.geometry.LatLng;

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

public final class PolygonOptions implements Parcelable {

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

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

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeList(getPoints());
        out.writeFloat(getAlpha());
        out.writeInt(getFillColor());
        out.writeInt(getStrokeColor());
    }

    private Polygon polygon;

    public PolygonOptions() {
        polygon = new Polygon();
    }

    public PolygonOptions add(LatLng point) {
        polygon.addPoint(point);
        return this;
    }

    public PolygonOptions add(LatLng... points) {
        for (LatLng point : points) {
            add(point);
        }
        return this;
    }

    public PolygonOptions addAll(Iterable<LatLng> points) {
        for (LatLng point : points) {
            add(point);
        }
        return this;
    }

    public PolygonOptions alpha(float alpha) {
        polygon.setAlpha(alpha);
        return this;
    }

    public float getAlpha() {
        return polygon.getAlpha();
    }

    /**
     * Sets the color of the polygon.
     *
     * @param color - the color in ARGB format
     * @return PolygonOptions - the options object
     */
    public PolygonOptions fillColor(int color) {
        polygon.setFillColor(color);
        return this;
    }

    public int getFillColor() {
        return polygon.getFillColor();
    }

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

    /**
     * Sets the color of the stroke of the polygon.
     *
     * @param color - the color in ARGB format
     * @return PolygonOptions - the options object
     */
    public PolygonOptions strokeColor(int color) {
        polygon.setStrokeColor(color);
        return this;
    }

    public int getStrokeColor() {
        return polygon.getStrokeColor();
    }

    public List<LatLng> getPoints() {
        // the getter gives us a copy, which is the safe thing to do...
        return polygon.getPoints();
    }

    @Override
    public boolean equals(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;
        return !(getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null);
    }

    @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);
        return result;
    }
}