Deprecate DataNodeIterator
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / DataNodeIterator.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.model.util;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Iterator;
13 import java.util.List;
14 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21
22 /**
23  * DataNodeIterator is iterator, which walks down whole YANG DataNodeContainer
24  * and walks all instances of {@link DataSchemaNode} present in subtree.
25  *
26  * <p>
27  * Iterator instance is eagerly created, walking happens on initialization. Iteration is not ordered.
28  *
29  * @deprecated Use {@link SchemaNodeUtils#getAllContainers(DataNodeContainer)},
30  *             {@link SchemaNodeUtils#getAllTypeDefinitions(DataNodeContainer)} or
31  *             {@link SchemaNodeUtils#traverse(DataNodeAggregator, DataNodeContainer)} instead.
32  */
33 @Deprecated
34 public class DataNodeIterator extends DataNodeAggregator implements Iterator<DataSchemaNode> {
35     private final List<ListSchemaNode> allLists = new ArrayList<>();
36     private final List<ContainerSchemaNode> allContainers = new ArrayList<>();
37     private final List<ChoiceSchemaNode> allChoices = new ArrayList<>();
38     private final List<DataSchemaNode> allChilds = new ArrayList<>();
39     private final List<GroupingDefinition> allGroupings = new ArrayList<>();
40     private final List<TypeDefinition<?>> allTypedefs = new ArrayList<>();
41
42     private final DataNodeContainer container;
43
44     public DataNodeIterator(final DataNodeContainer container) {
45         if (container == null) {
46             throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!");
47         }
48
49         this.container = container;
50         SchemaNodeUtils.traverse(this, container);
51     }
52
53     /**
54      * Returns list all containers present in subtree.
55      *
56      * @return Returns list all containers present in subtree.
57      */
58     public List<ContainerSchemaNode> allContainers() {
59         return allContainers;
60     }
61
62     /**
63      * Returns list all lists present in subtree.
64      *
65      * @return Returns list all containers present in subtree.
66      */
67     public List<ListSchemaNode> allLists() {
68         return allLists;
69     }
70
71     /**
72      * Returns list all choices present in subtree.
73      *
74      * @return Returns list all containers present in subtree.
75      */
76     public List<ChoiceSchemaNode> allChoices() {
77         return allChoices;
78     }
79
80     /**
81      * Returns list all groupings present in subtree.
82      *
83      * @return Returns list all containers present in subtree.
84      */
85     public List<GroupingDefinition> allGroupings() {
86         return allGroupings;
87     }
88
89     /**
90      * Returns list all typedefs present in subtree.
91      *
92      * @return Returns list all containers present in subtree.
93      */
94     public List<TypeDefinition<?>> allTypedefs() {
95         return allTypedefs;
96     }
97
98     @Override
99     public boolean hasNext() {
100         return !container.getChildNodes().isEmpty();
101     }
102
103     @Override
104     public DataSchemaNode next() {
105         return allChilds.iterator().next();
106     }
107
108     @Override
109     protected void addChild(final DataSchemaNode childNode) {
110         allChilds.add(childNode);
111     }
112
113     @Override
114     protected void addContainer(final ContainerSchemaNode containerNode) {
115         allContainers.add(containerNode);
116     }
117
118     @Override
119     protected void addList(final ListSchemaNode list) {
120         allLists.add(list);
121     }
122
123     @Override
124     protected void addChoice(final ChoiceSchemaNode choiceNode) {
125         allChoices.add(choiceNode);
126     }
127
128     @Override
129     protected void addTypedefs(final Collection<? extends TypeDefinition<?>> typeDefs) {
130         allTypedefs.addAll(typeDefs);
131     }
132
133     @Override
134     protected void addGrouping(final GroupingDefinition grouping) {
135         allGroupings.add(grouping);
136     }
137 }