Bug 6414: Fixed DataNodeIterator's traverseModule method
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / DerivedTypeBuilder.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util.type;
9
10 import com.google.common.base.Preconditions;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
13 import org.opendaylight.yangtools.yang.model.api.Status;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Builder of {@link TypeDefinitions} for use in typedef statements.
20  *
21  * @param <T> Resulting {@link TypeDefinition}
22  */
23 public abstract class DerivedTypeBuilder<T extends TypeDefinition<T>> extends TypeBuilder<T> {
24     private static final Logger LOG = LoggerFactory.getLogger(DecimalTypeBuilder.class);
25     private Object defaultValue;
26     private String description;
27     private String reference;
28     private Status status = Status.CURRENT;
29     private String units;
30
31     DerivedTypeBuilder(final T baseType, final SchemaPath path) {
32         super(Preconditions.checkNotNull(baseType), path);
33
34         Preconditions.checkArgument(baseType instanceof AbstractBaseType ||
35             baseType instanceof AbstractDerivedType || baseType instanceof AbstractRestrictedType,
36             "Derived type can be built only from a base, derived, or restricted type, not %s", baseType);
37
38         // http://tools.ietf.org/html/rfc6020#section-7.3.4
39         defaultValue = baseType.getDefaultValue();
40
41         // In similar vein, it makes sense to propagate units
42         units = baseType.getUnits();
43     }
44
45     public void setDefaultValue(@Nonnull final Object defaultValue) {
46         this.defaultValue = Preconditions.checkNotNull(defaultValue);
47     }
48
49     public final void setDescription(@Nonnull final String description) {
50         this.description = Preconditions.checkNotNull(description);
51     }
52
53     public final void setReference(@Nonnull final String reference) {
54         this.reference = Preconditions.checkNotNull(reference);
55     }
56
57     public final void setStatus(@Nonnull final Status status) {
58         this.status = Preconditions.checkNotNull(status);
59     }
60
61     public final void setUnits(final String units) {
62         Preconditions.checkNotNull(units);
63         if (getBaseType().getUnits() != null && !units.equals(getBaseType().getUnits())) {
64             LOG.warn("Type {} uverrides 'units' of type {} to \"{}\"", getPath(), getBaseType(), units);
65         }
66
67         this.units = units;
68     }
69
70     final Object getDefaultValue() {
71         return defaultValue;
72     }
73
74     final String getDescription() {
75         return description;
76     }
77
78     final String getReference() {
79         return reference;
80     }
81
82     final Status getStatus() {
83         return status;
84     }
85
86     final String getUnits() {
87         return units;
88     }
89 }