blob: 287d28417d3d3628b5991c886922a751f66eb7dc [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 Uint32 data type processing.
23 *
24 * Uint32 represents integer values between 0 and 4294967295, inclusively.
25 */
Bharat saraswal96dfef02016-06-16 00:29:12 +053026public class YangUint32 implements YangBuiltInDataTypeInfo<YangUint32>, Serializable {
27
28 private static final long serialVersionUID = 8006201662L;
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053029
30 private static final String MIN_KEYWORD = "min";
31 private static final String MAX_KEYWORD = "max";
32
33 /**
34 * Valid minimum value of YANG's Uint32.
35 */
36 public static final long MIN_VALUE = 0;
37
38 /**
39 * Valid maximum value of YANG's Uint32.
40 */
41 public static final long MAX_VALUE = 4294967295L;
42
43 /**
44 * Value of the object.
45 */
46 private long value;
47
48 /**
49 * Creates an object with the value initialized with value represented in
50 * string.
51 *
52 * @param valueInString value of the object in string
53 */
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053054 public YangUint32(String valueInString) {
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053055
56 if (valueInString.matches(MIN_KEYWORD)) {
57 value = MIN_VALUE;
58 } else if (valueInString.matches(MAX_KEYWORD)) {
59 value = MAX_VALUE;
60 } else {
61 try {
62 value = Long.parseLong(valueInString);
63 } catch (Exception e) {
Gaurav Agrawalcfa1c412016-05-03 00:41:48 +053064 throw new DataTypeException("YANG file error : Input value \"" + valueInString + "\" is not a valid " +
65 "uint32.");
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053066 }
67 }
68
69 if (value < MIN_VALUE) {
70 throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
71 + MIN_VALUE + ".");
72 } else if (value > MAX_VALUE) {
73 throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
74 + MAX_VALUE + ".");
75 }
76 }
77
78 /**
79 * Returns YANG's uint32 value.
80 *
81 * @return value of YANG's uint32
82 */
83 public long getValue() {
84 return value;
85 }
86
87 @Override
88 public int compareTo(YangUint32 another) {
89 return Long.compare(value, another.value);
90 }
91
92 @Override
93 public YangDataTypes getYangType() {
94 return YangDataTypes.UINT32;
95 }
Vidyashree Ramaa2f73982016-04-12 23:33:33 +053096}