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