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