blob: e356055462c648b62d63b388e45a953cc7fbe5bc [file] [log] [blame]
Vidyashree Ramabc9611f2016-04-12 23:33:33 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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 */
16package org.onosproject.yangutils.utils.builtindatatype;
17
18import org.onosproject.yangutils.datamodel.YangDataTypes;
19
20/**
21 * Handles the YANG's Uint32 data type processing.
22 *
23 * Uint32 represents integer values between 0 and 4294967295, inclusively.
24 */
25public class YangUint32 implements YangBuiltInDataTypeInfo<YangUint32> {
26
27 private static final String MIN_KEYWORD = "min";
28 private static final String MAX_KEYWORD = "max";
29
30 /**
31 * Valid minimum value of YANG's Uint32.
32 */
33 public static final long MIN_VALUE = 0;
34
35 /**
36 * Valid maximum value of YANG's Uint32.
37 */
38 public static final long MAX_VALUE = 4294967295L;
39
40 /**
41 * Value of the object.
42 */
43 private long value;
44
45 /**
46 * Creates an object with the value initialized with value represented in
47 * string.
48 *
49 * @param valueInString value of the object in string
50 */
51 YangUint32(String valueInString) {
52
53 if (valueInString.matches(MIN_KEYWORD)) {
54 value = MIN_VALUE;
55 } else if (valueInString.matches(MAX_KEYWORD)) {
56 value = MAX_VALUE;
57 } else {
58 try {
59 value = Long.parseLong(valueInString);
60 } catch (Exception e) {
61 throw new DataTypeException("YANG file error : " + valueInString + " is not valid.");
62 }
63 }
64
65 if (value < MIN_VALUE) {
66 throw new DataTypeException("YANG file error : " + valueInString + " is lesser than minimum value "
67 + MIN_VALUE + ".");
68 } else if (value > MAX_VALUE) {
69 throw new DataTypeException("YANG file error : " + valueInString + " is greater than maximum value "
70 + MAX_VALUE + ".");
71 }
72 }
73
74 /**
75 * Returns YANG's uint32 value.
76 *
77 * @return value of YANG's uint32
78 */
79 public long getValue() {
80 return value;
81 }
82
83 @Override
84 public int compareTo(YangUint32 another) {
85 return Long.compare(value, another.value);
86 }
87
88 @Override
89 public YangDataTypes getYangType() {
90 return YangDataTypes.UINT32;
91 }
92
93}