blob: eebe6ab0e25762ed69b27d6b6dd69b6322b5b870 [file] [log] [blame]
Mahesh Poojary Huawei46fb4db2016-07-14 12:38:17 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.datamodel.utils;
18
19import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
20
21import java.math.BigDecimal;
22import java.util.ArrayList;
23
24/**
25 * The "fraction-digits" statement, which is a substatement to the
26 * "type" statement, MUST be present if the type is "decimal64". It
27 * takes as an argument an integer between 1 and 18, inclusively. It
28 * controls the size of the minimum difference between values of a
29 * decimal64 type, by restricting the value space to numbers that are
30 * expressible as "i x 10^-n" where n is the fraction-digits argument.
31 *
32 * +----------------+-----------------------+----------------------+
33 * | fraction-digit | min | max |
34 * +----------------+-----------------------+----------------------+
35 * | 1 | -922337203685477580.8 | 922337203685477580.7 |
36 * | 2 | -92233720368547758.08 | 92233720368547758.07 |
37 * | 3 | -9223372036854775.808 | 9223372036854775.807 |
38 * | 4 | -922337203685477.5808 | 922337203685477.5807 |
39 * | 5 | -92233720368547.75808 | 92233720368547.75807 |
40 * | 6 | -9223372036854.775808 | 9223372036854.775807 |
41 * | 7 | -922337203685.4775808 | 922337203685.4775807 |
42 * | 8 | -92233720368.54775808 | 92233720368.54775807 |
43 * | 9 | -9223372036.854775808 | 9223372036.854775807 |
44 * | 10 | -922337203.6854775808 | 922337203.6854775807 |
45 * | 11 | -92233720.36854775808 | 92233720.36854775807 |
46 * | 12 | -9223372.036854775808 | 9223372.036854775807 |
47 * | 13 | -922337.2036854775808 | 922337.2036854775807 |
48 * | 14 | -92233.72036854775808 | 92233.72036854775807 |
49 * | 15 | -9223.372036854775808 | 9223.372036854775807 |
50 * | 16 | -922.3372036854775808 | 922.3372036854775807 |
51 * | 17 | -92.23372036854775808 | 92.23372036854775807 |
52 * | 18 | -9.223372036854775808 | 9.223372036854775807 |
53 * +----------------+-----------------------+----------------------+
54 */
55
56/**
57 * Represents the decimal64 value range based on fraction-digits.
58 */
59public final class FractionDigits {
60
61 public static class Range {
62 private double min;
63 private double max;
64
65 /**
66 * Creates an instance of range.
67 *
68 * @param min minimum value of decimal64
69 * @param max maximum value of decimal64
70 */
71 protected Range(double min, double max) {
72 this.min = min;
73 this.max = max;
74 }
75
76 /**
77 * Retrieve minimum value range.
78 *
79 * @return minimum value range
80 */
81 public double getMin() {
82 return min;
83 }
84
85 /**
86 * Retrieve maximum value range.
87 *
88 * @return maximum value range
89 */
90 public double getMax() {
91 return max;
92 }
93 }
94
95 private static ArrayList<Range> decimal64ValueRange = null;
96
97 /**
98 * Creates a fraction-digits instance.
99 */
100 private FractionDigits() {
101 }
102
103 /**
104 * Generates decimal64 value range based on fraction-digits.
105 *
106 * @return decimal64 value range by fraction-digits as index
107 */
108 private static ArrayList<Range> getDecimal64ValueRange() {
109 if (decimal64ValueRange == null) {
110 decimal64ValueRange = new ArrayList<>();
111 decimal64ValueRange.add(new Range(-922337203685477580.8, 922337203685477580.7)); // fraction-digit: 1
112 decimal64ValueRange.add(new Range(-92233720368547758.08, 92233720368547758.07)); // fraction-digit: 2
113 decimal64ValueRange.add(new Range(-9223372036854775.808, 9223372036854775.807)); // fraction-digit: 3
114 decimal64ValueRange.add(new Range(-922337203685477.5808, 922337203685477.5807)); // fraction-digit: 4
115 decimal64ValueRange.add(new Range(-92233720368547.75808, 92233720368547.75807)); // fraction-digit: 5
116 decimal64ValueRange.add(new Range(-9223372036854.775808, 9223372036854.775807)); // fraction-digit: 6
117 decimal64ValueRange.add(new Range(-922337203685.4775808, 922337203685.4775807)); // fraction-digit: 7
118 decimal64ValueRange.add(new Range(-92233720368.54775808, 92233720368.54775807)); // fraction-digit: 8
119 decimal64ValueRange.add(new Range(-9223372036.854775808, 9223372036.854775807)); // fraction-digit: 9
120 decimal64ValueRange.add(new Range(-922337203.6854775808, 922337203.6854775807)); // fraction-digit: 10
121 decimal64ValueRange.add(new Range(-92233720.36854775808, 92233720.36854775807)); // fraction-digit: 11
122 decimal64ValueRange.add(new Range(-9223372.036854775808, 9223372.036854775807)); // fraction-digit: 12
123 decimal64ValueRange.add(new Range(-922337.2036854775808, 922337.2036854775807)); // fraction-digit: 13
124 decimal64ValueRange.add(new Range(-92233.72036854775808, 92233.72036854775807)); // fraction-digit: 14
125 decimal64ValueRange.add(new Range(-9223.372036854775808, 9223.372036854775807)); // fraction-digit: 15
126 decimal64ValueRange.add(new Range(-922.3372036854775808, 922.3372036854775807)); // fraction-digit: 16
127 decimal64ValueRange.add(new Range(-92.23372036854775808, 92.23372036854775807)); // fraction-digit: 17
128 decimal64ValueRange.add(new Range(-9.223372036854775808, 9.223372036854775807)); // fraction-digit: 18
129 }
130 return decimal64ValueRange;
131 }
132
133 /**
134 * Checks given decimal64 value is in the specific range based on given fraction-digit.
135 *
136 * @param value decimal64 value
137 * @param fractionDigit fraction-digits
138 * @return success when it is in specific range otherwise false
139 */
140 public static boolean isValidDecimal64(BigDecimal value, int fractionDigit) {
141 if (!((fractionDigit >= 1) && (fractionDigit <= 18))) {
142 return false;
143 }
144
145 // ArrayList index starts from 0.
146 Range range = getDecimal64ValueRange().get(fractionDigit - 1);
147 if ((value.doubleValue() >= range.min) && (value.doubleValue() <= range.max)) {
148 return true;
149 }
150 return false;
151 }
152
153 /**
154 * Retrieve range based on fraction-digits.
155 *
156 * @param fractionDigit fraction-digits
157 * @return range
158 * @throws DataModelException a violation of data model rules
159 */
160 public static Range getRange(int fractionDigit) throws DataModelException {
161 if (!((fractionDigit >= 1) && (fractionDigit <= 18))) {
162 throw new DataModelException("YANG file error : given fraction-digit is not in its range (1..18).");
163 }
164
165 return getDecimal64ValueRange().get(fractionDigit - 1);
166 }
167}