View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2013
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, ebak@de.ibm.com
12   * 
13   * Flag       Date        Prog         Description
14   * -------------------------------------------------------------------------------
15   * 1565892    2006-10-09  ebak         Make SBLIM client JSR48 compliant
16   * 1737123    2007-06-15  ebak         Differences to JSR48 public review draft
17   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
18   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
19   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
20   * 2750520    2009-04-10  blaschke-oss Code cleanup from empty statement et al
21   * 2795671    2009-05-22  raman_arora  Add Type to Comparable <T>
22   * 2935258    2010-01-22  blaschke-oss Sync up javax.cim.* javadoc with JSR48 1.0.0
23   * 2973233    2010-03-19  blaschke-oss TCK: UnsignedIntegerNN.hashCode() not working
24   *    2719    2013-12-10  blaschke-oss TCK: CIM APIs should not generate NullPointerException
25   *    2716    2013-12-11  blaschke-oss Sync up javax.* javadoc with JSR48 1.0.0 Final V
26   */
27  
28  package org.sentrysoftware.wbem.javax.cim;
29  
30  /*-
31   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
32   * WBEM Java Client
33   * ჻჻჻჻჻჻
34   * Copyright (C) 2023 Sentry Software
35   * ჻჻჻჻჻჻
36   * This program is free software: you can redistribute it and/or modify
37   * it under the terms of the GNU Lesser General Public License as
38   * published by the Free Software Foundation, either version 3 of the
39   * License, or (at your option) any later version.
40   *
41   * This program is distributed in the hope that it will be useful,
42   * but WITHOUT ANY WARRANTY; without even the implied warranty of
43   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44   * GNU General Lesser Public License for more details.
45   *
46   * You should have received a copy of the GNU General Lesser Public
47   * License along with this program.  If not, see
48   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
49   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
50   */
51  
52  //Sync'd against JSR48 1.0.0 javadoc (version 1.7.0_03) on Tue Dec 10 07:02:50 EST 2013
53  /**
54   * This class represents an <code>UnsignedInteger32</code>. A
55   * <code>uint32</code> data type is defined by the (<a
56   * href=http://www.dmtf.org>DMTF</a>) CIM Infrastructure Specification (<a
57   * href=http://www.dmtf.org/standards/published_documents/DSP0004V2.3_final.pdf
58   * >DSP004</a>).
59   */
60  public class UnsignedInteger32 extends Number implements Comparable<UnsignedInteger32> {
61  
62  	private static final long serialVersionUID = -8861436527534071393L;
63  
64  	/**
65  	 * The maximum value for an <code>UnsignedInteger32</code>.
66  	 */
67  	public static final long MAX_VALUE = 4294967295l;
68  
69  	/**
70  	 * The minimum value for an <code>UnsignedInteger32</code>.
71  	 */
72  	public static final long MIN_VALUE = 0l;
73  
74  	private long iValue;
75  
76  	/**
77  	 * Sets the value of this integer object if it falls within the range of
78  	 * minimum and maximum values.
79  	 * 
80  	 * @param pValue
81  	 *            The integer.
82  	 * @throws NumberFormatException
83  	 *             If the integer is out of range.
84  	 */
85  	private void setValue(long pValue) throws NumberFormatException {
86  		if (pValue > MAX_VALUE || pValue < MIN_VALUE) {
87  			String msg = "uint32:" + pValue + " is out of range!";
88  			throw new NumberFormatException(msg);
89  		}
90  		this.iValue = pValue;
91  	}
92  
93  	/**
94  	 * Constructs an unsigned 32-bit integer object for the specified long
95  	 * value. Only the lower 32 bits are considered.
96  	 * 
97  	 * @param pValue
98  	 *            The long to be represented as an unsigned 32-bit integer.
99  	 * @throws NumberFormatException
100 	 *             If the number is out of range.
101 	 */
102 	public UnsignedInteger32(long pValue) throws NumberFormatException {
103 		setValue(pValue);
104 	}
105 
106 	/**
107 	 * Constructs an unsigned 32-bit integer object for the specified string.
108 	 * Only the lower 32 bits are considered.
109 	 * 
110 	 * @param pValue
111 	 *            The string to be represented as an unsigned 32-bit integer.
112 	 * @throws NumberFormatException
113 	 *             If the number is out of range.
114 	 * @throws IllegalArgumentException
115 	 *             If value is <code>null</code>.
116 	 */
117 	public UnsignedInteger32(String pValue) throws NumberFormatException {
118 		if (pValue == null) throw new IllegalArgumentException("String value cannot be null!");
119 		setValue(Long.parseLong(pValue));
120 	}
121 
122 	/**
123 	 * Compares this object with the specified object for order. Returns a
124 	 * negative integer, zero, or a positive integer as this object is less
125 	 * than, equal to, or greater than the specified object.
126 	 * 
127 	 * @param pOther
128 	 *            The Object to be compared.
129 	 * @return A negative integer, zero, or a positive integer as this object is
130 	 *         less than, equal to, or greater than the specified object.
131 	 * @throws ClassCastException
132 	 *             If the specified object's type prevents it from being
133 	 *             compared to this Object.
134 	 * @throws IllegalArgumentException
135 	 *             If value is <code>null</code>.
136 	 */
137 	public int compareTo(UnsignedInteger32 pOther) {
138 		if (pOther == null) throw new IllegalArgumentException(
139 				"Other UnsignedInteger32 cannot be null!");
140 		UnsignedInteger32 that = pOther;
141 		long d = this.iValue - that.iValue;
142 		if (d == 0) return 0;
143 		return d < 0 ? -1 : 1;
144 	}
145 
146 	/**
147 	 * Compares this object against the specified object. The result is
148 	 * <code>true</code> if and only if the argument is not null and is an
149 	 * UnsignedInteger32 object that represents the same value as this object.
150 	 * 
151 	 * @param pObj
152 	 *            The object to compare.
153 	 * @return <code>true</code> if the objects are the same; <code>false</code>
154 	 *         otherwise.
155 	 */
156 	@Override
157 	public boolean equals(Object pObj) {
158 		if (!(pObj instanceof UnsignedInteger32)) return false;
159 		return this.iValue == ((UnsignedInteger32) pObj).iValue;
160 	}
161 
162 	/**
163 	 * Returns the value of this unsigned integer object as a <code>byte</code>.
164 	 * 
165 	 * @return The <code>byte</code> value of this unsigned integer object.
166 	 */
167 	@Override
168 	public byte byteValue() {
169 		return (byte) this.iValue;
170 	}
171 
172 	/**
173 	 * Returns the value of this unsigned integer object as a
174 	 * <code>double</code>.
175 	 * 
176 	 * @return Value of this unsigned integer object as a <code>double</code>.
177 	 */
178 	@Override
179 	public double doubleValue() {
180 		return this.iValue;
181 	}
182 
183 	/**
184 	 * Returns the value of this unsigned integer object as a <code>float</code>
185 	 * .
186 	 * 
187 	 * @return Value of this unsigned integer object as a <code>float</code>.
188 	 */
189 	@Override
190 	public float floatValue() {
191 		return this.iValue;
192 	}
193 
194 	/**
195 	 * Computes the hash code for this unsigned integer object.
196 	 * 
197 	 * @return The integer representing the hash code for this unsigned integer
198 	 *         object.
199 	 */
200 	@Override
201 	public int hashCode() {
202 		return Long.valueOf(this.iValue).hashCode();
203 	}
204 
205 	/**
206 	 * Returns the value of this unsigned integer object as an <code>int</code>.
207 	 * 
208 	 * @return Value of this unsigned integer object as an <code>int</code>.
209 	 */
210 	@Override
211 	public int intValue() {
212 		return (int) this.iValue;
213 	}
214 
215 	/**
216 	 * Returns the value of this unsigned integer object as a <code>long</code>.
217 	 * 
218 	 * @return Value of this unsigned integer object as a <code>long</code>.
219 	 */
220 	@Override
221 	public long longValue() {
222 		return this.iValue;
223 	}
224 
225 	/**
226 	 * Returns the value of this unsigned integer object as a <code>short</code>
227 	 * .
228 	 * 
229 	 * @return Value of this unsigned integer object as a <code>short</code>.
230 	 */
231 	@Override
232 	public short shortValue() {
233 		return (short) this.iValue;
234 	}
235 
236 	/**
237 	 * Returns the text representation of this unsigned integer object.
238 	 * 
239 	 * @return Text representation of this unsigned integer.
240 	 */
241 	@Override
242 	public String toString() {
243 		return Long.toString(this.iValue);
244 	}
245 
246 }