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; |
| 20 | |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 21 | /** |
| 22 | * Handles the YANG's Uint32 data type processing. |
| 23 | * |
| 24 | * Uint32 represents integer values between 0 and 4294967295, inclusively. |
| 25 | */ |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 26 | public class YangUint32 implements YangBuiltInDataTypeInfo<YangUint32>, Serializable { |
| 27 | |
| 28 | private static final long serialVersionUID = 8006201662L; |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 29 | |
| 30 | private static final String MIN_KEYWORD = "min"; |
| 31 | private static final String MAX_KEYWORD = "max"; |
| 32 | |
| 33 | /** |
| 34 | * Valid minimum value of YANG's Uint32. |
| 35 | */ |
| 36 | public static final long MIN_VALUE = 0; |
| 37 | |
| 38 | /** |
| 39 | * Valid maximum value of YANG's Uint32. |
| 40 | */ |
| 41 | public static final long MAX_VALUE = 4294967295L; |
| 42 | |
| 43 | /** |
| 44 | * Value of the object. |
| 45 | */ |
| 46 | private long value; |
| 47 | |
| 48 | /** |
| 49 | * Creates an object with the value initialized with value represented in |
| 50 | * string. |
| 51 | * |
| 52 | * @param valueInString value of the object in string |
| 53 | */ |
Gaurav Agrawal | 72cd1b7 | 2016-06-30 13:28:14 +0530 | [diff] [blame] | 54 | public YangUint32(String valueInString) { |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 55 | |
| 56 | if (valueInString.matches(MIN_KEYWORD)) { |
| 57 | value = MIN_VALUE; |
| 58 | } else if (valueInString.matches(MAX_KEYWORD)) { |
| 59 | value = MAX_VALUE; |
| 60 | } else { |
| 61 | try { |
| 62 | value = Long.parseLong(valueInString); |
| 63 | } catch (Exception e) { |
Gaurav Agrawal | cfa1c41 | 2016-05-03 00:41:48 +0530 | [diff] [blame] | 64 | throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " + |
| 65 | "uint32."); |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | if (value < MIN_VALUE) { |
| 70 | throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value " |
| 71 | + MIN_VALUE + "."); |
| 72 | } else if (value > MAX_VALUE) { |
| 73 | throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value " |
| 74 | + MAX_VALUE + "."); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns YANG's uint32 value. |
| 80 | * |
| 81 | * @return value of YANG's uint32 |
| 82 | */ |
| 83 | public long getValue() { |
| 84 | return value; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int compareTo(YangUint32 another) { |
| 89 | return Long.compare(value, another.value); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public YangDataTypes getYangType() { |
| 94 | return YangDataTypes.UINT32; |
| 95 | } |
Vidyashree Rama | a2f7398 | 2016-04-12 23:33:33 +0530 | [diff] [blame] | 96 | } |