[Mulgara-svn] r1579 - in trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool: . xa

pag at mulgara.org pag at mulgara.org
Tue Mar 3 07:34:57 UTC 2009


Author: pag
Date: 2009-03-02 23:34:56 -0800 (Mon, 02 Mar 2009)
New Revision: 1579

Added:
   trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPNumber.java
Modified:
   trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/AbstractSPObject.java
   trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPLimit.java
   trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPObject.java
   trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDecimalImpl.java
   trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDoubleImpl.java
   trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPFloatImpl.java
Log:
Added the capability to do by-value inequality tests for numeric literals

Modified: trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/AbstractSPObject.java
===================================================================
--- trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/AbstractSPObject.java	2009-03-03 07:33:13 UTC (rev 1578)
+++ trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/AbstractSPObject.java	2009-03-03 07:34:56 UTC (rev 1579)
@@ -184,6 +184,28 @@
 
 
   /**
+   * Comparison used for inequalities on numerical values for an object.
+   * If one of these objects is not a number, then use the standard comparison.
+   * @return -1 if this is smaller than o, +1 if larger, 0 if equal, or the result of compareTo
+   *         if not a number.
+   */
+  public int numericalCompare(SPObject o) {
+    assert !isNumber();
+    return compareTo(o);
+  }
+
+
+  /**
+   * Indicates if this object is a number. Not usually, so returns <code>false</code> in this
+   * abstract class. XSD extensions the represent numerical values should return true.
+   * @return <code>true</code> if this object is a number. False otherwise.
+   */
+  public boolean isNumber() {
+    return false;
+  }
+
+
+  /**
    * Utility for long comparisons
    * @param a The first long value
    * @param b The second long value

Modified: trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPLimit.java
===================================================================
--- trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPLimit.java	2009-03-03 07:33:13 UTC (rev 1578)
+++ trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPLimit.java	2009-03-03 07:34:56 UTC (rev 1579)
@@ -160,4 +160,18 @@
 
   }
 
+  /** 
+   * @see org.mulgara.store.stringpool.SPObject#isNumber()
+   */
+  public boolean isNumber() {
+    return false;
+  }
+
+  /**
+   * @see org.mulgara.store.stringpool.SPObject#numericalCompare(org.mulgara.store.stringpool.SPObject)
+   */
+  public int numericalCompare(SPObject o) {
+    return compareTo(o);
+  }
+
 }

Modified: trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPObject.java
===================================================================
--- trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPObject.java	2009-03-03 07:33:13 UTC (rev 1578)
+++ trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/SPObject.java	2009-03-03 07:34:56 UTC (rev 1579)
@@ -62,6 +62,21 @@
   public boolean isSmallest();
 
 
