Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.vehiclehal; |
| 18 | |
| 19 | import android.annotation.CheckResult; |
Pavel Maltsev | cfe9310 | 2017-02-02 12:38:08 -0800 | [diff] [blame] | 20 | import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 21 | import android.os.SystemClock; |
| 22 | |
Pavel Maltsev | cfe9310 | 2017-02-02 12:38:08 -0800 | [diff] [blame] | 23 | /** A builder class for {@link android.hardware.automotive.vehicle.V2_0.VehiclePropValue} */ |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 24 | public class VehiclePropValueBuilder { |
| 25 | private final VehiclePropValue mPropValue; |
| 26 | |
| 27 | public static VehiclePropValueBuilder newBuilder(int propId) { |
| 28 | return new VehiclePropValueBuilder(propId); |
| 29 | } |
| 30 | |
| 31 | public static VehiclePropValueBuilder newBuilder(VehiclePropValue propValue) { |
| 32 | return new VehiclePropValueBuilder(propValue); |
| 33 | } |
| 34 | |
| 35 | private VehiclePropValueBuilder(int propId) { |
| 36 | mPropValue = new VehiclePropValue(); |
| 37 | mPropValue.prop = propId; |
| 38 | } |
| 39 | |
| 40 | private VehiclePropValueBuilder(VehiclePropValue propValue) { |
| 41 | mPropValue = clone(propValue); |
| 42 | } |
| 43 | |
| 44 | private VehiclePropValue clone(VehiclePropValue propValue) { |
| 45 | VehiclePropValue newValue = new VehiclePropValue(); |
| 46 | |
| 47 | newValue.prop = propValue.prop; |
| 48 | newValue.areaId = propValue.areaId; |
| 49 | newValue.timestamp = propValue.timestamp; |
| 50 | newValue.value.stringValue = propValue.value.stringValue; |
| 51 | newValue.value.int32Values.addAll(propValue.value.int32Values); |
| 52 | newValue.value.floatValues.addAll(propValue.value.floatValues); |
| 53 | newValue.value.int64Values.addAll(propValue.value.int64Values); |
| 54 | newValue.value.bytes.addAll(propValue.value.bytes); |
| 55 | |
| 56 | return newValue; |
| 57 | } |
| 58 | |
| 59 | @CheckResult |
| 60 | public VehiclePropValueBuilder setAreaId(int areaId) { |
| 61 | mPropValue.areaId = areaId; |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | @CheckResult |
| 66 | public VehiclePropValueBuilder setTimestamp(long timestamp) { |
| 67 | mPropValue.timestamp = timestamp; |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | @CheckResult |
| 72 | public VehiclePropValueBuilder setTimestamp() { |
| 73 | mPropValue.timestamp = SystemClock.elapsedRealtimeNanos(); |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | @CheckResult |
| 78 | public VehiclePropValueBuilder addIntValue(int... values) { |
| 79 | for (int val : values) { |
| 80 | mPropValue.value.int32Values.add(val); |
| 81 | } |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | @CheckResult |
| 86 | public VehiclePropValueBuilder addFloatValue(float... values) { |
| 87 | for (float val : values) { |
| 88 | mPropValue.value.floatValues.add(val); |
| 89 | } |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | @CheckResult |
| 94 | public VehiclePropValueBuilder addByteValue(byte... values) { |
| 95 | for (byte val : values) { |
| 96 | mPropValue.value.bytes.add(val); |
| 97 | } |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | @CheckResult |
| 102 | public VehiclePropValueBuilder setInt64Value(long... values) { |
| 103 | for (long val : values) { |
| 104 | mPropValue.value.int64Values.add(val); |
| 105 | } |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | @CheckResult |
| 110 | public VehiclePropValueBuilder setBooleanValue(boolean value) { |
| 111 | mPropValue.value.int32Values.clear(); |
| 112 | mPropValue.value.int32Values.add(value ? 1 : 0); |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | @CheckResult |
| 117 | public VehiclePropValueBuilder setStringValue(String val) { |
| 118 | mPropValue.value.stringValue = val; |
| 119 | return this; |
| 120 | } |
| 121 | |
| 122 | public VehiclePropValue build() { |
| 123 | return clone(mPropValue); |
| 124 | } |
| 125 | } |