Merge branch 'blueprint' from controller
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / EncapsulatedValueCodec.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodHandles.Lookup;
15 import java.lang.invoke.MethodType;
16 import java.lang.reflect.Method;
17 import java.util.concurrent.Callable;
18 import org.opendaylight.mdsal.binding.dom.codec.impl.ValueTypeCodec.SchemaUnawareCodec;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
22
23 /**
24  * Derived YANG types are just immutable value holders for simple value
25  * types, which are same as in NormalizedNode model.
26  */
27 final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
28     private static final Lookup LOOKUP = MethodHandles.publicLookup();
29     private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class);
30     private final MethodHandle constructor;
31     private final MethodHandle getter;
32     private final Class<?> valueType;
33
34     private EncapsulatedValueCodec(final Class<?> typeClz, final MethodHandle constructor, final MethodHandle getter,
35             final Class<?> valueType) {
36         super(typeClz);
37         this.constructor = Preconditions.checkNotNull(constructor);
38         this.getter = Preconditions.checkNotNull(getter);
39         this.valueType = Preconditions.checkNotNull(valueType);
40     }
41
42     static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, TypeDefinition<?> typeDef) {
43         return () -> {
44             final Method m;
45             if (typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition) {
46                 m = typeClz.getMethod("isValue");
47             } else {
48                 m = typeClz.getMethod("getValue");
49             }
50             final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
51             final Class<?> valueType = m.getReturnType();
52
53             final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
54                 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
55             return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
56         };
57     }
58
59     /**
60      * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
61      *
62      * @param value Value to be checked
63      * @return True if the value can be encapsulated
64      */
65     boolean canAcceptObject(final Object value) {
66         return valueType.isInstance(value);
67     }
68
69     @Override
70     @SuppressWarnings("checkstyle:illegalCatch")
71     public Object deserialize(final Object input) {
72         try {
73             return constructor.invokeExact(input);
74         } catch (Throwable e) {
75             Throwables.throwIfUnchecked(e);
76             throw new RuntimeException(e);
77         }
78     }
79
80     @Override
81     @SuppressWarnings("checkstyle:illegalCatch")
82     public Object serialize(final Object input) {
83         try {
84             return getter.invokeExact(input);
85         } catch (Throwable e) {
86             Throwables.throwIfUnchecked(e);
87             throw new RuntimeException(e);
88         }
89     }
90 }