ec029163348b73874d890a1a1f3602672423fbe4
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / Nodes.java
1 /*
2  * Copyright (c) 2013 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.yang.data.util;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
18 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
19 import org.opendaylight.yangtools.yang.data.api.Node;
20 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
21
22 public final class Nodes {
23
24     private Nodes() {
25     }
26
27     public static <T> SimpleNode<T> leafNode(QName name, T value) {
28         return new SimpleNodeTO<T>(name, value, null);
29     }
30
31     public static CompositeNode containerNode(QName name, List<Node<?>> children) {
32         return containerNode(name, children, null);
33     }
34
35     public static CompositeNode containerNode(QName name, List<Node<?>> children, CompositeNode parent) {
36         return new ContainerNodeTO(name, parent, nodeMapFromList(children));
37     }
38
39     public static Map<QName, List<Node<?>>> nodeMapFromList(List<Node<?>> children) {
40         Map<QName, List<Node<?>>> map = new HashMap<QName, List<Node<?>>>();
41         for (Node<?> node : children) {
42
43             QName name = node.getNodeType();
44             List<Node<?>> targetList = map.get(name);
45             if (targetList == null) {
46                 targetList = new ArrayList<Node<?>>();
47                 map.put(name, targetList);
48             }
49             targetList.add(node);
50         }
51         return map;
52     }
53
54     private static class ContainerNodeTO extends AbstractContainerNode {
55
56         private final Map<QName, List<Node<?>>> nodeMap;
57
58         public ContainerNodeTO(QName name, Map<QName, List<Node<?>>> nodeMap) {
59             super(name);
60             this.nodeMap = nodeMap;
61         }
62
63         public ContainerNodeTO(QName name, CompositeNode parent, Map<QName, List<Node<?>>> nodeMap) {
64             super(name, parent);
65             this.nodeMap = nodeMap;
66         }
67
68         @Override
69         protected Map<QName, List<Node<?>>> getNodeMap() {
70
71             return nodeMap;
72         }
73
74         /*
75          * (non-Javadoc)
76          * 
77          * @see
78          * org.opendaylight.yangtools.yang.data.api.CompositeNode#asMutable()
79          */
80         @Override
81         public MutableCompositeNode asMutable() {
82             // TODO Auto-generated method stub
83             return null;
84         }
85
86     }
87
88     private static class SimpleNodeTO<T> extends AbstractNode<T> implements SimpleNode<T> {
89
90         private final T value;
91
92         protected SimpleNodeTO(QName name, T val, CompositeNode parent) {
93             super(name, parent);
94             value = val;
95
96         }
97
98         @Override
99         public T getValue() {
100             return value;
101         }
102
103         /*
104          * (non-Javadoc)
105          * 
106          * @see org.opendaylight.yangtools.yang.data.api.SimpleNode#asMutable()
107          */
108         @Override
109         public MutableSimpleNode<T> asMutable() {
110             // TODO Auto-generated method stub
111             return null;
112         }
113
114     }
115
116 }