+  /**
+   * Tests if this object represents a number.
+   * @return <code>true</code> if this object is a number.
+   */
+  public boolean isNumber();
+
+
+  /**
+   * Compare objects numerically. If one object is not a number, then use the standard comparison.
+   * @param o The object to compare to.
+   * @return -1 if this object is smaller, +1 if larger, 0 if equal.
+   */
+  public int numericalCompare(SPObject o);
+
+
   public static class TypeCategory {
 
     // Indicates an unused G2N record.

Modified: trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDecimalImpl.java
===================================================================
--- trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDecimalImpl.java	2009-03-03 07:33:13 UTC (rev 1578)
+++ trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDecimalImpl.java	2009-03-03 07:34:56 UTC (rev 1579)
@@ -51,7 +51,7 @@
  * @copyright &copy; 2004 <A href="http://www.PIsoftware.com/">Plugged In Software Pty Ltd</A>
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
-public abstract class SPDecimalImpl extends AbstractSPTypedLiteral {
+public abstract class SPDecimalImpl extends AbstractSPTypedLiteral implements SPNumber {
 
   @SuppressWarnings("unused")
   private final static Logger logger = Logger.getLogger(SPDecimalImpl.class);
@@ -218,6 +218,48 @@
   }
 
 
+  /**
+   * Indicates if this object is a number.
+   * @return <code>true</code> since this object is a number.
+   */
+  public boolean isNumber() {
+    return true;
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.AbstractSPObject#numericalCompare(org.mulgara.store.stringpool.SPObject)
+   */
+  public int numericalCompare(SPObject o) {
+    if (!o.isNumber()) return compareTo(o);
+    return -((SPNumber)o).numericalCompareTo(val);
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(java.math.BigDecimal)
+   */
+  public int numericalCompareTo(BigDecimal n) {
+    return val.compareTo(n);
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(double)
+   */
+  public int numericalCompareTo(double d) {
+    return val.compareTo(new BigDecimal(d));
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(long)
+   */
+  public int numericalCompareTo(long l) {
+    return val.compareTo(new BigDecimal(l));
+  }
+
+
   /** @see java.lang.Object#hashCode() */
   public int hashCode() {
     return lexical.hashCode();
@@ -282,7 +324,7 @@
 class SPDecimalExtImpl extends SPDecimalImpl {
 
   /** The long value containing the number. */
-  final Long l;
+  final long l;
 
   /**
    * Creates an xsd:decimal extension out of a long.
@@ -426,4 +468,37 @@
 
   }
 
+
+  /**
+   * Indicates if this object is a number.
+   * @return <code>true</code> since this object is a number.
+   */
+  public boolean isNumber() {
+    return true;
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.AbstractSPObject#numericalCompare(org.mulgara.store.stringpool.SPObject)
+   */
+  public int numericalCompare(SPObject o) {
+    if (!o.isNumber()) return compareTo(o);
+    return -((SPNumber)o).numericalCompareTo(l);
+  }
+
+
+  public int numericalCompareTo(BigDecimal n) {
+    return new BigDecimal(l).compareTo(n);
+  }
+
+
+  public int numericalCompareTo(double d) {
+    return Double.compare((double)l, d);
+  }
+
+
+  public int numericalCompareTo(long l) {
+    return this.l < l ? -1 : (this.l > l ? 1 : 0);
+  }
+
 }
\ No newline at end of file

Modified: trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDoubleImpl.java
===================================================================
--- trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDoubleImpl.java	2009-03-03 07:33:13 UTC (rev 1578)
+++ trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPDoubleImpl.java	2009-03-03 07:34:56 UTC (rev 1579)
@@ -27,6 +27,7 @@
 package org.mulgara.store.stringpool.xa;
 
 // Java 2 standard packages
+import java.math.BigDecimal;
 import java.net.URI;
 import java.nio.ByteBuffer;
 
@@ -59,7 +60,7 @@
  *
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
-public final class SPDoubleImpl extends AbstractSPTypedLiteral implements SPDouble {
+public final class SPDoubleImpl extends AbstractSPTypedLiteral implements SPDouble, SPNumber {
 
   @SuppressWarnings("unused")
   private final static Logger logger = Logger.getLogger(SPDoubleImpl.class);
@@ -147,6 +148,51 @@
   }
 
 
+  /**
+   * Comparison used for inequalities the value.
+   * If o is not a number, then use the standard comparison.
+   * @return -1 if this is smaller than o, +1 if larger, 0 if equal, or the result of compareTo
+   *         if not a number.
+   */
+  public int numericalCompare(SPObject o) {
+    return o.isNumber() ? -((SPNumber)o).numericalCompareTo(d) : compareTo(o);
+  }
+
+
+  /**
+   * Indicates if this object is a number. Not usually, so returns <code>false</code> in this
+   * abstract class. XSD extensions the represent numerical values should return true.
+   * @return <code>true</code> if this object is a number. False otherwise.
+   */
+  public boolean isNumber() {
+    return true;
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(java.math.BigInteger)
+   */
+  public int numericalCompareTo(BigDecimal n) {
+    double dn = n.doubleValue();
+    return d < dn ? -1 : (d > dn ? 1 : 0);
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(double)
+   */
+  public int numericalCompareTo(double d) {
+    return Double.compare(this.d, d);
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(long)
+   */
+  public int numericalCompareTo(long l) {
+    return Double.compare(d, (double)l);
+  }
+
   /** Compares the binary representations of two SPDoubleImpl objects. */
   public static class SPDoubleComparator implements SPComparator {
 

Modified: trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPFloatImpl.java
===================================================================
--- trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPFloatImpl.java	2009-03-03 07:33:13 UTC (rev 1578)
+++ trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPFloatImpl.java	2009-03-03 07:34:56 UTC (rev 1579)
@@ -27,6 +27,7 @@
 package org.mulgara.store.stringpool.xa;
 
 // Java 2 standard packages
+import java.math.BigDecimal;
 import java.net.URI;
 import java.nio.ByteBuffer;
 
@@ -59,7 +60,7 @@
  *
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
-public final class SPFloatImpl extends AbstractSPTypedLiteral {
+public final class SPFloatImpl extends AbstractSPTypedLiteral implements SPNumber {
 
   @SuppressWarnings("unused")
   private final static Logger logger = Logger.getLogger(SPFloatImpl.class);
@@ -158,4 +159,50 @@
 
   }
 
+
+  /**
+   * Comparison used for inequalities the value.
+   * If o is not a number, then use the standard comparison.
+   * @return -1 if this is smaller than o, +1 if larger, 0 if equal, or the result of compareTo
+   *         if not a number.
+   */
+  public int numericalCompare(SPObject o) {
+    return o.isNumber() ? -((SPNumber)o).numericalCompareTo(f) : compareTo(o);
+  }
+
+
+  /**
+   * Indicates if this object is a number. Not usually, so returns <code>false</code> in this
+   * abstract class. XSD extensions the represent numerical values should return true.
+   * @return <code>true</code> if this object is a number. False otherwise.
+   */
+  public boolean isNumber() {
+    return true;
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(java.math.BigDecimal)
+   */
+  public int numericalCompareTo(BigDecimal n) {
+    double dn = n.doubleValue();
+    return (double)f < dn ? -1 : (f > dn ? 1 : 0);
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(double)
+   */
+  public int numericalCompareTo(double d) {
+    return Double.compare(f, d);
+  }
+
+
+  /**
+   * @see org.mulgara.store.stringpool.xa.SPNumber#numericalCompareTo(long)
+   */
+  public int numericalCompareTo(long l) {
+    return Double.compare(f, (double)l);
+  }
+
 }

Added: trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPNumber.java
===================================================================
--- trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPNumber.java	                        (rev 0)
+++ trunk/src/jar/store-stringpool/java/org/mulgara/store/stringpool/xa/SPNumber.java	2009-03-03 07:34:56 UTC (rev 1579)
@@ -0,0 +1,47 @@
+/*
+ * The contents of this file are subject to the Open Software License
+ * Version 3.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.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+
+package org.mulgara.store.stringpool.xa;
+
+import java.math.BigDecimal;
+
+/**
+ * Used to indicate that the interface is a number, and to do by-value inequality.
+ *
+ * @created Mar 2, 2009
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="http://www.topazproject.org/">The Topaz Project</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public interface SPNumber {
+
+  /**
+   * Compare the value of current object to a BigDecimal.
+   * @param n The BigInteger to compare to.
+   * @return -1 if the value of this object is smaller, +1 if larger, 0 if equal.
+   */
+  public int numericalCompareTo(BigDecimal n);
+
+  /**
+   * Compare the value of current object to a BigInteger.
+   * @param n The double to compare to.
+   * @return -1 if the value of this object is smaller, +1 if larger, 0 if equal.
+   */
+  public int numericalCompareTo(double d);
+
+  /**
+   * Compare the current object to a long.
+   * @param n The long to compare to.
+   * @return -1 if the value of this object is smaller, +1 if larger, 0 if equal.
+   */
+  public int numericalCompareTo(long l);
+}




More information about the Mulgara-svn mailing list