Merge changes Ibfbb4deb,I96e28324
[mdsal.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / IntermediateMapping.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.sal.binding.generator.impl;
9
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
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;
20
21 import com.google.common.base.Preconditions;
22
23 public class IntermediateMapping {
24     private IntermediateMapping() {
25         throw new UnsupportedOperationException("Utility class should not be instantiated");
26     }
27
28     public static Node<? extends Object> toNode(final Map<? extends Object, ? extends Object> map) {
29         if ((map instanceof Node<?>)) {
30             return ((Node<?>) map);
31         }
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);
39     }
40
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));
46             } else {
47                 if ((obj instanceof Map<?, ?>)) {
48                     Node<? extends Object> _node = IntermediateMapping.toNode(((Map<?, ?>) obj));
49                     values.add(_node);
50                 }
51             }
52         }
53         return new CompositeNodeTOImpl(name, null, values);
54     }
55
56     protected static Node<? extends Object> _toNodeImpl(final QName name, final Map<QName, Object> object) {
57         throw new UnsupportedOperationException("Unsupported node hierarchy.");
58     }
59
60     protected static Node<? extends Object> _toNodeImpl(final QName name, final Object object) {
61         return new SimpleNodeTOImpl<Object>(name, null, object);
62     }
63
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);
72         } else {
73             throw new IllegalArgumentException("Unhandled parameter types: "
74                     + Arrays.<Object> asList(name, objects).toString());
75         }
76     }
77
78 }