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.yangtools.yang.parser.builder.impl;
12 import org.opendaylight.yangtools.yang.model.api.*;
13 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
14 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractBuilder;
15 import org.opendaylight.yangtools.yang.parser.util.*;
17 public final class DeviationBuilder extends AbstractBuilder {
18 private boolean isBuilt;
19 private final DeviationImpl instance;
20 private final String targetPathStr;
21 private SchemaPath targetPath;
23 DeviationBuilder(final String moduleName, final int line, final String targetPathStr) {
24 super(moduleName, line);
25 if (!targetPathStr.startsWith("/")) {
26 throw new YangParseException(moduleName, line,
27 "Deviation argument string must be an absolute schema node identifier.");
29 this.targetPathStr = targetPathStr;
30 this.targetPath = ParserUtils.parseXPathString(targetPathStr);
31 instance = new DeviationImpl();
35 public Deviation build() {
36 if (targetPath == null) {
37 throw new YangParseException(moduleName, line, "Unresolved deviation target");
41 instance.setTargetPath(targetPath);
44 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
45 unknownNodes.add(b.build());
47 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
48 instance.addUnknownSchemaNodes(unknownNodes);
56 public SchemaPath getTargetPath() {
60 public void setTargetPath(final SchemaPath targetPath) {
61 this.targetPath = targetPath;
64 public void setDeviate(final String deviate) {
65 if ("not-supported".equals(deviate)) {
66 instance.setDeviate(Deviate.NOT_SUPPORTED);
67 } else if ("add".equals(deviate)) {
68 instance.setDeviate(Deviate.ADD);
69 } else if ("replace".equals(deviate)) {
70 instance.setDeviate(Deviate.REPLACE);
71 } else if ("delete".equals(deviate)) {
72 instance.setDeviate(Deviate.DELETE);
74 throw new YangParseException(moduleName, line, "Unsupported type of 'deviate' statement: " + deviate);
78 public void setReference(final String reference) {
79 instance.reference = reference;
83 public String toString() {
84 return "deviation " + targetPathStr;
87 private static final class DeviationImpl implements Deviation {
88 private SchemaPath targetPath;
89 private Deviate deviate;
90 private String reference;
91 private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
93 private DeviationImpl() {
97 public SchemaPath getTargetPath() {
101 private void setTargetPath(final SchemaPath targetPath) {
102 this.targetPath = targetPath;
106 public Deviate getDeviate() {
110 private void setDeviate(final Deviate deviate) {
111 this.deviate = deviate;
115 public String getReference() {
120 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
121 return Collections.unmodifiableList(unknownNodes);
124 private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
125 if (unknownSchemaNodes != null) {
126 this.unknownNodes.addAll(unknownSchemaNodes);
131 public int hashCode() {
132 final int prime = 31;
134 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
135 result = prime * result + ((deviate == null) ? 0 : deviate.hashCode());
136 result = prime * result + ((reference == null) ? 0 : reference.hashCode());
141 public boolean equals(Object obj) {
148 if (getClass() != obj.getClass()) {
151 DeviationImpl other = (DeviationImpl) obj;
152 if (targetPath == null) {
153 if (other.targetPath != null) {
156 } else if (!targetPath.equals(other.targetPath)) {
159 if (deviate == null) {
160 if (other.deviate != null) {
163 } else if (!deviate.equals(other.deviate)) {
166 if (reference == null) {
167 if (other.reference != null) {
170 } else if (!reference.equals(other.reference)) {
177 public String toString() {
178 StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
180 sb.append("targetPath=" + targetPath);
181 sb.append(", deviate=" + deviate);
182 sb.append(", reference=" + reference);
184 return sb.toString();