/* * 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.parser.builder.api; import java.util.Collections; import java.util.HashSet; import java.util.Set; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.model.api.DataSchemaNode; import org.opendaylight.controller.yang.model.api.GroupingDefinition; import org.opendaylight.controller.yang.parser.util.YangParseException; public abstract class AbstractDataNodeContainerBuilder implements DataNodeContainerBuilder { protected final int line; protected final QName qname; protected Builder parent; protected Set childNodes; protected final Set addedChildNodes = new HashSet(); protected Set groupings; protected final Set addedGroupings = new HashSet(); protected AbstractDataNodeContainerBuilder(final int line, final QName qname) { this.line = line; this.qname = qname; } @Override public int getLine() { return line; } @Override public Builder getParent() { return parent; } @Override public void setParent(final Builder parent) { this.parent = parent; } @Override public QName getQName() { return qname; } @Override public Set getChildNodes() { if (childNodes == null) { return Collections.emptySet(); } return childNodes; } public void setChildNodes(Set childNodes) { this.childNodes = childNodes; } @Override public Set getChildNodeBuilders() { return addedChildNodes; } @Override public DataSchemaNodeBuilder getDataChildByName(final String name) { for (DataSchemaNodeBuilder child : addedChildNodes) { if (child.getQName().getLocalName().equals(name)) { return child; } } return null; } @Override public void addChildNode(DataSchemaNodeBuilder child) { for (DataSchemaNodeBuilder childNode : addedChildNodes) { if (childNode.getQName().getLocalName().equals(child.getQName().getLocalName())) { throw new YangParseException(child.getLine(), "Duplicate node found at line " + childNode.getLine()); } } addedChildNodes.add(child); } @Override public Set getGroupings() { if (groupings == null) { return Collections.emptySet(); } return groupings; } public void setGroupings(final Set groupings) { this.groupings = groupings; } public Set getGroupingBuilders() { return addedGroupings; } @Override public void addGrouping(GroupingBuilder groupingBuilder) { for (GroupingBuilder gb : addedGroupings) { if (gb.getQName().getLocalName().equals(groupingBuilder.getQName().getLocalName())) { throw new YangParseException(groupingBuilder.getLine(), "Duplicate node found at line " + gb.getLine()); } } addedGroupings.add(groupingBuilder); } }