Populate data/ hierarchy
[yangtools.git] / yang / yang-model-ri / src / main / java / org / opendaylight / yangtools / yang / model / ri / type / AbstractDerivedType.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 java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import java.util.Collection;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20
21 abstract class AbstractDerivedType<T extends TypeDefinition<T>> extends AbstractTypeDefinition<T> {
22     private final @NonNull T baseType;
23     private final Object defaultValue;
24     private final String description;
25     private final String reference;
26     private final @NonNull Status status;
27     private final String units;
28
29     AbstractDerivedType(final T baseType, final QName qname, final Object defaultValue, final String description,
30             final String reference, final Status status, final String units,
31             final Collection<? extends UnknownSchemaNode> unknownSchemNodes) {
32         super(qname, unknownSchemNodes);
33         this.baseType = requireNonNull(baseType);
34         this.status = requireNonNull(status);
35         this.defaultValue = defaultValue;
36         this.description = description;
37         this.reference = reference;
38         this.units = units;
39     }
40
41     @Override
42     public final T getBaseType() {
43         return baseType instanceof AbstractRestrictedType ? baseType.getBaseType() : baseType;
44     }
45
46     /**
47      * Return the stored base type. Unlike {@link #getBaseType()}, this method returns the underlying restricted type.
48      */
49     final T baseType() {
50         return baseType;
51     }
52
53     @Override
54     public final Optional<? extends Object> getDefaultValue() {
55         return defaultValue != null ? Optional.of(defaultValue) : baseType.getDefaultValue();
56     }
57
58     @Override
59     public final Optional<String> getDescription() {
60         return Optional.ofNullable(description);
61     }
62
63     @Override
64     public final Optional<String> getReference() {
65         return Optional.ofNullable(reference);
66     }
67
68     @Override
69     public final Status getStatus() {
70         return status;
71     }
72
73     @Override
74     public final Optional<String> getUnits() {
75         return units != null ? Optional.of(units) : baseType.getUnits();
76     }
77
78     @Override
79     public final String toString() {
80         return MoreObjects.toStringHelper(this).omitNullValues()
81                 .add("baseType", baseType)
82                 .add("default", defaultValue)
83                 .add("description", description)
84                 .add("qname", getQName())
85                 .add("reference", reference)
86                 .add("status", status)
87                 .add("units", units).toString();
88     }
89 }