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
17package android.car.vms;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.HashSet;
25import java.util.List;
26import java.util.Objects;
27import java.util.Set;
28
29/**
30 * The list of layers with subscribers.
31 *
32 * @hide
33 */
34public final class VmsSubscriptionState implements Parcelable {
35    private final int mSequenceNumber;
36    private final Set<VmsLayer> mLayers;
37    private final Set<VmsAssociatedLayer> mSubscribedLayersFromPublishers;
38
39    /**
40     * Construcs a summary of the state of the current subscriptions for publishers to consume
41     * and adjust which layers that the are publishing.
42     */
43    public VmsSubscriptionState(int sequenceNumber,
44                                Set<VmsLayer> subscribedLayers,
45                                Set<VmsAssociatedLayer> layersFromPublishers) {
46        mSequenceNumber = sequenceNumber;
47        mLayers = Collections.unmodifiableSet(subscribedLayers);
48        mSubscribedLayersFromPublishers = Collections.unmodifiableSet(layersFromPublishers);
49    }
50
51    /**
52     * Returns the sequence number assigned by the VMS service. Sequence numbers are
53     * monotonically increasing and help clients ignore potential out-of-order states.
54     */
55    public int getSequenceNumber() {
56        return mSequenceNumber;
57    }
58
59    public Set<VmsLayer> getLayers() {
60        return mLayers;
61    }
62
63    public Set<VmsAssociatedLayer> getAssociatedLayers() {
64        return mSubscribedLayersFromPublishers;
65    }
66
67    @Override
68    public String toString() {
69        StringBuilder sb = new StringBuilder();
70        sb.append("sequence number=").append(mSequenceNumber);
71        sb.append("; layers={");
72        for (VmsLayer layer : mLayers) {
73            sb.append(layer).append(",");
74        }
75        sb.append("}");
76        sb.append("; associatedLayers={");
77        for (VmsAssociatedLayer layer : mSubscribedLayersFromPublishers) {
78            sb.append(layer).append(",");
79        }
80        sb.append("}");
81        return sb.toString();
82    }
83
84    public static final Parcelable.Creator<VmsSubscriptionState> CREATOR = new
85            Parcelable.Creator<VmsSubscriptionState>() {
86                public VmsSubscriptionState createFromParcel(Parcel in) {
87                    return new VmsSubscriptionState(in);
88                }
89
90                public VmsSubscriptionState[] newArray(int size) {
91                    return new VmsSubscriptionState[size];
92                }
93            };
94
95    @Override
96    public void writeToParcel(Parcel out, int flags) {
97        out.writeInt(mSequenceNumber);
98        out.writeParcelableList(new ArrayList(mLayers), flags);
99        out.writeParcelableList(new ArrayList(mSubscribedLayersFromPublishers), flags);
100    }
101
102    @Override
103    public boolean equals(Object o) {
104        if (!(o instanceof VmsSubscriptionState)) {
105            return false;
106        }
107        VmsSubscriptionState p = (VmsSubscriptionState) o;
108        return Objects.equals(p.mSequenceNumber, mSequenceNumber) &&
109                p.mLayers.equals(mLayers) &&
110                p.mSubscribedLayersFromPublishers.equals(mSubscribedLayersFromPublishers);
111    }
112
113    @Override
114    public int hashCode() {
115        return Objects.hash(mSequenceNumber, mLayers, mSubscribedLayersFromPublishers);
116    }
117
118    @Override
119    public int describeContents() {
120        return 0;
121    }
122
123    private VmsSubscriptionState(Parcel in) {
124        mSequenceNumber = in.readInt();
125
126        List<VmsLayer> layers = new ArrayList<>();
127        in.readParcelableList(layers, VmsLayer.class.getClassLoader());
128        mLayers = Collections.unmodifiableSet(new HashSet(layers));
129
130        List<VmsAssociatedLayer> associatedLayers = new ArrayList<>();
131        in.readParcelableList(associatedLayers, VmsAssociatedLayer.class.getClassLoader());
132        mSubscribedLayersFromPublishers =
133                Collections.unmodifiableSet(new HashSet(associatedLayers));
134    }
135}