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;
10 import com.google.common.collect.ImmutableList;
11 import java.util.List;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.model.api.Deviation;
14 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
17 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
18 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractBuilder;
19 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
21 public final class DeviationBuilder extends AbstractBuilder {
22 private DeviationImpl instance;
23 private final SchemaPath targetPath;
24 private Deviate deviate;
25 private String reference;
27 DeviationBuilder(final String moduleName, final int line, final SchemaPath targetPath) {
28 super(moduleName, line);
29 this.targetPath = targetPath;
33 public Deviation build() {
34 if (instance != null) {
37 if (targetPath == null) {
38 throw new YangParseException(getModuleName(), getLine(), "Unresolved deviation target");
41 instance = new DeviationImpl(targetPath);
42 instance.deviate = deviate;
43 instance.reference = reference;
46 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
47 unknownNodes.add(b.build());
49 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
54 public SchemaPath getTargetPath() {
58 public void setDeviate(final String deviate) {
59 if ("not-supported".equals(deviate)) {
60 this.deviate = Deviate.NOT_SUPPORTED;
61 } else if ("add".equals(deviate)) {
62 this.deviate = Deviate.ADD;
63 } else if ("replace".equals(deviate)) {
64 this.deviate = Deviate.REPLACE;
65 } else if ("delete".equals(deviate)) {
66 this.deviate = Deviate.DELETE;
68 throw new YangParseException(getModuleName(), getLine(), "Unsupported type of 'deviate' statement: " + deviate);
72 public void setReference(final String reference) {
73 this.reference = reference;
77 public String toString() {
78 return "deviation " + targetPath;
81 private static final class DeviationImpl implements Deviation {
82 private final SchemaPath targetPath;
83 private Deviate deviate;
84 private String reference;
85 private ImmutableList<UnknownSchemaNode> unknownNodes;
87 private DeviationImpl(final SchemaPath targetPath) {
88 this.targetPath = targetPath;
92 public SchemaPath getTargetPath() {
97 public Deviate getDeviate() {
102 public String getReference() {
107 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
112 public int hashCode() {
113 final int prime = 31;
115 result = prime * result + Objects.hashCode(targetPath);
116 result = prime * result + Objects.hashCode(deviate);
117 result = prime * result + Objects.hashCode(reference);
122 public boolean equals(Object obj) {
129 if (getClass() != obj.getClass()) {
132 DeviationImpl other = (DeviationImpl) obj;
133 if (targetPath == null) {
134 if (other.targetPath != null) {
137 } else if (!targetPath.equals(other.targetPath)) {
140 if (deviate == null) {
141 if (other.deviate != null) {
144 } else if (!deviate.equals(other.deviate)) {
147 if (reference == null) {
148 if (other.reference != null) {
151 } else if (!reference.equals(other.reference)) {
158 public String toString() {
159 StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
161 sb.append("targetPath=").append(targetPath);
162 sb.append(", deviate=").append(deviate);
163 sb.append(", reference=").append(reference);
165 return sb.toString();