Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / AbstractRestrictedTypeBuilder.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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verifyNotNull;
12
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16
17 abstract class AbstractRestrictedTypeBuilder<T extends TypeDefinition<T>> extends TypeBuilder<T> {
18     private boolean touched;
19
20     AbstractRestrictedTypeBuilder(final T baseType, final SchemaPath path) {
21         super(baseType, path);
22         if (baseType != null) {
23             checkArgument(baseType instanceof AbstractBaseType || baseType instanceof AbstractDerivedType,
24                 "Restricted type has to be based on either a base or derived type, not %s", baseType);
25         } else {
26             touched = true;
27         }
28     }
29
30     final void touch() {
31         touched = true;
32     }
33
34     abstract @NonNull T buildType();
35
36     @Override
37     public final T build() {
38         return touched ? buildType() : verifyNotNull(getBaseType());
39     }
40 }