[Mulgara-svn] r687 - branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic

pag at mulgara.org pag at mulgara.org
Mon Mar 17 22:08:57 UTC 2008


Author: pag
Date: 2008-03-17 15:08:57 -0700 (Mon, 17 Mar 2008)
New Revision: 687

Added:
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/UnaryMinus.java
Modified:
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/BinaryOperation.java
Log:
Added unary minus operation. Factored out common elements between BinaryOperation and the new unary operation into a single AbstractNumericOperation

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/AbstractNumericOperation.java	2008-03-17 22:08:57 UTC (rev 687)
@@ -0,0 +1,102 @@
+package org.mulgara.query.filter.arithmetic;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.Context;
+import org.mulgara.query.filter.ContextOwner;
+import org.mulgara.query.filter.value.AbstractComparable;
+import org.mulgara.query.filter.value.RDFTerm;
+
+public abstract class AbstractNumericOperation extends AbstractComparable {
+
+  /** The owner of the context for resolving here */
+  private ContextOwner owner = null;
+
+  public AbstractNumericOperation() {
+    super();
+  }
+
+  /** @see org.mulgara.query.filter.value.ComparableExpression#getValue() */
+  public Object getValue() throws QueryException { return getNumber(); }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#equals() */
+  public boolean equals(RDFTerm v) throws QueryException { return getNumber().equals(v.getValue()); }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#isBlank() */
+  public boolean isBlank() throws QueryException { return false; }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#isIRI() */
+  public boolean isIRI() throws QueryException { return false; }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#isLiteral() */
+  public boolean isLiteral() throws QueryException { return false; }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#isURI() */
+  public boolean isURI() throws QueryException { return false; }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#notEquals() */
+  public boolean notEquals(RDFTerm v) throws QueryException { return !getNumber().equals(v.getValue()); }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#sameTerm() */
+  public boolean sameTerm(RDFTerm v) throws QueryException { return equals(v); }
+
+  /** @see org.mulgara.query.filter.Filter#test() */
+  public boolean test(Context context) throws QueryException {
+    setCurrentContext(context);
+    return getNumber().doubleValue() != 0.0;
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#setContextOwner(org.mulgara.query.filter.ContextOwner) */
+  public void setContextOwner(ContextOwner owner) {
+    this.owner = owner;
+  }
+
+  /** @see org.mulgara.query.filter.value.RDFTerm#getContextOwner() */
+  public ContextOwner getContextOwner() {
+    return owner;
+  }
+
+  /** @see org.mulgara.query.filter.value.NumericExpression#getNumber() */
+  public abstract Number getNumber() throws QueryException;
+
+
+  //////////////////////////////////////////////////////////////
+  // Implementation of AbstractComparable.compare(Object,Object)
+  //////////////////////////////////////////////////////////////
+  
+  /** The set of classes to be compared as floating point */
+  private static Set<Class<? extends Number>> floatUpcast = new HashSet<Class<? extends Number>>();
+  
+  /* Initialize the set of classes to be compared as floating point */
+  static {
+    floatUpcast.add(Double.class);
+    floatUpcast.add(Float.class);
+  }
+
+  /**
+   * @see org.mulgara.query.filter.value.AbstractComparable#compare(java.lang.Object, java.lang.Object)
+   */
+  protected int compare(Object left, Object right) throws QueryException {
+    return compare((Number)left, right);
+  }
+
+  /**
+   * Compares 2 numbers of unknown type.  If they are both floating point, then use Double compare,
+   * otherwise use Long compare.
+   * @param left The first number in the comparison.
+   * @param right The second number. This cannot be assumed to be a number due to poorly formed queries.
+   * @return -1 if left<right, +1 if left>right, 0 if left==right
+   * @throws QueryException if right is a type other than {@link java.lang.Number}.
+   */
+  protected int compare(Number left, Object right) throws QueryException {
+    if (!(right instanceof Number)) throw new QueryException("Cannot compare a number to a: " + right.getClass().getSimpleName());
+    if (floatUpcast.contains(left.getClass()) || floatUpcast.contains(right.getClass())) {
+      return Double.valueOf(left.doubleValue()).compareTo(((Number)right).doubleValue());
+    }
+    return Long.valueOf(left.longValue()).compareTo(((Number)right).longValue());
+  }
+
+
+}
\ No newline at end of file

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/BinaryOperation.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/BinaryOperation.java	2008-03-17 22:06:42 UTC (rev 686)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/BinaryOperation.java	2008-03-17 22:08:57 UTC (rev 687)
@@ -12,16 +12,10 @@
 package org.mulgara.query.filter.arithmetic;
 
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
 
 import org.mulgara.query.QueryException;
-import org.mulgara.query.filter.Context;
-import org.mulgara.query.filter.ContextOwner;
-import org.mulgara.query.filter.value.AbstractComparable;
 import org.mulgara.query.filter.value.NumericExpression;
-import org.mulgara.query.filter.value.RDFTerm;
 
 
 /**
@@ -32,7 +26,8 @@
  * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
  * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
  */
-public abstract class BinaryOperation extends AbstractComparable implements NumericExpression {
+public abstract class BinaryOperation extends 
+AbstractNumericOperation implements NumericExpression {
 
   /** The first operand */
   protected NumericExpression lhs;
@@ -40,9 +35,6 @@
   /** The second operand */
   protected NumericExpression rhs;
 
-  /** The owner of the context for resolving here */
-  private ContextOwner owner = null;
-
   /**
    * Creates an operation between two terms
    * @param lhs The left side of the operation
@@ -55,46 +47,6 @@
     rhs.setContextOwner(this);
   }
 
-  /** @see org.mulgara.query.filter.value.ComparableExpression#getValue() */
-  public Object getValue() throws QueryException { return getNumber(); }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#equals() */
-  public boolean equals(RDFTerm v) throws QueryException { return getNumber().equals(v.getValue()); }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#isBlank() */
-  public boolean isBlank() throws QueryException { return false; }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#isIRI() */
-  public boolean isIRI() throws QueryException { return false; }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#isLiteral() */
-  public boolean isLiteral() throws QueryException { return false; }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#isURI() */
-  public boolean isURI() throws QueryException { return false; }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#notEquals() */
-  public boolean notEquals(RDFTerm v) throws QueryException { return !getNumber().equals(v.getValue()); }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#sameTerm() */
-  public boolean sameTerm(RDFTerm v) throws QueryException { return equals(v); }
-
-  /** @see org.mulgara.query.filter.Filter#test() */
-  public boolean test(Context context) throws QueryException {
-    setCurrentContext(context);
-    return getNumber().doubleValue() != 0.0;
-  }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#setContextOwner(org.mulgara.query.filter.ContextOwner) */
-  public void setContextOwner(ContextOwner owner) {
-    this.owner = owner;
-  }
-
-  /** @see org.mulgara.query.filter.value.RDFTerm#getContextOwner() */
-  public ContextOwner getContextOwner() {
-    return owner;
-  }
-
   // Not using generics in NumberOps as we can't know the types at this stage, but they are handy
   // for defining the classes correctly
   /**
@@ -121,39 +73,6 @@
   @SuppressWarnings("unchecked")
   abstract Number doOperation(NumberOps ops, Number left, Number right);
 
-  /** The set of classes to be compared as floating point */
-  private static Set<Class<? extends Number>> floatUpcast = new HashSet<Class<? extends Number>>();
-  
-  /* Initialize the set of classes to be compared as floating point */
-  static {
-    floatUpcast.add(Double.class);
-    floatUpcast.add(Float.class);
-  }
-
-  /**
-   * @see org.mulgara.query.filter.value.AbstractComparable#compare(java.lang.Object, java.lang.Object)
-   */
-  protected int compare(Object left, Object right) throws QueryException {
-    return compare((Number)left, right);
-  }
-
-  /**
-   * Compares 2 numbers of unknown type.  If they are both floating point, then use Double compare,
-   * otherwise use Long compare.
-   * @param left The first number in the comparison.
-   * @param right The second number. This cannot be assumed to be a number due to poorly formed queries.
-   * @return -1 if left<right, +1 if left>right, 0 if left==right
-   * @throws QueryException if right is a type other than {@link java.lang.Number}.
-   */
-  protected int compare(Number left, Object right) throws QueryException {
-    if (!(right instanceof Number)) throw new QueryException("Cannot compare a number to a: " + right.getClass().getSimpleName());
-    if (floatUpcast.contains(left.getClass()) || floatUpcast.contains(right.getClass())) {
-      return Double.valueOf(left.doubleValue()).compareTo(((Number)right).doubleValue());
-    }
-    return Long.valueOf(left.longValue()).compareTo(((Number)right).longValue());
-  }
-
-
   /** Stores classes as an integrated pair for mapping pairs of Numbers to their appropriate functions */
   static class ClassPair {
     private Class<? extends Number> left;

Added: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/UnaryMinus.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/UnaryMinus.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/arithmetic/UnaryMinus.java	2008-03-17 22:08:57 UTC (rev 687)
@@ -0,0 +1,110 @@
+/**
+ * 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.query.filter.arithmetic;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.value.AbstractComparable;
+import org.mulgara.query.filter.value.NumericExpression;
+
+
+/**
+ * Represents a numeric negation.
+ *
+ * @created Mar 17, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public abstract class UnaryMinus extends AbstractComparable implements NumericExpression {
+
+  /** The operand */
+  protected NumericExpression operand;
+
+  /**
+   * Creates a negation of the given term.
+   * @param operand The numeric term to negate.
+   */
+  UnaryMinus(NumericExpression operand) {
+    this.operand = operand;
+    operand.setContextOwner(this);
+  }
+
+  // Not using generics in NumberOps as we can't know the types at this stage, but they are handy
+  // for defining the classes correctly
+  /**
+   * Calculate the result of this operation, returning it as a normal number.
+   * @throws QueryException The values of one of the operands could not be resolved.
+   * @see org.mulgara.query.filter.value.NumericExpression#getNumber()
+   */
+  @SuppressWarnings("unchecked")
+  public Number getNumber() throws QueryException {
+    Number n = operand.getNumber();
+    InversionOp op = opMap.get(n.getClass());
+    if (op == null) throw new AssertionError("Missing entry in negation operation map");
+    return op.invert(n);
+  }
+
+  /** Defines a unary negation function that returns a number of the same type as it receives */
+  interface InversionOp<T extends Number> {
+    public Class<T> getType();
+    public Number invert(T v);
+  }
+
+  /** A map of types to the functions that multiply them with correct type promotions */
+  protected static Map<Class<? extends Number>,InversionOp<? extends Number>> opMap = new HashMap<Class<? extends Number>,InversionOp<? extends Number>>();
+
+  /** A utility to add a number function to the promotion map */
+  private static void addType(InversionOp<? extends Number> op) { opMap.put(op.getType(), op); }
+
+  static {
+    addType(new InvertD());
+    addType(new InvertF());
+    addType(new InvertL());
+    addType(new InvertI());
+    addType(new InvertS());
+    addType(new InvertB());
+  }
+
+  private static class InvertD implements InversionOp<Double> {
+    public Class<Double> getType() { return Double.class; }
+    public Number invert(Double v) { return -v; }
+  }
+
+  private static class InvertF implements InversionOp<Float> {
+    public Class<Float> getType() { return Float.class; }
+    public Number invert(Float v) { return -v; }
+  }
+
+  private static class InvertL implements InversionOp<Long> {
+    public Class<Long> getType() { return Long.class; }
+    public Number invert(Long v) { return -v; }
+  }
+
+  private static class InvertI implements InversionOp<Integer> {
+    public Class<Integer> getType() { return Integer.class; }
+    public Number invert(Integer v) { return -v; }
+  }
+
+  private static class InvertS implements InversionOp<Short> {
+    public Class<Short> getType() { return Short.class; }
+    public Number invert(Short v) { return -v; }
+  }
+
+  private static class InvertB implements InversionOp<Byte> {
+    public Class<Byte> getType() { return Byte.class; }
+    public Number invert(Byte v) { return -v; }
+  }
+
+}




More information about the Mulgara-svn mailing list