blob: 49a51536be95c03d14541fb413869f0b52690eb6 [file] [log] [blame]
Vidyashree Ramaa2f73982016-04-12 23:33:33 +05301/*
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +05302 * Copyright 2016-present Open Networking Laboratory
Vidyashree Ramaa2f73982016-04-12 23:33:33 +05303 *
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 */
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053016
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053017package org.onosproject.yangutils.datamodel.utils.builtindatatype;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053018
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053020import java.math.BigInteger;
21import java.util.regex.Pattern;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053023/**
24 * Handles the YANG's Uint16 data type processing.
25 *
26 * Uint64 represents integer values between 0 and 18446744073709551615, inclusively.
27 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053028public class YangUint64 implements YangBuiltInDataTypeInfo<YangUint64>, Serializable {
29
30 private static final long serialVersionUID = 8006201661L;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053031
32 /**
33 * YANG's min keyword.
34 */
35 private static final String MIN_KEYWORD = "min";
36
37 /**
38 * YANG's max keyword.
39 */
40 private static final String MAX_KEYWORD = "max";
41
42 /**
43 * YANG's Integer value pattern.
44 */
45 private static final Pattern NON_NEGATIVE_INTEGER_PATTERN = Pattern.compile("[0-9]+");
46
47 /**
48 * Valid minimum value of YANG's Uint64.
49 */
50 public static final BigInteger MIN_VALUE = BigInteger.valueOf(0);
51
52 /**
53 * Valid maximum value of YANG's Uint64.
54 */
55 public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
56
57 /**
58 * Value of the object.
59 */
60 private BigInteger value;
61
62 /**
63 * Creates an object with the value initialized with value represented in
64 * string.
65 *
66 * @param valueInString value of the object in string
67 */
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053068 public YangUint64(String valueInString) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053069
70 if (valueInString.matches(MIN_KEYWORD)) {
71 value = MIN_VALUE;
72 } else if (valueInString.matches(MAX_KEYWORD)) {
73 value = MAX_VALUE;
74 } else if (NON_NEGATIVE_INTEGER_PATTERN.matcher(valueInString).matches()) {
75 value = new BigInteger(valueInString);
76 } else {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053077 throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " +
78 "uint64.");
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053079 }
80
81 if (value.compareTo(MIN_VALUE) < 0) {
82 throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
83 + MIN_VALUE + ".");
84 } else if (value.compareTo(MAX_VALUE) > 0) {
85 throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
86 + MAX_VALUE + ".");
87 }
88 }
89
90 /**
91 * Returns YANG's uint64 value.
92 *
93 * @return value of YANG's uint64
94 */
95 public BigInteger getValue() {
96 return value;
97 }
98
99 @Override
100 public int compareTo(YangUint64 another) {
101 return value.compareTo(another.value);
102 }
103
104 @Override
105 public YangDataTypes getYangType() {
106 return YangDataTypes.UINT64;
107 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530108}