2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.binding.data.codec.impl;
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;
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;
22 final class UnionTypeCodec extends ReflectionBasedCodec {
24 private final ImmutableSet<UnionValueOptionContext> typeCodecs;
25 private final Constructor<?> charConstructor;
27 private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
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);
37 static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType) {
38 return new Callable<UnionTypeCodec>() {
40 public UnionTypeCodec call() throws NoSuchMethodException, SecurityException {
41 Set<UnionValueOptionContext> values = new HashSet<>();
42 for(TypeDefinition<?> subtype : unionType.getTypes()) {
43 String methodName = "get" + BindingMapping.getClassName(subtype.getQName());
44 Method valueGetter = unionCls.getMethod(methodName);
45 Class<?> valueType = valueGetter.getReturnType();
46 Codec<Object, Object> valueCodec = UnionTypeCodec.getCodecForType(valueType, subtype);
47 values.add(new UnionValueOptionContext(valueType,valueGetter, valueCodec));
49 return new UnionTypeCodec(unionCls, values);
54 private static Codec<Object, Object> getCodecForType(final Class<?> valueType, final TypeDefinition<?> subtype) {
55 if (subtype.getBaseType() instanceof UnionTypeDefinition) {
57 return UnionTypeCodec.loader(valueType, (UnionTypeDefinition) subtype.getBaseType()).call();
58 } catch (final Exception e) {
59 throw new IllegalStateException("Could not construct Union Type Codec");
62 return ValueTypeCodec.getCodecFor(valueType, subtype);
67 public Object deserialize(final Object input) {
69 return charConstructor.newInstance((input.toString().toCharArray()));
70 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
71 throw new IllegalStateException("Could not construct instance",e);
76 public Object serialize(final Object input) {
78 for (UnionValueOptionContext valCtx : typeCodecs) {
79 Object domValue = valCtx.serialize(input);
80 if (domValue != null) {