blob: 455e34346126916ff0f78766c65531469194f2e3 [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 int8 data type processing.
23 *
24 * int8 represents integer values between -128 and 127, inclusively.
25 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053026public class YangInt8 implements YangBuiltInDataTypeInfo<YangInt8>, Serializable {
27
28 private static final long serialVersionUID = 8006201664L;
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 int8.
42 */
43 public static final byte MIN_VALUE = -128;
44
45 /**
46 * Valid maximum value of YANG's int8.
47 */
48 public static final byte MAX_VALUE = 127;
49
50 /**
51 * The value of YANG's int8.
52 */
53 private final byte 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 YangInt8(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 = Byte.parseByte(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 "int8.");
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053073 }
74 }
75 }
76
77 /**
78 * Returns YANG's int8 value.
79 *
80 * @return value of YANG's int8
81 */
82 public byte getValue() {
83 return value;
84 }
85
86 @Override
87 public int compareTo(YangInt8 anotherYangInt8) {
88 return Byte.compare(value, anotherYangInt8.value);
89 }
90
91 @Override
92 public YangDataTypes getYangType() {
93 return YangDataTypes.INT8;
94 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053095}