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