• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 com.android.car.hal.test;
18 
19 import android.annotation.CheckResult;
20 import android.hardware.automotive.vehicle.RawPropValues;
21 import android.hardware.automotive.vehicle.VehiclePropValue;
22 import android.os.SystemClock;
23 
24 /** A builder class for {@link VehiclePropValue} */
25 public class AidlVehiclePropValueBuilder {
26     private final VehiclePropValue mPropValue;
27 
28     /**
29      * Get a new builder based on the property ID.
30      */
newBuilder(int propId)31     public static AidlVehiclePropValueBuilder newBuilder(int propId) {
32         return new AidlVehiclePropValueBuilder(propId);
33     }
34 
35     /**
36      * Get a new builder based on the {@link VehiclePropValue}.
37      */
newBuilder(VehiclePropValue propValue)38     public static AidlVehiclePropValueBuilder newBuilder(VehiclePropValue propValue) {
39         return new AidlVehiclePropValueBuilder(propValue);
40     }
41 
AidlVehiclePropValueBuilder(int propId)42     private AidlVehiclePropValueBuilder(int propId) {
43         mPropValue = new VehiclePropValue();
44         mPropValue.value = new RawPropValues();
45         mPropValue.value.int32Values = new int[0];
46         mPropValue.value.floatValues = new float[0];
47         mPropValue.value.int64Values = new long[0];
48         mPropValue.value.byteValues = new byte[0];
49         mPropValue.value.stringValue = new String();
50         mPropValue.prop = propId;
51     }
52 
AidlVehiclePropValueBuilder(VehiclePropValue propValue)53     private AidlVehiclePropValueBuilder(VehiclePropValue propValue) {
54         mPropValue = clone(propValue);
55     }
56 
clone(VehiclePropValue propValue)57     private VehiclePropValue clone(VehiclePropValue propValue) {
58         VehiclePropValue newValue = new VehiclePropValue();
59 
60         newValue.prop = propValue.prop;
61         newValue.areaId = propValue.areaId;
62         newValue.status = propValue.status;
63         newValue.timestamp = propValue.timestamp;
64         newValue.value = new RawPropValues();
65         newValue.value.stringValue = propValue.value.stringValue;
66         if (propValue.value.int32Values != null) {
67             newValue.value.int32Values = propValue.value.int32Values.clone();
68         } else {
69             newValue.value.int32Values = new int[0];
70         }
71         if (propValue.value.floatValues != null) {
72             newValue.value.floatValues = propValue.value.floatValues.clone();
73         } else {
74             newValue.value.floatValues = new float[0];
75         }
76         if (propValue.value.int64Values != null) {
77             newValue.value.int64Values = propValue.value.int64Values.clone();
78         } else {
79             newValue.value.int64Values = new long[0];
80         }
81         if (propValue.value.byteValues != null) {
82             newValue.value.byteValues = propValue.value.byteValues.clone();
83         } else {
84             newValue.value.byteValues = new byte[0];
85         }
86 
87         return newValue;
88     }
89 
90     /**
91      * Set the area ID.
92      */
93     @CheckResult
setAreaId(int areaId)94     public AidlVehiclePropValueBuilder setAreaId(int areaId) {
95         mPropValue.areaId = areaId;
96         return this;
97     }
98 
99     /**
100      * Set the timestamp.
101      */
102     @CheckResult
setTimestamp(long timestamp)103     public AidlVehiclePropValueBuilder setTimestamp(long timestamp) {
104         mPropValue.timestamp = timestamp;
105         return this;
106     }
107 
108     /**
109      * Set the timestamp to the current time.
110      */
111     @CheckResult
setCurrentTimestamp()112     public AidlVehiclePropValueBuilder setCurrentTimestamp() {
113         mPropValue.timestamp = SystemClock.elapsedRealtimeNanos();
114         return this;
115     }
116 
117     /**
118      * Add int32 values.
119      */
120     @CheckResult
addIntValues(int... values)121     public AidlVehiclePropValueBuilder addIntValues(int... values) {
122         int oldSize = mPropValue.value.int32Values.length;
123         int newSize = oldSize + values.length;
124         int[] newValues = new int[newSize];
125         for (int i = 0; i < oldSize; i++) {
126             newValues[i] = mPropValue.value.int32Values[i];
127         }
128         for (int i = 0; i < values.length; i++) {
129             newValues[oldSize + i] = values[i];
130         }
131         mPropValue.value.int32Values = newValues;
132         return this;
133     }
134 
135     /**
136      * Add float values.
137      */
138     @CheckResult
addFloatValues(float... values)139     public AidlVehiclePropValueBuilder addFloatValues(float... values) {
140         int oldSize = mPropValue.value.floatValues.length;
141         int newSize = oldSize + values.length;
142         float[] newValues = new float[newSize];
143         for (int i = 0; i < oldSize; i++) {
144             newValues[i] = mPropValue.value.floatValues[i];
145         }
146         for (int i = 0; i < values.length; i++) {
147             newValues[oldSize + i] = values[i];
148         }
149         mPropValue.value.floatValues = newValues;
150         return this;
151     }
152 
153     /**
154      * Add byte values.
155      */
156     @CheckResult
addByteValues(byte... values)157     public AidlVehiclePropValueBuilder addByteValues(byte... values) {
158         int oldSize = mPropValue.value.byteValues.length;
159         int newSize = oldSize + values.length;
160         byte[] newValues = new byte[newSize];
161         for (int i = 0; i < oldSize; i++) {
162             newValues[i] = mPropValue.value.byteValues[i];
163         }
164         for (int i = 0; i < values.length; i++) {
165             newValues[oldSize + i] = values[i];
166         }
167         mPropValue.value.byteValues = newValues;
168         return this;
169     }
170 
171     /**
172      * Add int64 values.
173      */
174     @CheckResult
addInt64Values(long... values)175     public AidlVehiclePropValueBuilder addInt64Values(long... values) {
176         int oldSize = mPropValue.value.int64Values.length;
177         int newSize = oldSize + values.length;
178         long[] newValues = new long[newSize];
179         for (int i = 0; i < oldSize; i++) {
180             newValues[i] = mPropValue.value.int64Values[i];
181         }
182         for (int i = 0; i < values.length; i++) {
183             newValues[oldSize + i] = values[i];
184         }
185         mPropValue.value.int64Values = newValues;
186         return this;
187     }
188 
189     /**
190      * Set boolean value.
191      */
192     @CheckResult
setBooleanValue(boolean value)193     public AidlVehiclePropValueBuilder setBooleanValue(boolean value) {
194         mPropValue.value.int32Values = new int[1];
195         mPropValue.value.int32Values[0] = value ? 1 : 0;
196         return this;
197     }
198 
199     /**
200      * Set string value.
201      */
202     @CheckResult
setStringValue(String val)203     public AidlVehiclePropValueBuilder setStringValue(String val) {
204         mPropValue.value.stringValue = val;
205         return this;
206     }
207 
208     /**
209      * Build the {@link VehiclePropValue}.
210      */
build()211     public VehiclePropValue build() {
212         return clone(mPropValue);
213     }
214 }
215