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.Objects;
33 import java.util.Set;
34 
35 /**
36  * Subscription state of Vehicle Map Service layers.
37  *
38  * The subscription state is used by publishers to determine which layers to publish data for, as
39  * any data published to a layer without subscribers will be dropped by the Vehicle Map Service.
40  *
41  * Sequence numbers are used to indicate the succession of subscription states, and increase
42  * monotonically with each change in subscriptions. They must be used by clients to ignore states
43  * that are received out-of-order.
44  *
45  * @hide
46  */
47 @SystemApi
48 public final class VmsSubscriptionState implements Parcelable {
49     /**
50      * Sequence number of the subscription state
51      */
52     private final int mSequenceNumber;
53 
54     /**
55      * Layers with subscriptions to all publishers
56      */
57     private @NonNull Set<VmsLayer> mLayers;
58 
59     /**
60      * Layers with subscriptions to a subset of publishers
61      */
62     private @NonNull Set<VmsAssociatedLayer> mAssociatedLayers;
63 
onConstructed()64     private void onConstructed() {
65         mLayers = Collections.unmodifiableSet(mLayers);
66         mAssociatedLayers = Collections.unmodifiableSet(mAssociatedLayers);
67     }
68 
parcelLayers(Parcel dest)69     private void parcelLayers(Parcel dest) {
70         ParcelHelper.writeArraySet(dest, new ArraySet<>(mLayers));
71     }
72 
73     @SuppressWarnings("unchecked")
unparcelLayers(Parcel in)74     private Set<VmsLayer> unparcelLayers(Parcel in) {
75         return (Set<VmsLayer>) ParcelHelper.readArraySet(in, VmsLayer.class.getClassLoader());
76     }
77 
parcelAssociatedLayers(Parcel dest)78     private void parcelAssociatedLayers(Parcel dest) {
79         ParcelHelper.writeArraySet(dest, new ArraySet<>(mAssociatedLayers));
80     }
81 
82     @SuppressWarnings("unchecked")
unparcelAssociatedLayers(Parcel in)83     private Set<VmsAssociatedLayer> unparcelAssociatedLayers(Parcel in) {
84         return (Set<VmsAssociatedLayer>) ParcelHelper.readArraySet(in,
85                 VmsAssociatedLayer.class.getClassLoader());
86     }
87 
88     /**
89      * Creates a new VmsSubscriptionState.
90      *
91      * @param sequenceNumber
92      *   Sequence number of the subscription state
93      * @param layers
94      *   Layers with subscriptions to all publishers
95      * @param associatedLayers
96      *   Layers with subscriptions to a subset of publishers
97      */
VmsSubscriptionState( int sequenceNumber, @NonNull Set<VmsLayer> layers, @NonNull Set<VmsAssociatedLayer> associatedLayers)98     public VmsSubscriptionState(
99             int sequenceNumber,
100             @NonNull Set<VmsLayer> layers,
101             @NonNull Set<VmsAssociatedLayer> associatedLayers) {
102         this.mSequenceNumber = sequenceNumber;
103         this.mLayers = layers;
104         AnnotationValidations.validate(
105                 NonNull.class, null, mLayers);
106         this.mAssociatedLayers = associatedLayers;
107         AnnotationValidations.validate(
108                 NonNull.class, null, mAssociatedLayers);
109 
110         onConstructed();
111     }
112 
113     /**
114      * Sequence number of the subscription state
115      */
getSequenceNumber()116     public int getSequenceNumber() {
117         return mSequenceNumber;
118     }
119 
120     /**
121      * Layers with subscriptions to all publishers
122      */
getLayers()123     public @NonNull Set<VmsLayer> getLayers() {
124         return mLayers;
125     }
126 
127     /**
128      * Layers with subscriptions to a subset of publishers
129      */
getAssociatedLayers()130     public @NonNull Set<VmsAssociatedLayer> getAssociatedLayers() {
131         return mAssociatedLayers;
132     }
133 
134     @Override
toString()135     public String toString() {
136         // You can override field toString logic by defining methods like:
137         // String fieldNameToString() { ... }
138 
139         return "VmsSubscriptionState { " +
140                 "sequenceNumber = " + mSequenceNumber + ", " +
141                 "layers = " + mLayers + ", " +
142                 "associatedLayers = " + mAssociatedLayers +
143         " }";
144     }
145 
146     @Override
equals(@ndroid.annotation.Nullable Object o)147     public boolean equals(@android.annotation.Nullable Object o) {
148         // You can override field equality logic by defining either of the methods like:
149         // boolean fieldNameEquals(VmsSubscriptionState other) { ... }
150         // boolean fieldNameEquals(FieldType otherValue) { ... }
151 
152         if (this == o) return true;
153         if (o == null || getClass() != o.getClass()) return false;
154         @SuppressWarnings("unchecked")
155         VmsSubscriptionState that = (VmsSubscriptionState) o;
156         //noinspection PointlessBooleanExpression
157         return true
158                 && mSequenceNumber == that.mSequenceNumber
159                 && Objects.equals(mLayers, that.mLayers)
160                 && Objects.equals(mAssociatedLayers, that.mAssociatedLayers);
161     }
162 
163     @Override
hashCode()164     public int hashCode() {
165         // You can override field hashCode logic by defining methods like:
166         // int fieldNameHashCode() { ... }
167 
168         int _hash = 1;
169         _hash = 31 * _hash + mSequenceNumber;
170         _hash = 31 * _hash + Objects.hashCode(mLayers);
171         _hash = 31 * _hash + Objects.hashCode(mAssociatedLayers);
172         return _hash;
173     }
174 
175     @Override
writeToParcel(@onNull Parcel dest, int flags)176     public void writeToParcel(@NonNull Parcel dest, int flags) {
177         // You can override field parcelling by defining methods like:
178         // void parcelFieldName(Parcel dest, int flags) { ... }
179 
180         dest.writeInt(mSequenceNumber);
181         parcelLayers(dest);
182         parcelAssociatedLayers(dest);
183     }
184 
185     @Override
186     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
describeContents()187     public int describeContents() { return 0; }
188 
189     /** @hide */
190     @SuppressWarnings({"unchecked", "RedundantCast"})
VmsSubscriptionState(@onNull Parcel in)191     /* package-private */ VmsSubscriptionState(@NonNull Parcel in) {
192         // You can override field unparcelling by defining methods like:
193         // static FieldType unparcelFieldName(Parcel in) { ... }
194 
195         int sequenceNumber = in.readInt();
196         Set<VmsLayer> layers = unparcelLayers(in);
197         Set<VmsAssociatedLayer> associatedLayers = unparcelAssociatedLayers(in);
198 
199         this.mSequenceNumber = sequenceNumber;
200         this.mLayers = layers;
201         AnnotationValidations.validate(
202                 NonNull.class, null, mLayers);
203         this.mAssociatedLayers = associatedLayers;
204         AnnotationValidations.validate(
205                 NonNull.class, null, mAssociatedLayers);
206 
207         onConstructed();
208     }
209 
210     public static final @NonNull Parcelable.Creator<VmsSubscriptionState> CREATOR
211             = new Parcelable.Creator<VmsSubscriptionState>() {
212         @Override
213         public VmsSubscriptionState[] newArray(int size) {
214             return new VmsSubscriptionState[size];
215         }
216 
217         @Override
218         public VmsSubscriptionState createFromParcel(@NonNull Parcel in) {
219             return new VmsSubscriptionState(in);
220         }
221     };
222 }
223