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