2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util.type;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Optional;
14 import org.eclipse.jdt.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.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Builder of {@link TypeDefinition}s for use in typedef statements.
24 * @param <T> Resulting {@link TypeDefinition}
26 public abstract class DerivedTypeBuilder<T extends TypeDefinition<T>> extends TypeBuilder<T> {
27 private static final Logger LOG = LoggerFactory.getLogger(DerivedTypeBuilder.class);
28 private Object defaultValue;
29 private String description;
30 private String reference;
31 private Status status = Status.CURRENT;
34 DerivedTypeBuilder(final T baseType, final SchemaPath path) {
35 super(requireNonNull(baseType), path);
37 checkArgument(baseType instanceof AbstractBaseType || baseType instanceof AbstractDerivedType
38 || baseType instanceof AbstractRestrictedType,
39 "Derived type can be built only from a base, derived, or restricted type, not %s", baseType);
41 // http://tools.ietf.org/html/rfc6020#section-7.3.4
42 defaultValue = baseType.getDefaultValue().orElse(null);
44 // In similar vein, it makes sense to propagate units
45 units = baseType.getUnits().orElse(null);
48 public void setDefaultValue(final @NonNull Object defaultValue) {
49 this.defaultValue = requireNonNull(defaultValue);
52 public final void setDescription(final @NonNull String description) {
53 this.description = requireNonNull(description);
56 public final void setReference(final @NonNull String reference) {
57 this.reference = requireNonNull(reference);
60 public final void setStatus(final @NonNull Status status) {
61 this.status = requireNonNull(status);
64 public final void setUnits(final String units) {
65 requireNonNull(units);
67 final Optional<String> baseUnits = getBaseType().getUnits();
68 if (baseUnits.isPresent() && !units.equals(baseUnits.get())) {
69 LOG.warn("Type {} uverrides 'units' of type {} to \"{}\"", getPath(), getBaseType(), units);
75 final Object getDefaultValue() {
79 final String getDescription() {
83 final String getReference() {
87 final Status getStatus() {
91 final String getUnits() {