Fix checkstyle in mdsal-binding-dom-codec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / UnionValueOptionContext.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 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.MethodType;
15 import java.lang.reflect.Method;
16 import org.opendaylight.yangtools.concepts.Codec;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 final class UnionValueOptionContext {
21     private static final MethodType OBJECT_TYPE = MethodType.methodType(Object.class, Object.class);
22     private static final Logger LOG = LoggerFactory.getLogger(UnionValueOptionContext.class);
23
24     private final Class<?> bindingType;
25     private final Codec<Object,Object> codec;
26     private final MethodHandle getter;
27     private final MethodHandle unionCtor;
28
29     UnionValueOptionContext(final Class<?> unionType, final Class<?> valueType, final Method getter,
30             final Codec<Object, Object> codec) {
31         this.bindingType = Preconditions.checkNotNull(valueType);
32         this.codec = Preconditions.checkNotNull(codec);
33
34         try {
35             this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
36         } catch (IllegalAccessException e) {
37             throw new IllegalStateException("Failed to access method " + getter, e);
38         }
39
40         try {
41             this.unionCtor = MethodHandles.publicLookup().findConstructor(unionType,
42                 MethodType.methodType(void.class, valueType)).asType(OBJECT_TYPE);
43         } catch (IllegalAccessException | NoSuchMethodException e) {
44             throw new IllegalStateException(String.format("Failed to access constructor for %s in type %s", valueType,
45                     unionType), e);
46         }
47     }
48
49     Object serialize(final Object input) {
50         final Object baValue = getValueFrom(input);
51         return baValue == null ? null : codec.serialize(baValue);
52     }
53
54     @SuppressWarnings("checkstyle:illegalCatch")
55     Object deserializeUnion(final Object input) {
56         // Side-step potential exceptions by checking the type if it is available
57         if (codec instanceof EncapsulatedValueCodec && !((EncapsulatedValueCodec) codec).canAcceptObject(input)) {
58             return null;
59         }
60
61         final Object value;
62         try {
63             value = codec.deserialize(input);
64         } catch (Exception e) {
65             LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
66             return null;
67         }
68
69         try {
70             return unionCtor.invokeExact(value);
71         } catch (ClassCastException e) {
72             // This case can happen. e.g. NOOP_CODEC
73             LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
74             return null;
75         } catch (Throwable e) {
76             throw new IllegalArgumentException("Failed to construct union for value " + value, e);
77         }
78     }
79
80     @SuppressWarnings("checkstyle:illegalCatch")
81     Object getValueFrom(final Object input) {
82         try {
83             return getter.invokeExact(input);
84         } catch (Throwable e) {
85             Throwables.throwIfUnchecked(e);
86             throw new RuntimeException(e);
87         }
88     }
89
90     @Override
91     public int hashCode() {
92         return bindingType.hashCode();
93     }
94
95     @Override
96     public boolean equals(final Object obj) {
97         if (this == obj) {
98             return true;
99         }
100         if (!(obj instanceof UnionValueOptionContext)) {
101             return false;
102         }
103
104         final UnionValueOptionContext other = (UnionValueOptionContext) obj;
105         return bindingType.equals(other.bindingType);
106     }
107 }