1 /*
2  * Copyright (C) 2014 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 package android.hardware.camera2.marshal.impl;
17 
18 import static android.hardware.camera2.impl.CameraMetadataNative.*;
19 import static android.hardware.camera2.marshal.MarshalHelpers.*;
20 
21 import android.hardware.camera2.marshal.Marshaler;
22 import android.hardware.camera2.marshal.MarshalQueryable;
23 import android.hardware.camera2.utils.TypeReference;
24 
25 import java.nio.ByteBuffer;
26 
27 /**
28  * Marshal booleans: TYPE_BYTE <-> boolean/Boolean
29  */
30 public class MarshalQueryableBoolean implements MarshalQueryable<Boolean> {
31 
32     private class MarshalerBoolean extends Marshaler<Boolean> {
MarshalerBoolean(TypeReference<Boolean> typeReference, int nativeType)33         protected MarshalerBoolean(TypeReference<Boolean> typeReference, int nativeType) {
34             super(MarshalQueryableBoolean.this, typeReference, nativeType);
35         }
36 
37         @Override
marshal(Boolean value, ByteBuffer buffer)38         public void marshal(Boolean value, ByteBuffer buffer) {
39             boolean unboxValue = value;
40             buffer.put((byte)(unboxValue ? 1 : 0));
41         }
42 
43         @Override
unmarshal(ByteBuffer buffer)44         public Boolean unmarshal(ByteBuffer buffer) {
45             return buffer.get() != 0;
46         }
47 
48         @Override
getNativeSize()49         public int getNativeSize() {
50             return SIZEOF_BYTE;
51         }
52     }
53 
54     @Override
createMarshaler(TypeReference<Boolean> managedType, int nativeType)55     public Marshaler<Boolean> createMarshaler(TypeReference<Boolean> managedType,
56             int nativeType) {
57         return new MarshalerBoolean(managedType, nativeType);
58     }
59 
60     @Override
isTypeMappingSupported(TypeReference<Boolean> managedType, int nativeType)61     public boolean isTypeMappingSupported(TypeReference<Boolean> managedType, int nativeType) {
62         return (Boolean.class.equals(managedType.getType())
63                 || boolean.class.equals(managedType.getType())) && nativeType == TYPE_BYTE;
64     }
65 
66 
67 }
68