You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by ch...@apache.org on 2006/10/03 07:50:42 UTC
svn commit: r452313 -
/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/UnsignedLong.java
Author: chinthaka
Date: Mon Oct 2 22:50:42 2006
New Revision: 452313
URL: http://svn.apache.org/viewvc?view=rev&rev=452313
Log:
interesting piece of code. This was throwing NPEs and contained a return statement inside finally.
Anyway, this lead to me another question. What if the passed value is a null? According to javadoc of the compare method, if the method can not be compared, then one should throw a ClasscastException. I had no option than to throw a ClasscastException even when the passed object is null.
Modified:
webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/UnsignedLong.java
Modified: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/UnsignedLong.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/UnsignedLong.java?view=diff&rev=452313&r1=452312&r2=452313
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/UnsignedLong.java (original)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/UnsignedLong.java Mon Oct 2 22:50:42 2006
@@ -19,6 +19,7 @@
// All operations behave as if BigIntegers were represented in two's-complement notation.
// In its place, consider using primitive type long (which is already the right size) to hold the data.
// This class can hide the fact that the data is stored in a signed entity, by careful implementation of the class' methods.
+
import java.math.BigInteger;
/**
@@ -29,8 +30,8 @@
public class UnsignedLong extends java.lang.Number implements Comparable {
private static final long serialVersionUID = -5919942584284897583L;
-
- protected BigInteger lValue = BigInteger.ZERO;
+
+ protected BigInteger lValue = BigInteger.ZERO;
private static BigInteger MAX = new BigInteger("18446744073709551615"); // max unsigned long
public UnsignedLong() {
@@ -45,34 +46,33 @@
}
public UnsignedLong(long lValue) throws IllegalArgumentException {
- // new UnsignedLong( 0xffffffffffffffffL )
- // should not throw any Exception because, as an UnsignedLong, it is in range and nonnegative.
+ // new UnsignedLong( 0xffffffffffffffffL )
+ // should not throw any Exception because, as an UnsignedLong, it is in range and nonnegative.
setValue(BigInteger.valueOf(lValue));
}
public UnsignedLong(String stValue) throws NumberFormatException {
- // If stValue starts with a minus sign, that will be acceptable to the BigInteger constructor,
- // but it is not acceptable to us.
- // Once encoded into binary, it is too late to detect that the client intended a negative integer.
- // That detection must be performed here.
- try {
- if (stValue.charAt(0) == '\u002d')
- {
- throw new NumberFormatException("A String that starts with a minus sign is not a valid representation of an UnsignedLong.");
- }
- setValue(new BigInteger(stValue));
- }
-
- catch ( NumberFormatException numberFormatException ) {
- throw numberFormatException;
- }
-
- catch ( IndexOutOfBoundsException indexOutOfBoundsException ) {
- // This could happen if stValue is empty when we attempt to detect a minus sign.
- // From the client's point of view, the empty String should cause a NumberFormatException.
- throw new NumberFormatException("An empty string is not a valid representation of an UnsignedLong.");
- }
+ // If stValue starts with a minus sign, that will be acceptable to the BigInteger constructor,
+ // but it is not acceptable to us.
+ // Once encoded into binary, it is too late to detect that the client intended a negative integer.
+ // That detection must be performed here.
+ try {
+ if (stValue.charAt(0) == '\u002d') {
+ throw new NumberFormatException("A String that starts with a minus sign is not a valid representation of an UnsignedLong.");
+ }
+ setValue(new BigInteger(stValue));
+ }
+
+ catch (NumberFormatException numberFormatException) {
+ throw numberFormatException;
+ }
+
+ catch (IndexOutOfBoundsException indexOutOfBoundsException) {
+ // This could happen if stValue is empty when we attempt to detect a minus sign.
+ // From the client's point of view, the empty String should cause a NumberFormatException.
+ throw new NumberFormatException("An empty string is not a valid representation of an UnsignedLong.");
+ }
}
@@ -87,14 +87,14 @@
public static boolean isValid(BigInteger value) {
- // Converts this BigInteger to a long.
- // This conversion is analogous to a narrowing primitive conversion from long to int as defined in the Java Language Specification:
- // if this BigInteger is too big to fit in a long, only the low-order 64 bits are returned.
- // Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.
- long unsignedLongValue = value.longValue();
+ // Converts this BigInteger to a long.
+ // This conversion is analogous to a narrowing primitive conversion from long to int as defined in the Java Language Specification:
+ // if this BigInteger is too big to fit in a long, only the low-order 64 bits are returned.
+ // Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.
+ long unsignedLongValue = value.longValue();
return !(compare(unsignedLongValue, BigInteger.ZERO.longValue()) < 0 || // less than zero
- compare(unsignedLongValue, MAX.longValue()) > 0);
+ compare(unsignedLongValue, MAX.longValue()) > 0);
}
public String toString() {
@@ -153,87 +153,58 @@
}
/**
- * @return the value 0 if the argument is an UnsignedLong numerically equal to this UnsignedLong; a value less than 0 if the argument is an UnsignedLong numerically greater than this UnsignedLong; and a value greater than 0 if the argument is an UnsignedLong numerically less than this UnsignedLong.
+ * @return the value 0 if the argument is an UnsignedLong numerically equal to this UnsignedLong; a value less than 0 if the argument is an UnsignedLong numerically greater than this UnsignedLong; and a value greater than 0 if the argument is an UnsignedLong numerically less than this UnsignedLong.
*/
- public int compareTo( Object o )
- throws ClassCastException, NullPointerException
- {
- int retVal = 0; // arbitrary default value in case of exception; required return value in case this object is equal to the specified object
-
- try {
- if ( o == null )
- {
- throw new NullPointerException( "Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException." );
- }
- if ( ! ( o instanceof UnsignedLong ) )
- {
- throw new ClassCastException( "The argument is not an UnsignedLong." );
- }
- // Only need to change retVal if this object is not equal to the specified object.
- retVal = compare( longValue(), ( (UnsignedLong) o ).longValue() );
- }
-
- catch ( NullPointerException nullPointerException ) {
- throw nullPointerException;
- }
-
- catch ( ClassCastException classCastException ) {
- throw classCastException;
- }
-
- finally {
- return retVal;
- }
+ public int compareTo(Object o) {
+ int retVal = 0; // arbitrary default value in case of exception; required return value in case this object is equal to the specified object
+
+ if (o == null || !(o instanceof UnsignedLong)) {
+ throw new ClassCastException("The argument is not an UnsignedLong.");
+ }
+ // Only need to change retVal if this object is not equal to the specified object.
+ retVal = compare(longValue(), ((UnsignedLong) o).longValue());
+
+ return retVal;
}
/**
- * @return the value 0 if thatLong is a long numerically equal to thisLong; a value less than 0 if thatLong is a long numerically greater than thisLong; and a value greater than 0 if thatLong is a long numerically less than thisLong (unsigned comparison).
+ * @return the value 0 if thatLong is a long numerically equal to thisLong; a value less than 0 if thatLong is a long numerically greater than thisLong; and a value greater than 0 if thatLong is a long numerically less than thisLong (unsigned comparison).
*/
- private static int compare( long thisLong, long thatLong )
- {
- // To avoid infinite recursion, do not instantiate UnsignedLong in this method, which may be called during UnsignedLong instantiation.
-
- if ( thisLong == thatLong )
- {
- return 0;
- }
- else
- {
- boolean isLessThan; // This is less than that.
-
- // Prepare the most significant half of the data for comparison.
- // The shift distance can be any number from 1 to 32 inclusive (1 is probably fastest).
- // A shift distance of one is sufficient to move the significant data off of the sign bit, allowing for a signed comparison of positive numbers (i.e. an unsigned comparison).
- long thisHalfLong = ( thisLong & 0xffffffff00000000L ) >>> 1;
- long thatHalfLong = ( thatLong & 0xffffffff00000000L ) >>> 1;
-
- if ( thisHalfLong == thatHalfLong )
- {
- // We must also look at the least significant half of the data.
-
- // Prepare the least significant half of the data for comparison.
- thisHalfLong = ( thisLong & 0x00000000ffffffffL );
- thatHalfLong = ( thatLong & 0x00000000ffffffffL );
-
- // We already know that the data is not equal.
- isLessThan = thisHalfLong < thatHalfLong;
- }
- else
- {
- // The answer is in the most significant half of the data.
- isLessThan = thisHalfLong < thatHalfLong;
- }
-
- if ( isLessThan )
- {
- return -1; // Returns a negative integer as this object is less than than the specified object.
- }
- else
- {
- return 1; // Returns a positive integer as this object is greater than than the specified object.
- }
- }
+ private static int compare(long thisLong, long thatLong) {
+ // To avoid infinite recursion, do not instantiate UnsignedLong in this method, which may be called during UnsignedLong instantiation.
+
+ if (thisLong == thatLong) {
+ return 0;
+ } else {
+ boolean isLessThan; // This is less than that.
+
+ // Prepare the most significant half of the data for comparison.
+ // The shift distance can be any number from 1 to 32 inclusive (1 is probably fastest).
+ // A shift distance of one is sufficient to move the significant data off of the sign bit, allowing for a signed comparison of positive numbers (i.e. an unsigned comparison).
+ long thisHalfLong = (thisLong & 0xffffffff00000000L) >>> 1;
+ long thatHalfLong = (thatLong & 0xffffffff00000000L) >>> 1;
+
+ if (thisHalfLong == thatHalfLong) {
+ // We must also look at the least significant half of the data.
+
+ // Prepare the least significant half of the data for comparison.
+ thisHalfLong = (thisLong & 0x00000000ffffffffL);
+ thatHalfLong = (thatLong & 0x00000000ffffffffL);
+
+ // We already know that the data is not equal.
+ isLessThan = thisHalfLong < thatHalfLong;
+ } else {
+ // The answer is in the most significant half of the data.
+ isLessThan = thisHalfLong < thatHalfLong;
+ }
+
+ if (isLessThan) {
+ return -1; // Returns a negative integer as this object is less than than the specified object.
+ } else {
+ return 1; // Returns a positive integer as this object is greater than than the specified object.
+ }
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org