2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.model.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.SchemaPath;
19 import org.opendaylight.controller.yang.model.api.UsesNode;
20 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
21 import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
22 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
24 public class UsesNodeBuilderImpl implements UsesNodeBuilder, Builder {
26 private final UsesNodeImpl instance;
27 private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();
29 UsesNodeBuilderImpl(String groupingPathStr) {
30 SchemaPath groupingPath = parseUsesPath(groupingPathStr);
31 instance = new UsesNodeImpl(groupingPath);
35 public UsesNode build() {
37 final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
38 for (AugmentationSchemaBuilder builder : addedAugments) {
39 augments.add(builder.build());
41 instance.setAugmentations(augments);
47 public void addAugment(AugmentationSchemaBuilder augmentBuilder) {
48 addedAugments.add(augmentBuilder);
52 public void setAugmenting(boolean augmenting) {
53 instance.setAugmenting(augmenting);
56 private SchemaPath parseUsesPath(String augmentPath) {
57 String[] splittedPath = augmentPath.split("/");
58 List<QName> path = new ArrayList<QName>();
60 for (String pathElement : splittedPath) {
61 String[] splittedElement = pathElement.split(":");
62 if (splittedElement.length == 1) {
63 name = new QName(null, null, null, splittedElement[0]);
65 name = new QName(null, null, splittedElement[0],
70 final boolean absolute = augmentPath.startsWith("/");
71 return new SchemaPath(path, absolute);
74 private static class UsesNodeImpl implements UsesNode {
76 private final SchemaPath groupingPath;
77 private Set<AugmentationSchema> augmentations = Collections.emptySet();
78 private boolean augmenting;
80 private UsesNodeImpl(SchemaPath groupingPath) {
81 this.groupingPath = groupingPath;
85 public SchemaPath getGroupingPath() {
90 public Set<AugmentationSchema> getAugmentations() {
94 private void setAugmentations(Set<AugmentationSchema> augmentations) {
95 if (augmentations != null) {
96 this.augmentations = augmentations;
101 public boolean isAugmenting() {
105 private void setAugmenting(boolean augmenting) {
106 this.augmenting = augmenting;
110 public int hashCode() {
111 final int prime = 31;
113 result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
114 result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
115 result = prime * result + (augmenting ? 1231 : 1237);
120 public boolean equals(Object obj) {
127 if (getClass() != obj.getClass()) {
130 UsesNodeImpl other = (UsesNodeImpl) obj;
131 if (groupingPath == null) {
132 if (other.groupingPath != null) {
135 } else if (!groupingPath.equals(other.groupingPath)) {
138 if (augmentations == null) {
139 if (other.augmentations != null) {
142 } else if (!augmentations.equals(other.augmentations)) {
145 if (augmenting != other.augmenting) {
152 public String toString() {
153 StringBuilder sb = new StringBuilder(
154 UsesNodeImpl.class.getSimpleName());
155 sb.append("[groupingPath=" + groupingPath +"]");
156 return sb.toString();