Introduce top-level pom file.
[mdsal.git] / binding / mdsal-binding-dom-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 import org.opendaylight.yangtools.yang.model.util.UnionType;
22
23 final class UnionTypeCodec extends ReflectionBasedCodec {
24
25     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
26     private final Constructor<?> charConstructor;
27
28     private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
29         super(unionCls);
30         try {
31             charConstructor = unionCls.getConstructor(char[].class);
32             typeCodecs = ImmutableSet.copyOf(codecs);
33         } catch (NoSuchMethodException | SecurityException e) {
34            throw new IllegalStateException("Required constructor is not available.",e);
35         }
36     }
37
38     static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType) {
39         return new Callable<UnionTypeCodec>() {
40             @Override
41             public UnionTypeCodec call() throws NoSuchMethodException, SecurityException {
42                 Set<UnionValueOptionContext> values = new HashSet<>();
43                 for(TypeDefinition<?> subtype : unionType.getTypes()) {
44                     String methodName = "get" + BindingMapping.getClassName(subtype.getQName());
45                     Method valueGetter = unionCls.getMethod(methodName);
46                     Class<?> valueType = valueGetter.getReturnType();
47                     Codec<Object, Object> valueCodec = UnionTypeCodec.getCodecForType(valueType, subtype);
48                     values.add(new UnionValueOptionContext(valueType,valueGetter, valueCodec));
49                 }
50                 return new UnionTypeCodec(unionCls, values);
51             }
52         };
53     }
54
55     private static Codec<Object, Object> getCodecForType(final Class<?> valueType, final TypeDefinition<?> subtype) {
56         if (subtype.getBaseType() instanceof UnionType) {
57             try {
58                 return UnionTypeCodec.loader(valueType, (UnionType) subtype.getBaseType()).call();
59             } catch (final Exception e) {
60                 throw new IllegalStateException("Could not construct Union Type Codec");
61             }
62         } else {
63             return ValueTypeCodec.getCodecFor(valueType, subtype);
64         }
65     }
66
67     @Override
68     public Object deserialize(final Object input) {
69         try {
70             return charConstructor.newInstance((input.toString().toCharArray()));
71         } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
72             throw new IllegalStateException("Could not construct instance",e);
73         }
74     }
75
76     @Override
77     public Object serialize(final Object input) {
78         if (input != null) {
79             for (UnionValueOptionContext valCtx : typeCodecs) {
80                 Object domValue = valCtx.serialize(input);
81                 if (domValue != null) {
82                     return domValue;
83                 }
84             }
85         }
86         return null;
87     }
88
89 }