BUG-869: added proper handling of nullable parameter
[yangtools.git] / code-generator / binding-data-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / UnionTypeCodec.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.collect.ImmutableSet;
11 import java.lang.reflect.Constructor;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.util.HashSet;
15 import java.util.Set;
16 import java.util.concurrent.Callable;
17 import org.opendaylight.yangtools.concepts.Codec;
18 import org.opendaylight.yangtools.yang.binding.BindingMapping;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
21
22 final class UnionTypeCodec extends ReflectionBasedCodec {
23
24     private Constructor<?> charConstructor;
25     private ImmutableSet<UnionValueOptionContext> typeCodecs;
26
27     private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
28         super(unionCls);
29         try {
30             charConstructor = unionCls.getConstructor(char[].class);
31             typeCodecs = ImmutableSet.copyOf(codecs);
32         } catch (NoSuchMethodException | SecurityException e) {
33            throw new IllegalStateException("Required constructor is not available.",e);
34         }
35     }
36
37     @SuppressWarnings("rawtypes")
38     static final Callable<UnionTypeCodec> loader(final Class<?> unionCls,final UnionTypeDefinition unionType, final Codec instanceIdentifier, final Codec identity) {
39         return new Callable<UnionTypeCodec>() {
40
41             @Override
42             public UnionTypeCodec call() throws Exception {
43                 Set<UnionValueOptionContext> values = new HashSet<>();
44                 for(TypeDefinition<?> subtype : unionType.getTypes()) {
45                     String methodName = "get" + BindingMapping.getClassName(subtype.getQName());
46                     Method valueGetter = unionCls.getMethod(methodName);
47                     Class<?> valueType = valueGetter.getReturnType();
48                     SchemaUnawareCodec valueCodec = ValueTypeCodec.getCodecFor(valueType, subtype);
49                     values.add(new UnionValueOptionContext(valueType,valueGetter, valueCodec));
50                 }
51                 return new UnionTypeCodec(unionCls, values);
52             }
53         };
54     }
55
56     @Override
57     public Object deserialize(final Object input) {
58         try {
59             return charConstructor.newInstance((input.toString().toCharArray()));
60         } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
61             throw new IllegalStateException("Could not construct instance",e);
62         }
63     }
64
65     @Override
66     public Object serialize(final Object input) {
67         if(input != null) {
68             for(UnionValueOptionContext valCtx : typeCodecs) {
69                 Object domValue = valCtx.serialize(input);
70                 if(domValue != null) {
71                     return domValue;
72                 }
73             }
74         }
75         return null;
76     }
77
78 }