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