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