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  * Availability of Vehicle Map Service layers.
33  *
34  * The layer availability is used by subscribers to determine which {@link VmsLayer}s are available
35  * for subscription and which publishers are offering to publish data for those layers. However,
36  * the Vehicle Map Service will allow subscription requests for unavailable layers.
37  *
38  * Sequence numbers are used to indicate the succession of availability states, and increase
39  * monotonically with each change in layer availability. They must be used by clients to ignore
40  * states that are received out-of-order.
41  *
42  * @hide
43  */
44 @SystemApi
45 public final class VmsAvailableLayers implements Parcelable {
46 
47     // A sequence number.
48     private final int mSeq;
49 
50     // The list of AssociatedLayers
51     private final Set<VmsAssociatedLayer> mAssociatedLayers;
52 
53     /**
54      * Constructs a new layer availability.
55      *
56      * @param associatedLayers set of layers available for subscription
57      * @param sequence         sequence number of the availability state
58      */
VmsAvailableLayers(@onNull Set<VmsAssociatedLayer> associatedLayers, int sequence)59     public VmsAvailableLayers(@NonNull Set<VmsAssociatedLayer> associatedLayers, int sequence) {
60         mSeq = sequence;
61         mAssociatedLayers = Collections.unmodifiableSet(associatedLayers);
62     }
63 
64     /**
65      * @return sequence number of the availability state
66      */
getSequence()67     public int getSequence() {
68         return mSeq;
69     }
70 
71     /**
72      * @return set of layers available for subscription
73      */
74     @NonNull
getAssociatedLayers()75     public Set<VmsAssociatedLayer> getAssociatedLayers() {
76         return mAssociatedLayers;
77     }
78 
79     @Override
toString()80     public String toString() {
81         return "VmsAvailableLayers{ seq: " +
82                 mSeq +
83                 ", AssociatedLayers: " +
84                 mAssociatedLayers +
85                 "}";
86     }
87 
88 
89     // Parcelable related methods.
90     public static final Parcelable.Creator<VmsAvailableLayers> CREATOR = new
91             Parcelable.Creator<VmsAvailableLayers>() {
92                 public VmsAvailableLayers createFromParcel(Parcel in) {
93                     return new VmsAvailableLayers(in);
94                 }
95 
96                 public VmsAvailableLayers[] newArray(int size) {
97                     return new VmsAvailableLayers[size];
98                 }
99             };
100 
101     @Override
writeToParcel(Parcel out, int flags)102     public void writeToParcel(Parcel out, int flags) {
103         out.writeInt(mSeq);
104         out.writeParcelableList(new ArrayList(mAssociatedLayers), flags);
105     }
106 
107     @Override
equals(Object o)108     public boolean equals(Object o) {
109         if (!(o instanceof VmsAvailableLayers)) {
110             return false;
111         }
112         VmsAvailableLayers p = (VmsAvailableLayers) o;
113         return Objects.equals(p.mAssociatedLayers, mAssociatedLayers) &&
114                 p.mSeq == mSeq;
115     }
116 
117     @Override
describeContents()118     public int describeContents() {
119         return 0;
120     }
121 
VmsAvailableLayers(Parcel in)122     private VmsAvailableLayers(Parcel in) {
123         mSeq = in.readInt();
124 
125         List<VmsAssociatedLayer> associatedLayers = new ArrayList<>();
126         in.readParcelableList(associatedLayers, VmsAssociatedLayer.class.getClassLoader());
127         mAssociatedLayers = Collections.unmodifiableSet(new HashSet(associatedLayers));
128     }
129 }