blob: 70001aed86daa2afa1c1ba9fd56b4981328a68e1 [file] [log] [blame]
Vidyashree Ramaa2f73982016-04-12 23:33:33 +05301/*
2 * Copyright 2016 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 */
16package org.onosproject.yangutils.utils.builtindatatype;
17
18import java.math.BigInteger;
19import java.util.regex.Pattern;
20import org.onosproject.yangutils.datamodel.YangDataTypes;
21
22/**
23 * Handles the YANG's Uint16 data type processing.
24 *
25 * Uint64 represents integer values between 0 and 18446744073709551615, inclusively.
26 */
27public class YangUint64 implements YangBuiltInDataTypeInfo<YangUint64> {
28
29 /**
30 * YANG's min keyword.
31 */
32 private static final String MIN_KEYWORD = "min";
33
34 /**
35 * YANG's max keyword.
36 */
37 private static final String MAX_KEYWORD = "max";
38
39 /**
40 * YANG's Integer value pattern.
41 */
42 private static final Pattern NON_NEGATIVE_INTEGER_PATTERN = Pattern.compile("[0-9]+");
43
44 /**
45 * Valid minimum value of YANG's Uint64.
46 */
47 public static final BigInteger MIN_VALUE = BigInteger.valueOf(0);
48
49 /**
50 * Valid maximum value of YANG's Uint64.
51 */
52 public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
53
54 /**
55 * Value of the object.
56 */
57 private BigInteger value;
58
59 /**
60 * Creates an object with the value initialized with value represented in
61 * string.
62 *
63 * @param valueInString value of the object in string
64 */
65 YangUint64(String valueInString) {
66
67 if (valueInString.matches(MIN_KEYWORD)) {
68 value = MIN_VALUE;
69 } else if (valueInString.matches(MAX_KEYWORD)) {
70 value = MAX_VALUE;
71 } else if (NON_NEGATIVE_INTEGER_PATTERN.matcher(valueInString).matches()) {
72 value = new BigInteger(valueInString);
73 } else {
74 throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
75 }
76
77 if (value.compareTo(MIN_VALUE) < 0) {
78 throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
79 + MIN_VALUE + ".");
80 } else if (value.compareTo(MAX_VALUE) > 0) {
81 throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
82 + MAX_VALUE + ".");
83 }
84 }
85
86 /**
87 * Returns YANG's uint64 value.
88 *
89 * @return value of YANG's uint64
90 */
91 public BigInteger getValue() {
92 return value;
93 }
94
95 @Override
96 public int compareTo(YangUint64 another) {
97 return value.compareTo(another.value);
98 }
99
100 @Override
101 public YangDataTypes getYangType() {
102 return YangDataTypes.UINT64;
103 }
104
105}