BUG-8043: introduce RangeRestrictedTypeDefinition
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / ConcreteTypeBuilder.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.annotations.Beta;
11 import com.google.common.base.Verify;
12 import java.util.Objects;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16
17 /**
18  * Builder of {@link TypeDefinitions} for use in leaf statements. While similar to {@link DerivedTypeBuilder}, this
19  * builder does not support adding of unknown nodes and will return the base type if the type is not modified, hence
20  * not preserving the schema path.
21  *
22  * @param <T> Resulting {@link TypeDefinition}
23  */
24 @Beta
25 public abstract class ConcreteTypeBuilder<T extends TypeDefinition<T>> extends DerivedTypeBuilder<T> {
26     ConcreteTypeBuilder(final T baseType, final SchemaPath path) {
27         super(baseType, path);
28
29         if (baseType.getDescription() != null) {
30             setDescription(baseType.getDescription());
31         }
32         if (baseType.getReference() != null) {
33             setReference(baseType.getReference());
34         }
35         if (baseType.getStatus() != null) {
36             setStatus(baseType.getStatus());
37         }
38     }
39
40     /**
41      * Build the resulting type.
42      *
43      * @return A new type instance
44      */
45     @Nonnull abstract T buildType();
46
47     @Override
48     public final T build() {
49         final T base = getBaseType();
50         if (Objects.equals(getDefaultValue(), base.getDefaultValue()) && Objects.equals(getUnits(), base.getUnits())) {
51             return base;
52         }
53
54         return Verify.verifyNotNull(buildType());
55     }
56 }