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.base.Preconditions;
11 import com.google.common.collect.ImmutableSortedMap;
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.util.HashSet;
16 import java.util.Map.Entry;
18 import java.util.SortedMap;
19 import java.util.TreeMap;
20 import java.util.concurrent.Callable;
21 import org.opendaylight.yangtools.binding.data.codec.impl.ValueTypeCodec.SchemaUnawareCodec;
22 import org.opendaylight.yangtools.yang.binding.BindingMapping;
23 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
26 class BitsCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
28 private final ImmutableSortedMap<String, Method> valueGetters;
29 private final Constructor<?> constructor;
31 private BitsCodec(final Class<?> typeClass, final SortedMap<String, Method> valueGetters,
32 final Constructor<?> constructor) {
34 this.valueGetters = ImmutableSortedMap.copyOf(valueGetters);
35 this.constructor = constructor;
38 static Callable<BitsCodec> loader(final Class<?> returnType,
39 final BitsTypeDefinition rootType) {
40 return new Callable<BitsCodec>() {
43 public BitsCodec call() throws Exception {
45 SortedMap<String, Method> valueGetters = new TreeMap<>();
46 for (Bit bit : rootType.getBits()) {
47 String bindingName = BindingMapping.getClassName(bit.getName());
48 Method valueGetter = returnType.getMethod("is" + bindingName);
49 valueGetters.put(bit.getName(), valueGetter);
52 Constructor<?> constructor = null;
53 for (Constructor<?> cst : returnType.getConstructors()) {
54 if (cst.getParameterTypes()[0].equals(returnType)) {
60 return new BitsCodec(returnType, valueGetters, constructor);
61 } catch (IllegalArgumentException | NoSuchMethodException | SecurityException e) {
62 throw new IllegalStateException(e);
69 public Object deserialize(final Object input) {
70 Preconditions.checkArgument(input instanceof Set);
71 @SuppressWarnings("unchecked")
72 Set<String> casted = (Set<String>) input;
74 Object args[] = new Object[valueGetters.size()];
77 * We could do this walk based on field set
78 * sorted by name, since constructor arguments in
79 * Java Binding are sorted by name, so arguments will match
83 for (String value : valueGetters.keySet()) {
84 if (casted.contains(value)) {
85 args[currentArg] = Boolean.TRUE;
87 args[currentArg] = Boolean.FALSE;
93 return constructor.newInstance(args);
94 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
95 throw new IllegalStateException(e);
100 public Object serialize(final Object input) {
101 Set<String> result = new HashSet<>();
102 for (Entry<String, Method> valueGet : valueGetters.entrySet()) {
104 Boolean value = (Boolean) valueGet.getValue().invoke(input);
106 result.add(valueGet.getKey());
108 } catch (IllegalAccessException | InvocationTargetException e) {
109 throw new IllegalArgumentException(e);