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