8727eadddc8416d9d5f23e6068e5ca4e33475a73
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / value / EncapsulatedValueCodec.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Throwables;
14 import java.lang.invoke.MethodHandle;
15 import java.lang.invoke.MethodHandles;
16 import java.lang.invoke.MethodHandles.Lookup;
17 import java.lang.invoke.MethodType;
18 import java.lang.reflect.Method;
19 import java.util.concurrent.Callable;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.ValueTypeCodec.SchemaUnawareCodec;
21
22 /**
23  * Derived YANG types are just immutable value holders for simple value
24  * types, which are same as in NormalizedNode model.
25  */
26 @Beta
27 final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
28
29     private static final Lookup LOOKUP = MethodHandles.publicLookup();
30     private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class);
31     private final MethodHandle constructor;
32     private final MethodHandle getter;
33     private final Class<?> valueType;
34
35     private EncapsulatedValueCodec(final Class<?> typeClz, final MethodHandle constructor, final MethodHandle getter,
36             final Class<?> valueType) {
37
38         super(typeClz);
39         this.constructor = Preconditions.checkNotNull(constructor);
40         this.getter = Preconditions.checkNotNull(getter);
41         this.valueType = Preconditions.checkNotNull(valueType);
42     }
43
44     static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz) {
45         return () -> {
46             final Method m = typeClz.getMethod("getValue");
47             final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
48             final Class<?> valueType = m.getReturnType();
49
50             final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
51                     MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
52             return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
53         };
54     }
55
56     /**
57      * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
58      *
59      * @param value Value to be checked
60      * @return True if the value can be encapsulated
61      */
62     boolean canAcceptObject(final Object value) {
63         return valueType.isInstance(value);
64     }
65
66     @Override
67     public Object deserialize(Object input) {
68         try {
69             return constructor.invokeExact(input);
70         } catch (Throwable e) {
71             throw Throwables.propagate(e);
72         }
73     }
74
75     @Override
76     public Object serialize(Object input) {
77         try {
78             return getter.invokeExact(input);
79         } catch (Throwable e) {
80             throw Throwables.propagate(e);
81         }
82     }
83 }