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