KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.BigInteger JavaDoc;
23
24 /**
25  * This class provides a 64bit unsigned integer.
26  *
27  * @author Lee David Painter
28  */

29 public class UnsignedInteger64 {
30
31   /** */
32   public final static BigInteger JavaDoc MAX_VALUE = new BigInteger JavaDoc(
33       "18446744073709551615");
34
35   /** */
36   public final static BigInteger JavaDoc MIN_VALUE = new BigInteger JavaDoc("0");
37   private BigInteger JavaDoc bigInt;
38
39   /**
40    * Creates a new UnsignedInteger64 object.
41    *
42    * @param sval
43    *
44    * @throws NumberFormatException
45    */

46   public UnsignedInteger64(String JavaDoc sval) throws NumberFormatException JavaDoc {
47     bigInt = new BigInteger JavaDoc(sval);
48
49     if ( (bigInt.compareTo(MIN_VALUE) < 0)
50         || (bigInt.compareTo(MAX_VALUE) > 0)) {
51       throw new NumberFormatException JavaDoc();
52     }
53   }
54
55   /**
56    * Creates a new UnsignedInteger64 object.
57    *
58    * @param bval
59    *
60    * @throws NumberFormatException
61    */

62   public UnsignedInteger64(byte[] bval) throws NumberFormatException JavaDoc {
63     bigInt = new BigInteger JavaDoc(bval);
64
65     if ( (bigInt.compareTo(MIN_VALUE) < 0)
66         || (bigInt.compareTo(MAX_VALUE) > 0)) {
67       throw new NumberFormatException JavaDoc();
68     }
69   }
70
71   public UnsignedInteger64(long value) {
72     bigInt = BigInteger.valueOf(value);
73   }
74
75   /**
76    * Creates a new UnsignedInteger64 object.
77    *
78    * @param input
79    *
80    * @throws NumberFormatException
81    */

82   public UnsignedInteger64(BigInteger JavaDoc input) {
83     bigInt = new BigInteger JavaDoc(input.toString());
84
85     if ( (bigInt.compareTo(MIN_VALUE) < 0)
86         || (bigInt.compareTo(MAX_VALUE) > 0)) {
87       throw new NumberFormatException JavaDoc();
88     }
89   }
90
91   /**
92    *
93    * Compares this unsigned integer to an object.
94    *
95    * @param o
96    *
97    * @return
98    */

99   public boolean equals(Object JavaDoc o) {
100     try {
101       UnsignedInteger64 u = (UnsignedInteger64) o;
102
103       return u.bigInt.equals(this.bigInt);
104     }
105     catch (ClassCastException JavaDoc ce) {
106       // This was not an UnsignedInt64, so equals should fail.
107
return false;
108     }
109   }
110
111   /**
112    * Return a BigInteger value of the unsigned integer.
113    *
114    * @return
115    */

116   public BigInteger JavaDoc bigIntValue() {
117     return bigInt;
118   }
119
120   /**
121    * Return a long value of the unsigned integer.
122    *
123    * @return
124    */

125   public long longValue() {
126     return bigInt.longValue();
127   }
128
129
130   /**
131    * Return a String representation of the unsigned integer
132    *
133    * @return
134    */

135   public String JavaDoc toString() {
136     return bigInt.toString(10);
137   }
138
139   /**
140    * Return the objects hash code.
141    *
142    * @return
143    */

144   public int hashCode() {
145     return bigInt.hashCode();
146   }
147
148   /**
149    * Add an unsigned integer to another unsigned integer.
150    *
151    * @param x
152    * @param y
153    *
154    * @return
155    */

156   public static UnsignedInteger64 add(UnsignedInteger64 x, UnsignedInteger64 y) {
157     return new UnsignedInteger64(x.bigInt.add(y.bigInt));
158   }
159
160   /**
161    * Add an unsigned integer to an int.
162    *
163    * @param x
164    * @param y
165    *
166    * @return
167    */

168   public static UnsignedInteger64 add(UnsignedInteger64 x, int y) {
169     return new UnsignedInteger64(x.bigInt.add(BigInteger.valueOf(y)));
170   }
171
172   /**
173    * Returns a byte array encoded with the unsigned integer.
174    * @return
175    */

176   public byte[] toByteArray() {
177     byte[] raw = new byte[8];
178     byte[] bi = bigIntValue().toByteArray();
179     System.arraycopy(bi, 0, raw, raw.length - bi.length, bi.length);
180     return raw;
181   }
182 }
183
Popular Tags