Fix checkstyle in mdsal-binding2-dom-codec
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / ValueContext.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 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.MethodType;
16 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.LeafNodeCodecContext;
17 import org.opendaylight.yangtools.concepts.Codec;
18
19 /**
20  * Context for serialize/deserialize leaf.
21  */
22 @Beta
23 public final class ValueContext {
24
25     private static final MethodType OBJECT_METHOD = MethodType.methodType(Object.class, Object.class);
26     private final Codec<Object, Object> codec;
27     private final MethodHandle getter;
28     private final Class<?> identifier;
29     private final String getterName;
30
31     /**
32      * Prepare codec of leaf value and getter of binding leaf object for getting leaf.
33      *
34      * @param identifier
35      *            - binding class
36      * @param leaf
37      *            - leaf codec context
38      */
39     public ValueContext(final Class<?> identifier, final LeafNodeCodecContext<?> leaf) {
40         getterName = leaf.getGetter().getName();
41         try {
42             getter = MethodHandles.publicLookup().unreflect(identifier.getMethod(getterName)).asType(OBJECT_METHOD);
43         } catch (IllegalAccessException | NoSuchMethodException | SecurityException e) {
44             throw new IllegalStateException(
45                 String.format("Cannot find method %s in class %s", getterName, identifier), e);
46         }
47         this.identifier = identifier;
48         codec = leaf.getValueCodec();
49     }
50
51     /**
52      * Get object via invoking getter with input and serializes it by prepared codec of leaf.
53      *
54      * @param obj
55      *            - input object
56      * @return serialized invoked object
57      */
58     @SuppressWarnings("checkstyle:illegalCatch")
59     public Object getAndSerialize(final Object obj) {
60         final Object value;
61         try {
62             value = getter.invokeExact(obj);
63         } catch (final Throwable e) {
64             throw Throwables.propagate(e);
65         }
66
67         Preconditions.checkArgument(value != null,
68                 "All keys must be specified for %s. Missing key is %s. Supplied key is %s",
69                 identifier, getterName, obj);
70         return codec.serialize(value);
71     }
72
73     /**
74      * Deserialize input object by prepared codec.
75      *
76      * @param obj
77      *            - input object
78      * @return deserialized object
79      */
80     public Object deserialize(final Object obj) {
81         return codec.deserialize(obj);
82     }
83 }