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 android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.Set;
30 
31 /**
32  * Subscription state of Vehicle Map Service layers.
33  *
34  * The subscription state is used by publishers to determine which layers to publish data for, as
35  * any data published to a layer without subscribers will be dropped by the Vehicle Map Service.
36  *
37  * Sequence numbers are used to indicate the succession of subscription states, and increase
38  * monotonically with each change in subscriptions. They must be used by clients to ignore states
39  * that are received out-of-order.
40  *
41  * @hide
42  */
43 @SystemApi
44 public final class VmsSubscriptionState implements Parcelable {
45     private final int mSequenceNumber;
46     private final Set<VmsLayer> mLayers;
47     private final Set<VmsAssociatedLayer> mSubscribedLayersFromPublishers;
48 
49     /**
50      * Constructs a summary of the state of the current subscriptions for publishers to consume
51      * and adjust which layers that the are publishing.
52      */
VmsSubscriptionState(int sequenceNumber, @NonNull Set<VmsLayer> subscribedLayers, @NonNull Set<VmsAssociatedLayer> layersFromPublishers)53     public VmsSubscriptionState(int sequenceNumber,
54             @NonNull Set<VmsLayer> subscribedLayers,
55             @NonNull Set<VmsAssociatedLayer> layersFromPublishers) {
56         mSequenceNumber = sequenceNumber;
57         mLayers = Collections.unmodifiableSet(subscribedLayers);
58         mSubscribedLayersFromPublishers = Collections.unmodifiableSet(layersFromPublishers);
59     }
60 
61     /**
62      * @return sequence number of the subscription state
63      */
getSequenceNumber()64     public int getSequenceNumber() {
65         return mSequenceNumber;
66     }
67 
68     /**
69      * @return set of layers with subscriptions to all publishers
70      */
71     @NonNull
getLayers()72     public Set<VmsLayer> getLayers() {
73         return mLayers;
74     }
75 
76     /**
77      * @return set of layers with subscriptions to a subset of publishers
78      */
79     @NonNull
getAssociatedLayers()80     public Set<VmsAssociatedLayer> getAssociatedLayers() {
81         return mSubscribedLayersFromPublishers;
82     }
83 
84     @Override
toString()85     public String toString() {
86         StringBuilder sb = new StringBuilder();
87         sb.append("sequence number=").append(mSequenceNumber);
88         sb.append("; layers={");
89         for (VmsLayer layer : mLayers) {
90             sb.append(layer).append(",");
91         }
92         sb.append("}");
93         sb.append("; associatedLayers={");
94         for (VmsAssociatedLayer layer : mSubscribedLayersFromPublishers) {
95             sb.append(layer).append(",");
96         }
97         sb.append("}");
98         return sb.toString();
99     }
100 
101     public static final Parcelable.Creator<VmsSubscriptionState> CREATOR = new
102             Parcelable.Creator<VmsSubscriptionState>() {
103                 public VmsSubscriptionState createFromParcel(Parcel in) {
104                     return new VmsSubscriptionState(in);
105                 }
106 
107                 public VmsSubscriptionState[] newArray(int size) {
108                     return new VmsSubscriptionState[size];
109                 }
110             };
111 
112     @Override
writeToParcel(Parcel out, int flags)113     public void writeToParcel(Parcel out, int flags) {
114         out.writeInt(mSequenceNumber);
115         out.writeParcelableList(new ArrayList(mLayers), flags);
116         out.writeParcelableList(new ArrayList(mSubscribedLayersFromPublishers), flags);
117     }
118 
119     @Override
equals(Object o)120     public boolean equals(Object o) {
121         if (!(o instanceof VmsSubscriptionState)) {
122             return false;
123         }
124         VmsSubscriptionState p = (VmsSubscriptionState) o;
125         return Objects.equals(p.mSequenceNumber, mSequenceNumber) &&
126                 p.mLayers.equals(mLayers) &&
127                 p.mSubscribedLayersFromPublishers.equals(mSubscribedLayersFromPublishers);
128     }
129 
130     @Override
hashCode()131     public int hashCode() {
132         return Objects.hash(mSequenceNumber, mLayers, mSubscribedLayersFromPublishers);
133     }
134 
135     @Override
describeContents()136     public int describeContents() {
137         return 0;
138     }
139 
VmsSubscriptionState(Parcel in)140     private VmsSubscriptionState(Parcel in) {
141         mSequenceNumber = in.readInt();
142 
143         List<VmsLayer> layers = new ArrayList<>();
144         in.readParcelableList(layers, VmsLayer.class.getClassLoader());
145         mLayers = Collections.unmodifiableSet(new HashSet(layers));
146 
147         List<VmsAssociatedLayer> associatedLayers = new ArrayList<>();
148         in.readParcelableList(associatedLayers, VmsAssociatedLayer.class.getClassLoader());
149         mSubscribedLayersFromPublishers =
150                 Collections.unmodifiableSet(new HashSet(associatedLayers));
151     }
152 }