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.sal.binding.generator.impl;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
14 import java.util.Map.Entry;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.Node;
18 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
19 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
21 import com.google.common.base.Preconditions;
23 public class IntermediateMapping {
24 private IntermediateMapping() {
25 throw new UnsupportedOperationException("Utility class should not be instantiated");
28 public static Node<? extends Object> toNode(final Map<? extends Object, ? extends Object> map) {
29 if ((map instanceof Node<?>)) {
30 return ((Node<?>) map);
32 @SuppressWarnings("unchecked")
33 final Map<QName, Object> nodeMap = ((Map<QName, Object>) map);
34 Preconditions.checkArgument(map.size() == 1);
35 final Entry<QName, Object> elem = nodeMap.entrySet().iterator().next();
36 final QName qname = elem.getKey();
37 final Object value = elem.getValue();
38 return toNodeImpl(qname, value);
41 protected static Node<? extends Object> _toNodeImpl(final QName name, final List<? extends Object> objects) {
42 List<Node<? extends Object>> values = new ArrayList<>(objects.size());
43 for (Object obj : objects) {
44 if ((obj instanceof Node<?>)) {
45 values.add(((Node<?>) obj));
47 if ((obj instanceof Map<?, ?>)) {
48 Node<? extends Object> _node = IntermediateMapping.toNode(((Map<?, ?>) obj));
53 return new CompositeNodeTOImpl(name, null, values);
56 protected static Node<? extends Object> _toNodeImpl(final QName name, final Map<QName, Object> object) {
57 throw new UnsupportedOperationException("Unsupported node hierarchy.");
60 protected static Node<? extends Object> _toNodeImpl(final QName name, final Object object) {
61 return new SimpleNodeTOImpl<Object>(name, null, object);
64 @SuppressWarnings("unchecked")
65 public static Node<? extends Object> toNodeImpl(final QName name, final Object objects) {
66 if (objects instanceof List) {
67 return _toNodeImpl(name, (List<?>) objects);
68 } else if (objects instanceof Map) {
69 return _toNodeImpl(name, (Map<QName, Object>) objects);
70 } else if (objects != null) {
71 return _toNodeImpl(name, objects);
73 throw new IllegalArgumentException("Unhandled parameter types: "
74 + Arrays.<Object> asList(name, objects).toString());