1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.car.vms;
18 
19 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE;
20 
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.car.builtin.os.ParcelHelper;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.util.ArraySet;
27 
28 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport;
29 import com.android.car.internal.util.AnnotationValidations;
30 
31 import java.util.Collections;
32 import java.util.Set;
33 
34 /**
35  * Availability of Vehicle Map Service layers.
36  *
37  * The layer availability is used by subscribers to determine which {@link VmsLayer}s are available
38  * for subscription and which publishers are offering to publish data for those layers. However,
39  * the Vehicle Map Service will allow subscription requests for unavailable layers.
40  *
41  * Sequence numbers are used to indicate the succession of availability states, and increase
42  * monotonically with each change in layer availability. They must be used by clients to ignore
43  * states that are received out-of-order.
44  *
45  * @hide
46  */
47 @SystemApi
48 public final class VmsAvailableLayers implements Parcelable {
49     /**
50      * Sequence number of the availability state
51      */
52     private final int mSequenceNumber;
53 
54     /**
55      * Set of layers available for subscription
56      */
57     private @NonNull Set<VmsAssociatedLayer> mAssociatedLayers;
58 
onConstructed()59     private void onConstructed() {
60         mAssociatedLayers = Collections.unmodifiableSet(mAssociatedLayers);
61     }
62 
parcelAssociatedLayers(Parcel dest)63     private void parcelAssociatedLayers(Parcel dest) {
64         ParcelHelper.writeArraySet(dest, new ArraySet<>(mAssociatedLayers));
65     }
66 
67     @SuppressWarnings("unchecked")
unparcelAssociatedLayers(Parcel in)68     private Set<VmsAssociatedLayer> unparcelAssociatedLayers(Parcel in) {
69         return (Set<VmsAssociatedLayer>) ParcelHelper.readArraySet(in,
70                 VmsAssociatedLayer.class.getClassLoader());
71     }
72 
73     /**
74      * Creates a new VmsAvailableLayers.
75      *
76      * @param associatedLayers
77      *   Set of layers available for subscription
78      * @param sequenceNumber
79      *   Sequence number of the availability state
80      * @deprecated Use {@link #VmsAvailableLayers(int, Set)} instead
81      */
82     @Deprecated
VmsAvailableLayers(@onNull Set<VmsAssociatedLayer> associatedLayers, int sequenceNumber)83     public VmsAvailableLayers(@NonNull Set<VmsAssociatedLayer> associatedLayers,
84             int sequenceNumber) {
85         this(sequenceNumber, associatedLayers);
86     }
87 
88     /**
89      * Sequence number of the availability state
90      *
91      * @deprecated Use {@link #getSequenceNumber()} instead
92      */
93     @Deprecated
getSequence()94     public int getSequence() {
95         return mSequenceNumber;
96     }
97 
98     /**
99      * Creates a new VmsAvailableLayers.
100      *
101      * @param sequenceNumber
102      *   Sequence number of the availability state
103      * @param associatedLayers
104      *   Set of layers available for subscription
105      */
VmsAvailableLayers( int sequenceNumber, @NonNull Set<VmsAssociatedLayer> associatedLayers)106     public VmsAvailableLayers(
107             int sequenceNumber,
108             @NonNull Set<VmsAssociatedLayer> associatedLayers) {
109         this.mSequenceNumber = sequenceNumber;
110         this.mAssociatedLayers = associatedLayers;
111         AnnotationValidations.validate(
112                 NonNull.class, null, mAssociatedLayers);
113 
114         onConstructed();
115     }
116 
117     /**
118      * Sequence number of the availability state
119      */
getSequenceNumber()120     public int getSequenceNumber() {
121         return mSequenceNumber;
122     }
123 
124     /**
125      * Set of layers available for subscription
126      */
getAssociatedLayers()127     public @NonNull Set<VmsAssociatedLayer> getAssociatedLayers() {
128         return mAssociatedLayers;
129     }
130 
131     @Override
toString()132     public String toString() {
133         // You can override field toString logic by defining methods like:
134         // String fieldNameToString() { ... }
135 
136         return "VmsAvailableLayers { " +
137                 "sequenceNumber = " + mSequenceNumber + ", " +
138                 "associatedLayers = " + mAssociatedLayers +
139         " }";
140     }
141 
142     @Override
equals(@ndroid.annotation.Nullable Object o)143     public boolean equals(@android.annotation.Nullable Object o) {
144         // You can override field equality logic by defining either of the methods like:
145         // boolean fieldNameEquals(VmsAvailableLayers other) { ... }
146         // boolean fieldNameEquals(FieldType otherValue) { ... }
147 
148         if (this == o) return true;
149         if (o == null || getClass() != o.getClass()) return false;
150         @SuppressWarnings("unchecked")
151         VmsAvailableLayers that = (VmsAvailableLayers) o;
152         //noinspection PointlessBooleanExpression
153         return true
154                 && mSequenceNumber == that.mSequenceNumber
155                 && java.util.Objects.equals(mAssociatedLayers, that.mAssociatedLayers);
156     }
157 
158     @Override
hashCode()159     public int hashCode() {
160         // You can override field hashCode logic by defining methods like:
161         // int fieldNameHashCode() { ... }
162 
163         int _hash = 1;
164         _hash = 31 * _hash + mSequenceNumber;
165         _hash = 31 * _hash + java.util.Objects.hashCode(mAssociatedLayers);
166         return _hash;
167     }
168 
169     @Override
writeToParcel(@onNull Parcel dest, int flags)170     public void writeToParcel(@NonNull Parcel dest, int flags) {
171         // You can override field parcelling by defining methods like:
172         // void parcelFieldName(Parcel dest, int flags) { ... }
173 
174         dest.writeInt(mSequenceNumber);
175         parcelAssociatedLayers(dest);
176     }
177 
178     @Override
179     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
describeContents()180     public int describeContents() { return 0; }
181 
182     /** @hide */
183     @SuppressWarnings({"unchecked", "RedundantCast"})
VmsAvailableLayers(@onNull Parcel in)184     /* package-private */ VmsAvailableLayers(@NonNull Parcel in) {
185         // You can override field unparcelling by defining methods like:
186         // static FieldType unparcelFieldName(Parcel in) { ... }
187 
188         int sequenceNumber = in.readInt();
189         Set<VmsAssociatedLayer> associatedLayers = unparcelAssociatedLayers(in);
190 
191         this.mSequenceNumber = sequenceNumber;
192         this.mAssociatedLayers = associatedLayers;
193         AnnotationValidations.validate(
194                 NonNull.class, null, mAssociatedLayers);
195 
196         onConstructed();
197     }
198 
199     public static final @NonNull Parcelable.Creator<VmsAvailableLayers> CREATOR
200             = new Parcelable.Creator<VmsAvailableLayers>() {
201         @Override
202         public VmsAvailableLayers[] newArray(int size) {
203             return new VmsAvailableLayers[size];
204         }
205 
206         @Override
207         public VmsAvailableLayers createFromParcel(@NonNull Parcel in) {
208             return new VmsAvailableLayers(in);
209         }
210     };
211 }
212