ee161a9b537a9cb1288e540ec621da799cd10523
[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.Method;
12 import java.util.LinkedHashSet;
13 import java.util.Set;
14 import java.util.concurrent.Callable;
15 import org.opendaylight.yangtools.concepts.Codec;
16 import org.opendaylight.yangtools.yang.binding.BindingMapping;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
19
20 final class UnionTypeCodec extends ReflectionBasedCodec {
21     private final ImmutableSet<UnionValueOptionContext> typeCodecs;
22
23     private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
24         super(unionCls);
25         typeCodecs = ImmutableSet.copyOf(codecs);
26     }
27
28     static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
29                                            final BindingCodecContext bindingCodecContext) {
30         return () -> {
31             final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
32             for (TypeDefinition<?> subtype : unionType.getTypes()) {
33                 Method valueGetter = unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
34                 Class<?> valueType = valueGetter.getReturnType();
35                 Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
36                 values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
37             }
38             return new UnionTypeCodec(unionCls, values);
39         };
40     }
41
42     @Override
43     public Object deserialize(final Object input) {
44         for (UnionValueOptionContext member : typeCodecs) {
45             final Object ret = member.deserializeUnion(input);
46             if (ret != null) {
47                 return ret;
48             }
49         }
50
51         throw new IllegalArgumentException(String.format("Failed to construct instance of %s for input %s",
52             getTypeClass(), input));
53     }
54
55     @Override
56     public Object serialize(final Object input) {
57         if (input != null) {
58             for (UnionValueOptionContext valCtx : typeCodecs) {
59                 final Object domValue = valCtx.serialize(input);
60                 if (domValue != null) {
61                     return domValue;
62                 }
63             }
64         }
65         return null;
66     }
67 }