/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.yang.model.util; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.opendaylight.controller.yang.model.api.*; public class DataNodeIterator implements Iterator { private final DataNodeContainer container; private final List allLists; private final List allContainers; private final List allChoices; private final List allChilds; public DataNodeIterator(final DataNodeContainer container) { if (container == null) { throw new IllegalArgumentException("Data Node Container MUST be specified and cannot be NULL!"); } this.allContainers = new ArrayList<>(); this.allLists = new ArrayList<>(); this.allChilds = new ArrayList<>(); this.allChoices = new ArrayList<>(); this.container = container; traverse(this.container); } public List allContainers() { return allContainers; } public List allLists() { return allLists; } public List allChoices() { return allChoices; } private void traverse(final DataNodeContainer dataNode) { if (dataNode == null) { return; } final Set childNodes = dataNode.getChildNodes(); if (childNodes != null) { for (DataSchemaNode childNode : childNodes) { if (childNode.isAugmenting()) { continue; } allChilds.add(childNode); if (childNode instanceof ContainerSchemaNode) { final ContainerSchemaNode container = (ContainerSchemaNode) childNode; allContainers.add(container); traverse(container); } else if (childNode instanceof ListSchemaNode) { final ListSchemaNode list = (ListSchemaNode) childNode; allLists.add(list); traverse(list); } else if (childNode instanceof ChoiceNode) { final ChoiceNode choiceNode = (ChoiceNode) childNode; allChoices.add(choiceNode); final Set cases = choiceNode.getCases(); if (cases != null) { for (final ChoiceCaseNode caseNode : cases) { traverse(caseNode); } } } } } final Set groupings = dataNode.getGroupings(); if (groupings != null) { for (GroupingDefinition grouping : groupings) { traverse(grouping); } } } @Override public boolean hasNext() { if (container.getChildNodes() != null) { final Set childNodes = container.getChildNodes(); if ((childNodes != null) && !childNodes.isEmpty()) { return childNodes.iterator().hasNext(); } } return false; } @Override public DataSchemaNode next() { return allChilds.iterator().next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }