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