blob: f2f460414b27d0f107d48152351fcbd62e02e111 [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;
20
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053021/**
22 * Handles the YANG's Uint8 data type processing.
23 *
24 * Uint8 represents integer values between 0 and 255, inclusively.
25 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053026public class YangUint8 implements YangBuiltInDataTypeInfo<YangUint8>, Serializable {
27
28 private static final long serialVersionUID = 8006201660L;
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 Uint8.
42 */
43 public static final short MIN_VALUE = 0;
44
45 /**
46 * Valid maximum value of YANG's Uint8.
47 */
48 public static final short MAX_VALUE = 255;
49
50 /**
51 * Value of the object.
52 */
53 private 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 */
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053061 public YangUint8(String valueInString) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053062
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 "uint8.");
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053073 }
74 }
75
76 if (value < MIN_VALUE) {
77 throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
78 + MIN_VALUE + ".");
79 } else if (value > MAX_VALUE) {
80 throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
81 + MAX_VALUE + ".");
82 }
83 }
84
85 /**
86 * Returns YANG's uint8 value.
87 *
88 * @return value of YANG's uint8
89 */
90 public short getValue() {
91 return value;
92 }
93
94 @Override
95 public int compareTo(YangUint8 another) {
96 return Short.compare(value, another.value);
97 }
98
99 @Override
100 public YangDataTypes getYangType() {
101 return YangDataTypes.UINT8;
102 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +0530103}