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