blob: f9055e850079b9400e2ce22070cda4a133126963 [file] [log] [blame]
Vidyashree Ramaa2f73982016-04-12 23:33:33 +05301/*
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +05302 * Copyright 2016-present Open Networking Laboratory
Vidyashree Ramaa2f73982016-04-12 23:33:33 +05303 *
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 Agrawalcfa1c412016-05-03 00:41:48 +053016
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053017package org.onosproject.yangutils.utils.builtindatatype;
18
19import 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 */
27public 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 Agrawalcfa1c412016-05-03 00:41:48 +053070 throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " +
71 "int16.");
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053072 }
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}