Switch to Objects.requireNonNull
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / value / BitsCodec.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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17 import java.lang.invoke.MethodHandle;
18 import java.lang.invoke.MethodHandles;
19 import java.lang.invoke.MethodType;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29 import java.util.TreeSet;
30 import java.util.concurrent.Callable;
31 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.ValueTypeCodec.SchemaUnawareCodec;
32 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
33 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer;
34 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
36
37 @Beta
38 final class BitsCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
39
40     private static final MethodType CONSTRUCTOR_INVOKE_TYPE = MethodType.methodType(Object.class, Boolean[].class);
41     // Ordered by position
42     private final Map<String, Method> getters;
43     // Ordered by lexical name
44     private final Set<String> ctorArgs;
45     private final MethodHandle ctor;
46
47     private BitsCodec(final Class<?> typeClass, final MethodHandle ctor, final Set<String> ctorArgs,
48             final Map<String, Method> getters) {
49
50         super(typeClass);
51         this.ctor = requireNonNull(ctor);
52         this.ctorArgs = ImmutableSet.copyOf(ctorArgs);
53         this.getters = ImmutableMap.copyOf(getters);
54     }
55
56     static Callable<BitsCodec> loader(final Class<?> returnType, final BitsTypeDefinition rootType) {
57         return () -> {
58             final Map<String, Method> getters = new LinkedHashMap<>();
59             final Set<String> ctorArgs = new TreeSet<>();
60
61             for (Bit bit : rootType.getBits()) {
62                 final Method valueGetter = returnType.getMethod("is" + JavaIdentifierNormalizer
63                         .normalizeSpecificIdentifier(bit.getName(), JavaIdentifier.CLASS));
64                 ctorArgs.add(bit.getName());
65                 getters.put(bit.getName(), valueGetter);
66             }
67             Constructor<?> constructor = null;
68             for (Constructor<?> cst : returnType.getConstructors()) {
69                 if (!cst.getParameterTypes()[0].equals(returnType)) {
70                     constructor = cst;
71                 }
72             }
73
74             final MethodHandle ctor = MethodHandles.publicLookup().unreflectConstructor(constructor)
75                     .asSpreader(Boolean[].class, ctorArgs.size()).asType(CONSTRUCTOR_INVOKE_TYPE);
76             return new BitsCodec(returnType, ctor, ctorArgs, getters);
77         };
78     }
79
80     @Override
81     @SuppressWarnings("checkstyle:illegalCatch")
82     public Object deserialize(Object input) {
83         Preconditions.checkArgument(input instanceof Set);
84         @SuppressWarnings("unchecked")
85         final Set<String> casted = (Set<String>) input;
86
87         /*
88          * We can do this walk based on field set sorted by name,
89          * since constructor arguments in Java Binding are sorted by name.
90          *
91          * This means we will construct correct array for construction
92          * of bits object.
93          */
94         final Boolean[] args = new Boolean[ctorArgs.size()];
95         int currentArg = 0;
96         for (String value : ctorArgs) {
97             args[currentArg++] = casted.contains(value);
98         }
99
100         try {
101             return ctor.invokeExact(args);
102         } catch (Throwable e) {
103             throw new IllegalStateException("Failed to instantiate object for " + input, e);
104         }
105     }
106
107     @Override
108     public Object serialize(Object input) {
109         final Collection<String> result = new ArrayList<>(getters.size());
110         for (Entry<String, Method> valueGet : getters.entrySet()) {
111             final Boolean value;
112             try {
113                 value = (Boolean) valueGet.getValue().invoke(input);
114             } catch (IllegalAccessException | InvocationTargetException e) {
115                 throw new IllegalArgumentException("Failed to get bit " + valueGet.getKey(), e);
116             }
117
118             if (value) {
119                 result.add(valueGet.getKey());
120             }
121         }
122         return result.size() == getters.size() ? getters.keySet() : ImmutableSet.copyOf(result);
123     }
124 }