Move default value checks from TypeUtils to EffectiveStmtUtils
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / TypeUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.parser.stmt.rfc6020;
9
10 import com.google.common.base.Splitter;
11 import java.math.BigDecimal;
12 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
13
14 /**
15  * Utility class for manipulating YANG base and extended types implementation.
16  */
17 public final class TypeUtils {
18     public static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults();
19     public static final Splitter TWO_DOTS_SPLITTER = Splitter.on("..").trimResults();
20
21     // these objects are to compare whether range has MAX or MIN value
22     // none of these values should appear as Yang number according to spec so they are safe to use
23     private static final BigDecimal YANG_MIN_NUM = BigDecimal.valueOf(-Double.MAX_VALUE);
24     private static final BigDecimal YANG_MAX_NUM = BigDecimal.valueOf(Double.MAX_VALUE);
25
26     private TypeUtils() {
27     }
28
29     private static BigDecimal yangConstraintToBigDecimal(final Number number) {
30         if (UnresolvedNumber.max().equals(number)) {
31             return YANG_MAX_NUM;
32         }
33         if (UnresolvedNumber.min().equals(number)) {
34             return YANG_MIN_NUM;
35         }
36
37         return new BigDecimal(number.toString());
38     }
39
40     public static int compareNumbers(final Number n1, final Number n2) {
41         final BigDecimal num1 = yangConstraintToBigDecimal(n1);
42         final BigDecimal num2 = yangConstraintToBigDecimal(n2);
43         return new BigDecimal(num1.toString()).compareTo(new BigDecimal(num2.toString()));
44     }
45
46 }