d95b7f0df90082ca8aa141743cbb036b4e1d78e9
[yangtools.git] / model / yang-model-ri / src / main / java / org / opendaylight / yangtools / yang / model / ri / type / DecimalTypeBuilder.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.Preconditions.checkState;
11
12 import com.google.common.base.Preconditions;
13 import java.math.BigDecimal;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
16
17 public final class DecimalTypeBuilder extends RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> {
18     private Integer fractionDigits;
19
20     DecimalTypeBuilder(final QName qname) {
21         super(null, qname);
22     }
23
24     public DecimalTypeBuilder setFractionDigits(final int fractionDigits) {
25         checkState(this.fractionDigits == null, "Fraction digits already defined to %s", this.fractionDigits);
26         this.fractionDigits = fractionDigits;
27         return this;
28     }
29
30     @Override
31     DecimalTypeDefinition buildType() {
32         Preconditions.checkState(fractionDigits != null, "Fraction digits not defined");
33
34         return new BaseDecimalType(getQName(), getUnknownSchemaNodes(), fractionDigits,
35             calculateRangeConstraint(BaseDecimalType.constraintsForDigits(fractionDigits)));
36     }
37 }