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.builtin.os.ParcelHelper;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.util.ArraySet;
27 
28 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport;
29 import com.android.car.internal.util.AnnotationValidations;
30 
31 import java.util.Collections;
32 import java.util.Objects;
33 import java.util.Set;
34 
35 /**
36  * Layer dependencies for single Vehicle Map Service layer.
37  *
38  * Dependencies are treated as <b>hard</b> dependencies, meaning that an offered layer will not be
39  * reported as available until all dependent layers are also available.
40  *
41  * @hide
42  */
43 @SystemApi
44 public final class VmsLayerDependency implements Parcelable {
45     /**
46      * Layer that has dependencies
47      */
48     private final @NonNull VmsLayer mLayer;
49 
50     /**
51      * Layers that the given layer depends on
52      */
53     private @NonNull Set<VmsLayer> mDependencies;
54 
onConstructed()55     private void onConstructed() {
56         mDependencies = Collections.unmodifiableSet(mDependencies);
57     }
58 
parcelDependencies(Parcel dest)59     private void parcelDependencies(Parcel dest) {
60         ParcelHelper.writeArraySet(dest, new ArraySet<>(mDependencies));
61     }
62 
63     @SuppressWarnings("unchecked")
unparcelDependencies(Parcel in)64     private Set<VmsLayer> unparcelDependencies(Parcel in) {
65         return (Set<VmsLayer>) ParcelHelper.readArraySet(in, VmsLayer.class.getClassLoader());
66     }
67 
68     /**
69      * Creates a new VmsLayerDependency without dependencies.
70      *
71      * @param layer
72      *   Layer that has no dependencies
73      */
VmsLayerDependency(@onNull VmsLayer layer)74     public VmsLayerDependency(@NonNull VmsLayer layer) {
75         this(layer, Collections.emptySet());
76     }
77 
78     /**
79      * Creates a new VmsLayerDependency.
80      *
81      * @param layer
82      *   Layer that has dependencies
83      * @param dependencies
84      *   Layers that the given layer depends on
85      */
VmsLayerDependency( @onNull VmsLayer layer, @NonNull Set<VmsLayer> dependencies)86     public VmsLayerDependency(
87             @NonNull VmsLayer layer,
88             @NonNull Set<VmsLayer> dependencies) {
89         this.mLayer = layer;
90         AnnotationValidations.validate(
91                 NonNull.class, null, mLayer);
92         this.mDependencies = dependencies;
93         AnnotationValidations.validate(
94                 NonNull.class, null, mDependencies);
95 
96         onConstructed();
97     }
98 
99     /**
100      * Layer that has dependencies
101      */
getLayer()102     public @NonNull VmsLayer getLayer() {
103         return mLayer;
104     }
105 
106     /**
107      * Layers that the given layer depends on
108      */
getDependencies()109     public @NonNull Set<VmsLayer> getDependencies() {
110         return mDependencies;
111     }
112 
113     @Override
toString()114     public String toString() {
115         // You can override field toString logic by defining methods like:
116         // String fieldNameToString() { ... }
117 
118         return "VmsLayerDependency { " +
119                 "layer = " + mLayer + ", " +
120                 "dependencies = " + mDependencies +
121         " }";
122     }
123 
124     @Override
equals(@ndroid.annotation.Nullable Object o)125     public boolean equals(@android.annotation.Nullable Object o) {
126         // You can override field equality logic by defining either of the methods like:
127         // boolean fieldNameEquals(VmsLayerDependency other) { ... }
128         // boolean fieldNameEquals(FieldType otherValue) { ... }
129 
130         if (this == o) return true;
131         if (o == null || getClass() != o.getClass()) return false;
132         @SuppressWarnings("unchecked")
133         VmsLayerDependency that = (VmsLayerDependency) o;
134         //noinspection PointlessBooleanExpression
135         return true
136                 && Objects.equals(mLayer, that.mLayer)
137                 && Objects.equals(mDependencies, that.mDependencies);
138     }
139 
140     @Override
hashCode()141     public int hashCode() {
142         // You can override field hashCode logic by defining methods like:
143         // int fieldNameHashCode() { ... }
144 
145         int _hash = 1;
146         _hash = 31 * _hash + Objects.hashCode(mLayer);
147         _hash = 31 * _hash + Objects.hashCode(mDependencies);
148         return _hash;
149     }
150 
151     @Override
writeToParcel(@onNull Parcel dest, int flags)152     public void writeToParcel(@NonNull Parcel dest, int flags) {
153         // You can override field parcelling by defining methods like:
154         // void parcelFieldName(Parcel dest, int flags) { ... }
155 
156         dest.writeTypedObject(mLayer, flags);
157         parcelDependencies(dest);
158     }
159 
160     @Override
161     @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
describeContents()162     public int describeContents() { return 0; }
163 
164     /** @hide */
165     @SuppressWarnings({"unchecked", "RedundantCast"})
VmsLayerDependency(@onNull Parcel in)166     /* package-private */ VmsLayerDependency(@NonNull Parcel in) {
167         // You can override field unparcelling by defining methods like:
168         // static FieldType unparcelFieldName(Parcel in) { ... }
169 
170         VmsLayer layer = (VmsLayer) in.readTypedObject(VmsLayer.CREATOR);
171         Set<VmsLayer> dependencies = unparcelDependencies(in);
172 
173         this.mLayer = layer;
174         AnnotationValidations.validate(
175                 NonNull.class, null, mLayer);
176         this.mDependencies = dependencies;
177         AnnotationValidations.validate(
178                 NonNull.class, null, mDependencies);
179 
180         onConstructed();
181     }
182 
183     public static final @NonNull Parcelable.Creator<VmsLayerDependency> CREATOR
184             = new Parcelable.Creator<VmsLayerDependency>() {
185         @Override
186         public VmsLayerDependency[] newArray(int size) {
187             return new VmsLayerDependency[size];
188         }
189 
190         @Override
191         public VmsLayerDependency createFromParcel(@NonNull Parcel in) {
192             return new VmsLayerDependency(in);
193         }
194     };
195 }
196