axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r410890 - in /webservices/axis2/trunk/java/modules: adb-codegen/test/org/apache/axis2/schema/populate/derived/DerivedTypeUnsignedLongPopulateTest.java adb/src/org/apache/axis2/databinding/types/UnsignedLong.java
Date Thu, 01 Jun 2006 16:58:20 GMT
Author: dims
Date: Thu Jun  1 09:58:19 2006
New Revision: 410890

URL: http://svn.apache.org/viewvc?rev=410890&view=rev
Log:
Fix for AXIS2-492 - new UnsignedLong( 0xffffffffffffffffL ) should not throw a NumberFormatException
Fix for AXIS2-488 - UnsignedLong would be improved by implementing the Comparable interface


Modified:
    webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/derived/DerivedTypeUnsignedLongPopulateTest.java
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/UnsignedLong.java

Modified: webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/derived/DerivedTypeUnsignedLongPopulateTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/derived/DerivedTypeUnsignedLongPopulateTest.java?rev=410890&r1=410889&r2=410890&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/derived/DerivedTypeUnsignedLongPopulateTest.java
(original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/derived/DerivedTypeUnsignedLongPopulateTest.java
Thu Jun  1 09:58:19 2006
@@ -1,70 +1,72 @@
-package org.apache.axis2.schema.populate.derived;
-
-import org.apache.axis2.databinding.types.UnsignedLong;
-import org.apache.axis2.databinding.utils.ConverterUtil;
-
-/*
- * Copyright 2004,2005 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-public class DerivedTypeUnsignedLongPopulateTest extends AbstractDerivedPopulater{
-
-    private String values[]= {
-            "1",
-            "0",
-            "26758223334334" ,
-            "-1" ,
-            "-267582233"
-
-    };
-
-    private String xmlString[] = {
-            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[0]+"</DerivedUnsignedLong>",
-            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[1]+"</DerivedUnsignedLong>",
-            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[2]+"</DerivedUnsignedLong>",
-            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[3]+"</DerivedUnsignedLong>",
-            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[4]+"</DerivedUnsignedLong>"
-    };
-
-
-
-
-    protected void setUp() throws Exception {
-        className = "org.soapinterop.xsd.DerivedUnsignedLong";
-        propertyClass = UnsignedLong.class;
-    }
-
-    // force others to implement this method
-    public void testPopulate() throws Exception {
-
-        for (int i = 0; i < 3; i++) {
-            checkValue(xmlString[i],values[i]);
-        }
-
-        for (int i = 3; i < values.length; i++) {
-            try {
-                checkValue(xmlString[i],values[i]);
-                fail();
-            } catch (Exception e) {
-
-            }
-        }
-
-    }
-
-    protected String convertToString(Object o) {
-        return ConverterUtil.convertToString((UnsignedLong)o);
-    }
-}
+package org.apache.axis2.schema.populate.derived;
+
+import org.apache.axis2.databinding.types.UnsignedLong;
+import org.apache.axis2.databinding.utils.ConverterUtil;
+
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class DerivedTypeUnsignedLongPopulateTest extends AbstractDerivedPopulater{
+
+    private String values[]= {
+            "1",
+            "0",
+            "26758223334334" ,
+	    "18446744073709551615",
+            "-1" ,
+            "-267582233"
+
+    };
+
+    private String xmlString[] = {
+            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[0]+"</DerivedUnsignedLong>",
+            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[1]+"</DerivedUnsignedLong>",
+            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[2]+"</DerivedUnsignedLong>",
+            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[3]+"</DerivedUnsignedLong>",
+            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[4]+"</DerivedUnsignedLong>",
+            "<DerivedUnsignedLong xmlns=\"http://soapinterop.org/xsd\">"+values[5]+"</DerivedUnsignedLong>"
+    };
+
+
+
+
+    protected void setUp() throws Exception {
+        className = "org.soapinterop.xsd.DerivedUnsignedLong";
+        propertyClass = UnsignedLong.class;
+    }
+
+    // force others to implement this method
+    public void testPopulate() throws Exception {
+
+        for (int i = 0; i < 4; i++) {
+            checkValue(xmlString[i],values[i]);
+        }
+
+        for (int i = 4; i < values.length; i++) {
+            try {
+                checkValue(xmlString[i],values[i]);
+                fail();
+            } catch (Exception e) {
+
+            }
+        }
+
+    }
+
+    protected String convertToString(Object o) {
+        return ConverterUtil.convertToString((UnsignedLong)o);
+    }
+}

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?rev=410890&r1=410889&r2=410890&view=diff
==============================================================================
--- 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
Thu Jun  1 09:58:19 2006
@@ -1,120 +1,239 @@
-/*
- * Copyright 2001-2004 The Apache Software Foundation.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.axis2.databinding.types;
-
-import java.math.BigInteger;
-
-/**
- * Custom class for supporting primitive XSD data type UnsignedLong
- *
- * @see <a href="http://www.w3.org/TR/xmlschema-2/#unsignedLong">XML Schema 3.3.21</a>
- */
-public class UnsignedLong extends java.lang.Number {
-
-    private static final long serialVersionUID = -5919942584284897583L;
-    
-	protected BigInteger lValue = BigInteger.ZERO;
-    private static BigInteger MAX = new BigInteger("18446744073709551615"); // max unsigned
long
-
-    public UnsignedLong() {
-    }
-
-    public UnsignedLong(double value) throws NumberFormatException {
-        setValue(new BigInteger(Double.toString(value)));
-    }
-
-    public UnsignedLong(BigInteger value) throws NumberFormatException {
-        setValue(value);
-    }
-
-    public UnsignedLong(long lValue) throws NumberFormatException {
-        setValue(BigInteger.valueOf(lValue));
-    }
-
-    public UnsignedLong(String stValue) throws NumberFormatException {
-        setValue(new BigInteger(stValue));
-    }
-
-    private void setValue(BigInteger val) {
-        if (!UnsignedLong.isValid(val)) {
-            throw new NumberFormatException(
-//                    Messages.getMessage("badUnsignedLong00") +
-                    String.valueOf(val) + "]");
-        }
-        this.lValue = val;
-    }
-
-    public static boolean isValid(BigInteger value) {
-        return !(value.compareTo(BigInteger.ZERO) == -1 || // less than zero
-                value.compareTo(MAX) == 1);
-    }
-
-    public String toString() {
-        return lValue.toString();
-    }
-
-    public int hashCode() {
-        if (lValue != null)
-            return lValue.hashCode();
-        else
-            return 0;
-    }
-
-    private Object __equalsCalc = null;
-
-    public synchronized boolean equals(Object obj) {
-        if (!(obj instanceof UnsignedLong)) return false;
-        UnsignedLong other = (UnsignedLong) obj;
-        if (obj == null) return false;
-        if (this == obj) return true;
-        if (__equalsCalc != null) {
-            return (__equalsCalc == obj);
-        }
-        __equalsCalc = obj;
-        boolean _equals;
-        _equals = ((lValue == null && other.lValue == null) ||
-                (lValue != null &&
-                        lValue.equals(other.lValue)));
-        __equalsCalc = null;
-        return _equals;
-    }
-
-    // Implement java.lang.Number interface
-    public byte byteValue() {
-        return lValue.byteValue();
-    }
-
-    public short shortValue() {
-        return lValue.shortValue();
-    }
-
-    public int intValue() {
-        return lValue.intValue();
-    }
-
-    public long longValue() {
-        return lValue.longValue();
-    }
-
-    public double doubleValue() {
-        return lValue.doubleValue();
-    }
-
-    public float floatValue() {
-        return lValue.floatValue();
-    }
-
-}
+/*
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.axis2.databinding.types;
+
+// Consider removing this.
+// 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;
+
+/**
+ * Custom class for supporting primitive XSD data type UnsignedLong
+ *
+ * @see <a href="http://www.w3.org/TR/xmlschema-2/#unsignedLong">XML Schema 3.3.21</a>
+ */
+public class UnsignedLong extends java.lang.Number implements Comparable {
+
+    private static final long serialVersionUID = -5919942584284897583L;
+    
+	protected BigInteger lValue = BigInteger.ZERO;
+    private static BigInteger MAX = new BigInteger("18446744073709551615"); // max unsigned
long
+
+    public UnsignedLong() {
+    }
+
+    public UnsignedLong(double value) throws NumberFormatException {
+        setValue(new BigInteger(Double.toString(value)));
+    }
+
+    public UnsignedLong(BigInteger value) throws NumberFormatException {
+        setValue(value);
+    }
+
+    public UnsignedLong(long lValue) throws IllegalArgumentException {
+	// 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.");
+	}
+
+    }
+
+    private void setValue(BigInteger val) {
+        if (!UnsignedLong.isValid(val)) {
+            throw new IllegalArgumentException(
+//                    Messages.getMessage("badUnsignedLong00") +
+                    String.valueOf(val) + "]");
+        }
+        this.lValue = val;
+    }
+
+    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();
+
+        return !(compare(unsignedLongValue, BigInteger.ZERO.longValue()) < 0 || // less
than zero
+		 compare(unsignedLongValue, MAX.longValue()) > 0);
+    }
+
+    public String toString() {
+        return lValue.toString();
+    }
+
+    public int hashCode() {
+        if (lValue != null)
+            return lValue.hashCode();
+        else
+            return 0;
+    }
+
+    private Object __equalsCalc = null;
+
+    public synchronized boolean equals(Object obj) {
+        if (!(obj instanceof UnsignedLong)) return false;
+        UnsignedLong other = (UnsignedLong) obj;
+        if (obj == null) return false;
+        if (this == obj) return true;
+        if (__equalsCalc != null) {
+            return (__equalsCalc == obj);
+        }
+        __equalsCalc = obj;
+        boolean _equals;
+        _equals = ((lValue == null && other.lValue == null) ||
+                (lValue != null &&
+                        lValue.equals(other.lValue)));
+        __equalsCalc = null;
+        return _equals;
+    }
+
+    // Implement java.lang.Number interface
+    public byte byteValue() {
+        return lValue.byteValue();
+    }
+
+    public short shortValue() {
+        return lValue.shortValue();
+    }
+
+    public int intValue() {
+        return lValue.intValue();
+    }
+
+    public long longValue() {
+        return lValue.longValue();
+    }
+
+    public double doubleValue() {
+        return lValue.doubleValue();
+    }
+
+    public float floatValue() {
+        return lValue.floatValue();
+    }
+
+    /**
+     * @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;
+	}
+
+    }
+
+    /**
+     * @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.
+	    }
+	}
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org


Mime
View raw message