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