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 org.opendaylight.yangtools.yang.model.api.Deviation;
13 import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
16 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
17 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractBuilder;
18 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
20 public final class DeviationBuilder extends AbstractBuilder {
21 private DeviationImpl instance;
22 private final SchemaPath targetPath;
23 private Deviate deviate;
24 private String reference;
26 DeviationBuilder(final String moduleName, final int line, final SchemaPath targetPath) {
27 super(moduleName, line);
28 this.targetPath = targetPath;
32 public Deviation build() {
33 if (instance != null) {
36 if (targetPath == null) {
37 throw new YangParseException(getModuleName(), getLine(), "Unresolved deviation target");
40 instance = new DeviationImpl(targetPath);
41 instance.deviate = deviate;
42 instance.reference = reference;
45 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
46 unknownNodes.add(b.build());
48 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
53 public SchemaPath getTargetPath() {
57 public void setDeviate(final String deviate) {
58 if ("not-supported".equals(deviate)) {
59 this.deviate = Deviate.NOT_SUPPORTED;
60 } else if ("add".equals(deviate)) {
61 this.deviate = Deviate.ADD;
62 } else if ("replace".equals(deviate)) {
63 this.deviate = Deviate.REPLACE;
64 } else if ("delete".equals(deviate)) {
65 this.deviate = Deviate.DELETE;
67 throw new YangParseException(getModuleName(), getLine(), "Unsupported type of 'deviate' statement: " + deviate);
71 public void setReference(final String reference) {
72 this.reference = reference;
76 public String toString() {
77 return "deviation " + targetPath;
80 private static final class DeviationImpl implements Deviation {
81 private final SchemaPath targetPath;
82 private Deviate deviate;
83 private String reference;
84 private ImmutableList<UnknownSchemaNode> unknownNodes;
86 private DeviationImpl(final SchemaPath targetPath) {
87 this.targetPath = targetPath;
91 public SchemaPath getTargetPath() {
96 public Deviate getDeviate() {
101 public String getReference() {
106 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
111 public int hashCode() {
112 final int prime = 31;
114 result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
115 result = prime * result + ((deviate == null) ? 0 : deviate.hashCode());
116 result = prime * result + ((reference == null) ? 0 : reference.hashCode());
121 public boolean equals(Object obj) {
128 if (getClass() != obj.getClass()) {
131 DeviationImpl other = (DeviationImpl) obj;
132 if (targetPath == null) {
133 if (other.targetPath != null) {
136 } else if (!targetPath.equals(other.targetPath)) {
139 if (deviate == null) {
140 if (other.deviate != null) {
143 } else if (!deviate.equals(other.deviate)) {
146 if (reference == null) {
147 if (other.reference != null) {
150 } else if (!reference.equals(other.reference)) {
157 public String toString() {
158 StringBuilder sb = new StringBuilder(DeviationImpl.class.getSimpleName());
160 sb.append("targetPath=").append(targetPath);
161 sb.append(", deviate=").append(deviate);
162 sb.append(", reference=").append(reference);
164 return sb.toString();