Cleanup RuntimeException throws
[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.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
23
24 /**
25  * Derived YANG types are just immutable value holders for simple value
26  * types, which are same as in NormalizedNode model.
27  */
28 final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
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         super(typeClz);
38         this.constructor = requireNonNull(constructor);
39         this.getter = requireNonNull(getter);
40         this.valueType = requireNonNull(valueType);
41     }
42
43     static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, final TypeDefinition<?> typeDef) {
44         return () -> {
45             final Method m;
46             if (typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition) {
47                 m = typeClz.getMethod("isValue");
48             } else {
49                 m = typeClz.getMethod("getValue");
50             }
51             final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
52             final Class<?> valueType = m.getReturnType();
53
54             final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
55                 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
56             return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
57         };
58     }
59
60     /**
61      * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
62      *
63      * @param value Value to be checked
64      * @return True if the value can be encapsulated
65      */
66     boolean canAcceptObject(final Object value) {
67         return valueType.isInstance(value);
68     }
69
70     @Override
71     @SuppressWarnings("checkstyle:illegalCatch")
72     public Object deserialize(final Object input) {
73         try {
74             return constructor.invokeExact(input);
75         } catch (Throwable e) {
76             Throwables.throwIfUnchecked(e);
77             throw new IllegalStateException(e);
78         }
79     }
80
81     @Override
82     @SuppressWarnings("checkstyle:illegalCatch")
83     public Object serialize(final Object input) {
84         try {
85             return getter.invokeExact(input);
86         } catch (Throwable e) {
87             Throwables.throwIfUnchecked(e);
88             throw new IllegalStateException(e);
89         }
90     }
91 }