KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > util > UnsignedInteger32


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.util;
21
22 /**
23  * This class provides a 32bit unsigned integer.
24  *
25  * @author Lee David Painter
26  */

27 public class UnsignedInteger32 {
28
29   /** The maximum value of a 32bit unsigned integer */
30   public final static long MAX_VALUE = 0xffffffffL;
31
32   /** The minimum value of a 32bit unsigned integer */
33   public final static long MIN_VALUE = 0;
34   private Long JavaDoc value;
35
36   /**
37    * Creates a new UnsignedInteger32 object.
38    *
39    * @param a
40    *
41    * @throws NumberFormatException
42    */

43   public UnsignedInteger32(long a) {
44     if ( (a < MIN_VALUE) || (a > MAX_VALUE)) {
45       throw new NumberFormatException JavaDoc();
46     }
47
48     value = new Long JavaDoc(a);
49   }
50
51   /**
52    * Creates a new UnsignedInteger32 object.
53    *
54    * @param a
55    *
56    * @throws NumberFormatException
57    */

58   public UnsignedInteger32(String JavaDoc a) throws NumberFormatException JavaDoc {
59
60     long longValue = Long.parseLong(a);
61
62     if ( (longValue < MIN_VALUE) || (longValue > MAX_VALUE)) {
63       throw new NumberFormatException JavaDoc();
64     }
65
66     value = new Long JavaDoc(longValue);
67   }
68
69   /**
70    *
71    *
72    * @return
73    */

74   public int intValue() {
75     return (int) value.longValue();
76   }
77
78   /**
79    * Returns the long value of this unsigned integer.
80    *
81    * @return
82    */

83   public long longValue() {
84     return value.longValue();
85   }
86
87
88   /**
89    * Returns a String representation of the unsigned integer.
90    *
91    * @return
92    */

93   public String JavaDoc toString() {
94     return value.toString();
95   }
96
97   /**
98    * Returns the objects hash code.
99    *
100    * @return
101    */

102   public int hashCode() {
103     return value.hashCode();
104   }
105
106   /**
107    * Compares an object.
108    *
109    * @param o
110    *
111    * @return
112    */

113   public boolean equals(Object JavaDoc o) {
114     if (! (o instanceof UnsignedInteger32)) {
115       return false;
116     }
117
118     return ( ( (UnsignedInteger32) o).value.equals(this.value));
119   }
120
121   /**
122    * Add two unsigned integers together.
123    *
124    * @param x
125    * @param y
126    *
127    * @return
128    */

129   public static UnsignedInteger32 add(UnsignedInteger32 x, UnsignedInteger32 y) {
130     return new UnsignedInteger32(x.longValue() + y.longValue());
131   }
132
133   /**
134    * Add an int to an unsigned integer.
135    *
136    * @param x
137    * @param y
138    *
139    * @return
140    */

141   public static UnsignedInteger32 add(UnsignedInteger32 x, int y) {
142     return new UnsignedInteger32(x.longValue() + y);
143   }
144 }
145
Popular Tags