001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.io;
027:
028: import java.io.*;
029:
030: import java.math.*;
031:
032: /**
033: *
034: *
035: * @author $author$
036: * @version $Revision: 1.14 $
037: */
038: public class UnsignedInteger64 extends Number implements Serializable,
039: Comparable {
040: final static long serialVersionUID = 200;
041:
042: /** */
043: public final static BigInteger MAX_VALUE = new BigInteger(
044: "18446744073709551615");
045:
046: /** */
047: public final static BigInteger MIN_VALUE = new BigInteger("0");
048: private BigInteger bigInt;
049:
050: /**
051: * Creates a new UnsignedInteger64 object.
052: *
053: * @param sval
054: *
055: * @throws NumberFormatException
056: */
057: public UnsignedInteger64(String sval) throws NumberFormatException {
058: bigInt = new BigInteger(sval);
059:
060: if ((bigInt.compareTo(MIN_VALUE) < 0)
061: || (bigInt.compareTo(MAX_VALUE) > 0)) {
062: throw new NumberFormatException();
063: }
064: }
065:
066: /**
067: * Creates a new UnsignedInteger64 object.
068: *
069: * @param bval
070: *
071: * @throws NumberFormatException
072: */
073: public UnsignedInteger64(byte[] bval) throws NumberFormatException {
074: bigInt = new BigInteger(bval);
075:
076: if ((bigInt.compareTo(MIN_VALUE) < 0)
077: || (bigInt.compareTo(MAX_VALUE) > 0)) {
078: throw new NumberFormatException();
079: }
080: }
081:
082: /**
083: * Creates a new UnsignedInteger64 object.
084: *
085: * @param input
086: *
087: * @throws NumberFormatException
088: */
089: public UnsignedInteger64(BigInteger input) {
090: bigInt = new BigInteger(input.toString());
091:
092: if ((bigInt.compareTo(MIN_VALUE) < 0)
093: || (bigInt.compareTo(MAX_VALUE) > 0)) {
094: throw new NumberFormatException();
095: }
096: }
097:
098: /**
099: *
100: *
101: * @param o
102: *
103: * @return
104: */
105: public boolean equals(Object o) {
106: try {
107: UnsignedInteger64 u = (UnsignedInteger64) o;
108:
109: return u.bigInt.equals(this .bigInt);
110: } catch (ClassCastException ce) {
111: // This was not an UnsignedInt64, so equals should fail.
112: return false;
113: }
114: }
115:
116: /**
117: *
118: *
119: * @return
120: */
121: public BigInteger bigIntValue() {
122: return bigInt;
123: }
124:
125: /**
126: *
127: *
128: * @return
129: */
130: public int intValue() {
131: return bigInt.intValue();
132: }
133:
134: /**
135: *
136: *
137: * @return
138: */
139: public long longValue() {
140: return bigInt.longValue();
141: }
142:
143: /**
144: *
145: *
146: * @return
147: */
148: public double doubleValue() {
149: return bigInt.doubleValue();
150: }
151:
152: /**
153: *
154: *
155: * @return
156: */
157: public float floatValue() {
158: return bigInt.floatValue();
159: }
160:
161: /**
162: *
163: *
164: * @param val
165: *
166: * @return
167: */
168: public int compareTo(Object val) {
169: return bigInt.compareTo(((UnsignedInteger64) val).bigInt);
170: }
171:
172: /**
173: *
174: *
175: * @return
176: */
177: public String toString() {
178: return bigInt.toString();
179: }
180:
181: /**
182: *
183: *
184: * @return
185: */
186: public int hashCode() {
187: return bigInt.hashCode();
188: }
189:
190: /**
191: *
192: *
193: * @param x
194: * @param y
195: *
196: * @return
197: */
198: public static UnsignedInteger64 add(UnsignedInteger64 x,
199: UnsignedInteger64 y) {
200: return new UnsignedInteger64(x.bigInt.add(y.bigInt));
201: }
202:
203: /**
204: *
205: *
206: * @param x
207: * @param y
208: *
209: * @return
210: */
211: public static UnsignedInteger64 add(UnsignedInteger64 x, int y) {
212: return new UnsignedInteger64(x.bigInt
213: .add(BigInteger.valueOf(y)));
214: }
215: }
|