97ee81ff51fef8df9d943d165494e06f04518ab7
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Throwables;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.MethodHandles.Lookup;
16 import java.lang.invoke.MethodType;
17 import java.lang.reflect.Method;
18 import java.util.concurrent.Callable;
19 import org.opendaylight.mdsal.binding.dom.codec.impl.ValueTypeCodec.SchemaUnawareCodec;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
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 = requireNonNull(constructor);
38         this.getter = requireNonNull(getter);
39         this.valueType = requireNonNull(valueType);
40     }
41
42     static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, final TypeDefinition<?> typeDef) {
43         return () -> {
44             final Method m = typeClz.getMethod(BindingMapping.SCALAR_TYPE_OBJECT_GET_VALUE_NAME);
45             final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
46             final Class<?> valueType = m.getReturnType();
47             final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
48                 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
49             return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
50         };
51     }
52
53     /**
54      * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
55      *
56      * @param value Value to be checked
57      * @return True if the value can be encapsulated
58      */
59     boolean canAcceptObject(final Object value) {
60         return valueType.isInstance(value);
61     }
62
63     @Override
64     @SuppressWarnings("checkstyle:illegalCatch")
65     public Object deserialize(final Object input) {
66         try {
67             return constructor.invokeExact(input);
68         } catch (Throwable e) {
69             Throwables.throwIfUnchecked(e);
70             throw new IllegalStateException(e);
71         }
72     }
73
74     @Override
75     @SuppressWarnings("checkstyle:illegalCatch")
76     public Object serialize(final Object input) {
77         try {
78             return getter.invokeExact(input);
79         } catch (Throwable e) {
80             Throwables.throwIfUnchecked(e);
81             throw new IllegalStateException(e);
82         }
83     }
84 }