Cleanup DocumentedNode
[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         setStatus(baseType.getStatus());
30         baseType.getDescription().ifPresent(this::setDescription);
31         baseType.getReference().ifPresent(this::setReference);
32     }
33
34     /**
35      * Build the resulting type.
36      *
37      * @return A new type instance
38      */
39     @Nonnull abstract T buildType();
40
41     @Override
42     public final T build() {
43         final T base = getBaseType();
44         if (Objects.equals(getDefaultValue(), base.getDefaultValue()) && Objects.equals(getUnits(), base.getUnits())) {
45             return base;
46         }
47
48         return Verify.verifyNotNull(buildType());
49     }
50 }