Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 1 | /* |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | a2f7398 | 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 | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 16 | |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 17 | package org.onosproject.yangutils.datamodel.utils.builtindatatype; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 18 | |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 19 | import java.io.Serializable; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 20 | import java.math.BigInteger; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 21 | |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 22 | /** |
| 23 | * Handles the YANG's Uint16 data type processing. |
| 24 | * |
| 25 | * Uint64 represents integer values between 0 and 18446744073709551615, inclusively. |
| 26 | */ |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 27 | public class YangUint64 implements YangBuiltInDataTypeInfo<YangUint64>, Serializable { |
| 28 | |
| 29 | private static final long serialVersionUID = 8006201661L; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * YANG's min keyword. |
| 33 | */ |
| 34 | private static final String MIN_KEYWORD = "min"; |
| 35 | |
| 36 | /** |
| 37 | * YANG's max keyword. |
| 38 | */ |
| 39 | private static final String MAX_KEYWORD = "max"; |
| 40 | |
| 41 | /** |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 42 | * Valid minimum value of YANG's Uint64. |
| 43 | */ |
| 44 | public static final BigInteger MIN_VALUE = BigInteger.valueOf(0); |
| 45 | |
| 46 | /** |
| 47 | * Valid maximum value of YANG's Uint64. |
| 48 | */ |
| 49 | public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); |
| 50 | |
| 51 | /** |
| 52 | * Value of the object. |
| 53 | */ |
| 54 | private BigInteger value; |
| 55 | |
| 56 | /** |
| 57 | * Creates an object with the value initialized with value represented in |
| 58 | * string. |
| 59 | * |
| 60 | * @param valueInString value of the object in string |
| 61 | */ |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 62 | public YangUint64(String valueInString) { |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 63 | |
| 64 | if (valueInString.matches(MIN_KEYWORD)) { |
| 65 | value = MIN_VALUE; |
| 66 | } else if (valueInString.matches(MAX_KEYWORD)) { |
| 67 | value = MAX_VALUE; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 68 | } else { |
Mahesh Poojary S | 98675a9 | 2016-07-27 15:36:42 +0530 | [diff] [blame^] | 69 | try { |
| 70 | value = new BigInteger(valueInString); |
| 71 | } catch (Exception e) { |
| 72 | throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " + |
| 73 | "uint64."); |
| 74 | } |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 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 | } |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 104 | } |