3de5085c1d7c9f8decb1b407e91453781f6957a0
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / yangtools / binding / data / 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.yangtools.binding.data.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.yangtools.binding.data.codec.impl.ValueTypeCodec.SchemaUnawareCodec;
19
20 /**
21  *
22  * Derived YANG types are just immutable value holders for simple value
23  * types, which are same as in NormalizedNode model.
24  *
25  */
26 final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
27     private static final Lookup LOOKUP = MethodHandles.publicLookup();
28     private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class);
29     private final MethodHandle constructor;
30     private final MethodHandle getter;
31
32     private EncapsulatedValueCodec(final Class<?> typeClz, final MethodHandle constructor, final MethodHandle getter) {
33         super(typeClz);
34         this.constructor = Preconditions.checkNotNull(constructor);
35         this.getter = Preconditions.checkNotNull(getter);
36     }
37
38     static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz) {
39         return new Callable<EncapsulatedValueCodec>() {
40             @Override
41             public EncapsulatedValueCodec call() throws Exception {
42                 final Method m = typeClz.getMethod("getValue");
43                 final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
44                 final MethodHandle constructor = LOOKUP.findConstructor(typeClz, MethodType.methodType(void.class, m.getReturnType())).asType(OBJ_METHOD);
45                 return new EncapsulatedValueCodec(typeClz, constructor, getter);
46             }
47         };
48     }
49
50     @Override
51     public Object deserialize(final Object input) {
52         try {
53             return constructor.invokeExact(input);
54         } catch (Throwable e) {
55             throw Throwables.propagate(e);
56         }
57     }
58
59     @Override
60     public Object serialize(final Object input) {
61         try {
62             return getter.invokeExact(input);
63         } catch (Throwable e) {
64             throw Throwables.propagate(e);
65         }
66     }
67 }