blob: c005b8d88dafb944af08cf6073daebced4a7056c [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
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053017package org.onosproject.yangutils.datamodel.utils.builtindatatype;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053018
Bharat saraswal96dfef02016-06-16 00:29:12 +053019import java.io.Serializable;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053020
Bharat saraswal96dfef02016-06-16 00:29:12 +053021import org.onosproject.yangutils.datamodel.YangDataTypes;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053022
23/**
24 * Handles the YANG's int16 data type processing.
25 *
26 * int16 represents integer values between -32768 and 32767, inclusively.
27 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053028public class YangInt16 implements YangBuiltInDataTypeInfo<YangInt16>, Serializable {
29
30 private static final long serialVersionUID = 8006201667L;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053031
32 /**
33 * YANG's min keyword.
34 */
35 private static final String MIN_KEYWORD = "min";
36
37 /**
38 * YANG's max keyword.
39 */
40 private static final String MAX_KEYWORD = "max";
41
42 /**
43 * Valid minimum value of YANG's int16.
44 */
45 public static final short MIN_VALUE = -32768;
46
47 /**
48 * Valid maximum value of YANG's int16.
49 */
50 public static final short MAX_VALUE = 32767;
51
52 /**
53 * The value of YANG's int16.
54 */
55 private final short value;
56
57 /**
58 * Creates an object with the value initialized with value represented in
59 * string.
60 *
61 * @param valueInString value of the object in string
62 */
63 public YangInt16(String valueInString) {
64
65 if (valueInString.matches(MIN_KEYWORD)) {
66 value = MIN_VALUE;
67 } else if (valueInString.matches(MAX_KEYWORD)) {
68 value = MAX_VALUE;
69 } else {
70 try {
71 value = Short.parseShort(valueInString);
72 } catch (Exception e) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053073 throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " +
74 "int16.");
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053075 }
76 }
77 }
78
79 /**
80 * Returns YANG's int16 value.
81 *
82 * @return value of YANG's int16
83 */
84 public short getValue() {
85 return value;
86 }
87
88 @Override
89 public int compareTo(YangInt16 anotherYangInt16) {
90 return Short.compare(value, anotherYangInt16.value);
91 }
92
93 @Override
94 public YangDataTypes getYangType() {
95 return YangDataTypes.INT16;
96 }
97
98}