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