Merge "Added test suite for parser builder implementation"
[yangtools.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 com.google.common.base.Preconditions;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.Node;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
22 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
23
24 /**
25  * @deprecated Use {@link NormalizedNode} and their stream writer codec suite.
26  */
27 @Deprecated
28 public class IntermediateMapping {
29     private IntermediateMapping() {
30         throw new UnsupportedOperationException("Utility class should not be instantiated");
31     }
32
33     public static Node<? extends Object> toNode(final Map<? extends Object, ? extends Object> map) {
34         if ((map instanceof Node<?>)) {
35             return ((Node<?>) map);
36         }
37         @SuppressWarnings("unchecked")
38         final Map<QName, Object> nodeMap = ((Map<QName, Object>) map);
39         Preconditions.checkArgument(map.size() == 1);
40         final Entry<QName, Object> elem = nodeMap.entrySet().iterator().next();
41         final QName qname = elem.getKey();
42         final Object value = elem.getValue();
43         return toNodeImpl(qname, value);
44     }
45
46     protected static Node<? extends Object> _toNodeImpl(final QName name, final List<? extends Object> objects) {
47         List<Node<? extends Object>> values = new ArrayList<>(objects.size());
48         for (Object obj : objects) {
49             if ((obj instanceof Node<?>)) {
50                 values.add(((Node<?>) obj));
51             } else {
52                 if ((obj instanceof Map<?, ?>)) {
53                     Node<? extends Object> _node = IntermediateMapping.toNode(((Map<?, ?>) obj));
54                     values.add(_node);
55                 }
56             }
57         }
58         return new CompositeNodeTOImpl(name, null, values);
59     }
60
61     protected static Node<? extends Object> _toNodeImpl(final QName name, final Map<QName, Object> object) {
62         throw new UnsupportedOperationException("Unsupported node hierarchy.");
63     }
64
65     protected static Node<? extends Object> _toNodeImpl(final QName name, final Object object) {
66         return new SimpleNodeTOImpl<Object>(name, null, object);
67     }
68
69     @SuppressWarnings("unchecked")
70     public static Node<? extends Object> toNodeImpl(final QName name, final Object objects) {
71         if (objects instanceof List) {
72             return _toNodeImpl(name, (List<?>) objects);
73         } else if (objects instanceof Map) {
74             return _toNodeImpl(name, (Map<QName, Object>) objects);
75         } else if (objects != null) {
76             return _toNodeImpl(name, objects);
77         } else {
78             throw new IllegalArgumentException("Unhandled parameter types: "
79                     + Arrays.<Object> asList(name, objects).toString());
80         }
81     }
82
83 }