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