blob: 08e72ba200d53e0cb035d487931cc2c8fb526f2e [file] [log] [blame]
Antonio Cortes2febe9f2017-03-24 09:42:17 -07001/*
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
17package android.car.vms;
18
Mark Tabry16318e22019-03-22 10:04:32 -070019import android.annotation.NonNull;
20import android.annotation.SystemApi;
Antonio Cortes2febe9f2017-03-24 09:42:17 -070021import android.os.Parcel;
22import android.os.Parcelable;
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070023
Antonio Cortes2febe9f2017-03-24 09:42:17 -070024import java.util.ArrayList;
25import java.util.Collections;
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070026import java.util.HashSet;
Antonio Cortes2febe9f2017-03-24 09:42:17 -070027import java.util.List;
Asaf Rosenfeld9e1c3152018-02-02 08:47:10 -080028import java.util.Objects;
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070029import java.util.Set;
30
Antonio Cortes2febe9f2017-03-24 09:42:17 -070031/**
Mark Tabry16318e22019-03-22 10:04:32 -070032 * 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.
Antonio Cortes2febe9f2017-03-24 09:42:17 -070040 *
41 * @hide
42 */
Mark Tabry16318e22019-03-22 10:04:32 -070043@SystemApi
Antonio Cortes2febe9f2017-03-24 09:42:17 -070044public final class VmsSubscriptionState implements Parcelable {
45 private final int mSequenceNumber;
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070046 private final Set<VmsLayer> mLayers;
47 private final Set<VmsAssociatedLayer> mSubscribedLayersFromPublishers;
Antonio Cortes2febe9f2017-03-24 09:42:17 -070048
49 /**
Mark Tabry16318e22019-03-22 10:04:32 -070050 * Constructs a summary of the state of the current subscriptions for publishers to consume
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070051 * and adjust which layers that the are publishing.
Antonio Cortes2febe9f2017-03-24 09:42:17 -070052 */
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070053 public VmsSubscriptionState(int sequenceNumber,
Mark Tabry16318e22019-03-22 10:04:32 -070054 @NonNull Set<VmsLayer> subscribedLayers,
55 @NonNull Set<VmsAssociatedLayer> layersFromPublishers) {
Antonio Cortes2febe9f2017-03-24 09:42:17 -070056 mSequenceNumber = sequenceNumber;
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070057 mLayers = Collections.unmodifiableSet(subscribedLayers);
58 mSubscribedLayersFromPublishers = Collections.unmodifiableSet(layersFromPublishers);
Antonio Cortes2febe9f2017-03-24 09:42:17 -070059 }
60
Asaf Rosenfeld2e45d812017-08-31 23:51:12 -070061 /**
Mark Tabry16318e22019-03-22 10:04:32 -070062 * @return sequence number of the subscription state
Asaf Rosenfeld2e45d812017-08-31 23:51:12 -070063 */
Antonio Cortes2febe9f2017-03-24 09:42:17 -070064 public int getSequenceNumber() {
65 return mSequenceNumber;
66 }
67
Mark Tabry16318e22019-03-22 10:04:32 -070068 /**
69 * @return set of layers with subscriptions to all publishers
70 */
71 @NonNull
Asaf Rosenfeld2e45d812017-08-31 23:51:12 -070072 public Set<VmsLayer> getLayers() {
Antonio Cortes2febe9f2017-03-24 09:42:17 -070073 return mLayers;
74 }
75
Mark Tabry16318e22019-03-22 10:04:32 -070076 /**
77 * @return set of layers with subscriptions to a subset of publishers
78 */
79 @NonNull
Asaf Rosenfeld2e45d812017-08-31 23:51:12 -070080 public Set<VmsAssociatedLayer> getAssociatedLayers() {
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070081 return mSubscribedLayersFromPublishers;
82 }
83
Antonio Cortes2febe9f2017-03-24 09:42:17 -070084 @Override
85 public String toString() {
86 StringBuilder sb = new StringBuilder();
87 sb.append("sequence number=").append(mSequenceNumber);
88 sb.append("; layers={");
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -070089 for (VmsLayer layer : mLayers) {
90 sb.append(layer).append(",");
91 }
92 sb.append("}");
93 sb.append("; associatedLayers={");
94 for (VmsAssociatedLayer layer : mSubscribedLayersFromPublishers) {
Antonio Cortes2febe9f2017-03-24 09:42:17 -070095 sb.append(layer).append(",");
96 }
97 sb.append("}");
98 return sb.toString();
99 }
100
101 public static final Parcelable.Creator<VmsSubscriptionState> CREATOR = new
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -0700102 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 };
Antonio Cortes2febe9f2017-03-24 09:42:17 -0700111
112 @Override
113 public void writeToParcel(Parcel out, int flags) {
114 out.writeInt(mSequenceNumber);
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -0700115 out.writeParcelableList(new ArrayList(mLayers), flags);
116 out.writeParcelableList(new ArrayList(mSubscribedLayersFromPublishers), flags);
Antonio Cortes2febe9f2017-03-24 09:42:17 -0700117 }
118
119 @Override
Asaf Rosenfeld9e1c3152018-02-02 08:47:10 -0800120 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
131 public int hashCode() {
132 return Objects.hash(mSequenceNumber, mLayers, mSubscribedLayersFromPublishers);
133 }
134
135 @Override
Antonio Cortes2febe9f2017-03-24 09:42:17 -0700136 public int describeContents() {
137 return 0;
138 }
139
140 private VmsSubscriptionState(Parcel in) {
141 mSequenceNumber = in.readInt();
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -0700142
Antonio Cortes2febe9f2017-03-24 09:42:17 -0700143 List<VmsLayer> layers = new ArrayList<>();
144 in.readParcelableList(layers, VmsLayer.class.getClassLoader());
Asaf Rosenfeldf9be20b2017-06-30 18:20:08 -0700145 mLayers = Collections.unmodifiableSet(new HashSet(layers));
146
147 List<VmsAssociatedLayer> associatedLayers = new ArrayList<>();
148 in.readParcelableList(associatedLayers, VmsAssociatedLayer.class.getClassLoader());
Asaf Rosenfeld9e1c3152018-02-02 08:47:10 -0800149 mSubscribedLayersFromPublishers =
150 Collections.unmodifiableSet(new HashSet(associatedLayers));
Antonio Cortes2febe9f2017-03-24 09:42:17 -0700151 }
152}