52dd88e33d31d0f9b77c5ae5956847827509a6a9
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / yangtools / binding / data / 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.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.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, final Codec<Object, Object> codec) {
30         this.bindingType = Preconditions.checkNotNull(valueType);
31         this.codec = Preconditions.checkNotNull(codec);
32
33         try {
34             this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
35         } catch (IllegalAccessException e) {
36             throw new IllegalStateException("Failed to access method " + getter, e);
37         }
38
39         try {
40             this.unionCtor = MethodHandles.publicLookup().findConstructor(unionType,
41                 MethodType.methodType(void.class, valueType)).asType(OBJECT_TYPE);
42         } catch (IllegalAccessException | NoSuchMethodException e) {
43             throw new IllegalStateException(String.format("Failed to access constructor for %s in type %s", valueType,
44                     unionType), e);
45         }
46     }
47
48     Object serialize(final Object input) {
49         final Object baValue = getValueFrom(input);
50         return baValue == null ? null : codec.serialize(baValue);
51     }
52
53     Object deserializeUnion(final Object input) {
54         final Object value;
55
56         try {
57             value = codec.deserialize(input);
58         } catch (Exception e) {
59             LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
60             return null;
61         }
62
63         try {
64             return unionCtor.invokeExact(value);
65         } catch (ClassCastException e) {
66             // This case can happen. e.g. NOOP_CODEC
67             LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
68             return null;
69         } catch (Throwable e) {
70             throw new IllegalArgumentException("Failed to construct union for value " + value, e);
71         }
72     }
73
74     Object getValueFrom(final Object input) {
75         try {
76             return getter.invokeExact(input);
77         } catch (Throwable e) {
78             throw Throwables.propagate(e);
79         }
80     }
81
82     @Override
83     public int hashCode() {
84         return bindingType.hashCode();
85     }
86
87     @Override
88     public boolean equals(final Object obj) {
89         if (this == obj) {
90             return true;
91         }
92         if (!(obj instanceof UnionValueOptionContext)) {
93             return false;
94         }
95
96         final UnionValueOptionContext other = (UnionValueOptionContext) obj;
97         return bindingType.equals(other.bindingType);
98     }
99 }