Switch to Objects.requireNonNull
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / UnionValueOptionContext.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Throwables;
14 import java.lang.invoke.MethodHandle;
15 import java.lang.invoke.MethodHandles;
16 import java.lang.invoke.MethodType;
17 import java.lang.reflect.Method;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.EncapsulatedValueCodec;
19 import org.opendaylight.yangtools.concepts.Codec;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Context for serializing input value of union type and deserializing objects to binding.
25  *
26  */
27 @Beta
28 public class UnionValueOptionContext {
29
30     private static final Logger LOG = LoggerFactory.getLogger(UnionValueOptionContext.class);
31
32     private static final MethodType OBJECT_TYPE = MethodType.methodType(Object.class, Object.class);
33
34     private final Class<?> bindingType;
35     private final Codec<Object, Object> codec;
36     private final MethodHandle getter;
37     private final MethodHandle unionCtor;
38
39     /**
40      * Prepare union as binding object and codec for this object, make a direct method handle of getting union
41      * type and constructor of union type for initializing it.
42      *
43      * @param unionType
44      *            - union as binding object
45      * @param valueType
46      *            - returned type of union
47      * @param getter
48      *            - method for getting union type
49      * @param codec
50      *            - codec for serialize/deserialize type of union
51      */
52     public UnionValueOptionContext(final Class<?> unionType, final Class<?> valueType, final Method getter,
53             final Codec<Object, Object> codec) {
54         this.bindingType = requireNonNull(valueType);
55         this.codec = requireNonNull(codec);
56
57         try {
58             this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
59         } catch (final IllegalAccessException e) {
60             throw new IllegalStateException("Failed to access method " + getter, e);
61         }
62
63         try {
64             this.unionCtor = MethodHandles.publicLookup()
65                     .findConstructor(unionType, MethodType.methodType(void.class, valueType)).asType(OBJECT_TYPE);
66         } catch (IllegalAccessException | NoSuchMethodException e) {
67             throw new IllegalStateException(
68                     String.format("Failed to access constructor for %s in type %s", valueType, unionType), e);
69         }
70     }
71
72     /**
73      * Serialize input based on prepared codec.
74      *
75      * @param input
76      *            - object to serialize
77      * @return serialized objetc
78      */
79     public Object serialize(final Object input) {
80         final Object baValue = getValueFrom(input);
81         return baValue == null ? null : codec.serialize(baValue);
82     }
83
84     /**
85      * Deserialize input object via prepared codec for invoking new object of union as binding.
86      *
87      * @param input
88      *            - input object for deserializing
89      * @return deserialized union binding type object
90      */
91     @SuppressWarnings("checkstyle:illegalCatch")
92     public Object deserializeUnion(final Object input) {
93         // Side-step potential exceptions by checking the type if it is available
94         if (codec instanceof EncapsulatedValueCodec && !((EncapsulatedValueCodec) codec).canAcceptObject(input)) {
95             return null;
96         }
97
98         final Object value;
99         try {
100             value = codec.deserialize(input);
101         } catch (final Exception e) {
102             LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
103             return null;
104         }
105
106         try {
107             return unionCtor.invokeExact(value);
108         } catch (final ClassCastException e) {
109             // This case can happen. e.g. NOOP_CODEC
110             LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
111             return null;
112         } catch (final Throwable e) {
113             throw new IllegalArgumentException("Failed to construct union for value " + value, e);
114         }
115     }
116
117     @SuppressWarnings("checkstyle:illegalCatch")
118     private Object getValueFrom(final Object input) {
119         try {
120             return getter.invokeExact(input);
121         } catch (final Throwable e) {
122             throw Throwables.propagate(e);
123         }
124     }
125
126     @Override
127     public int hashCode() {
128         return bindingType.hashCode();
129     }
130
131     @Override
132     public boolean equals(final Object obj) {
133         if (this == obj) {
134             return true;
135         }
136         if (!(obj instanceof UnionValueOptionContext)) {
137             return false;
138         }
139
140         final UnionValueOptionContext other = (UnionValueOptionContext) obj;
141         return bindingType.equals(other.bindingType);
142     }
143 }