[Practicalxml-commits] SF.net SVN: practicalxml:[57] trunk/src
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-12-17 14:03:23
|
Revision: 57
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=57&view=rev
Author: kdgregory
Date: 2008-12-17 14:03:16 +0000 (Wed, 17 Dec 2008)
Log Message:
-----------
AbstractFunction: implement Comparable (will support FunctionResolver)
Modified Paths:
--------------
trunk/src/main/java/net/sf/practicalxml/xpath/AbstractFunction.java
trunk/src/test/java/net/sf/practicalxml/xpath/TestAbstractFunction.java
Modified: trunk/src/main/java/net/sf/practicalxml/xpath/AbstractFunction.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/xpath/AbstractFunction.java 2008-12-17 03:36:43 UTC (rev 56)
+++ trunk/src/main/java/net/sf/practicalxml/xpath/AbstractFunction.java 2008-12-17 14:03:16 UTC (rev 57)
@@ -42,7 +42,7 @@
* element in the list.
*/
public class AbstractFunction<T>
-implements XPathFunction
+implements XPathFunction, Comparable<AbstractFunction<?>>
{
/**
* Qualified name of this function.
@@ -156,7 +156,47 @@
&& (arity <= _maxArgCount);
}
+//----------------------------------------------------------------------------
+// Implementation of Comparable
+//----------------------------------------------------------------------------
+ /**
+ * Instances of this class implement <code>Comparable</code> in order to
+ * support a function resolver picking the most appropriate instance for
+ * a particular invocation. Comparison starts with namespace and name,
+ * using <code>String.compareTo()</code>. If two instances have the same
+ * namespace and name, they are compared based on the number of arguments
+ * they accept:
+ * <ul>
+ * <li> Instances that accept fewer arguments are considered less-than
+ * instances that accept more.
+ * <li> If two instances accept the same number of arguments, the instance
+ * with the lower minimum argument count is less-than that with the
+ * higher count.
+ * </ul>
+ */
+ public int compareTo(AbstractFunction<?> that)
+ {
+ int result = this._qname.getNamespaceURI().compareTo(that._qname.getNamespaceURI());
+ if (result != 0)
+ return (result < 0) ? -1 : 1;
+
+ result = this._qname.getLocalPart().compareTo(that._qname.getLocalPart());
+ if (result != 0)
+ return (result < 0) ? -1 : 1;
+
+ result = (this._maxArgCount - this._minArgCount) - (that._maxArgCount - that._minArgCount);
+ if (result != 0)
+ return (result < 0) ? -1 : 1;
+
+ result = this._minArgCount - that._minArgCount;
+ if (result != 0)
+ return (result < 0) ? -1 : 1;
+
+ return result;
+ }
+
+
//----------------------------------------------------------------------------
// Implementation of XPathFunction
//----------------------------------------------------------------------------
Modified: trunk/src/test/java/net/sf/practicalxml/xpath/TestAbstractFunction.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/xpath/TestAbstractFunction.java 2008-12-17 03:36:43 UTC (rev 56)
+++ trunk/src/test/java/net/sf/practicalxml/xpath/TestAbstractFunction.java 2008-12-17 14:03:16 UTC (rev 57)
@@ -317,4 +317,46 @@
assertSame(helper, fn.evaluate(Collections.<String>emptyList()));
}
+
+
+ public void testComparable()
+ throws Exception
+ {
+ AbstractFunction<Object> fn1 = new AbstractFunction<Object>("foo", "bar");
+ AbstractFunction<Object> fn2 = new AbstractFunction<Object>("foo", "bar", 1, 5);
+ AbstractFunction<Object> fn3 = new AbstractFunction<Object>("foo", "bar", 2, 5);
+ AbstractFunction<Object> fn4 = new AbstractFunction<Object>("foo", "bar", 1, 4);
+ AbstractFunction<Object> fn5 = new AbstractFunction<Object>("foo", "bar", 3);
+ AbstractFunction<Object> fn6 = new AbstractFunction<Object>("foo", "baz", 3);
+ AbstractFunction<Object> fn7 = new AbstractFunction<Object>("fzz", "bar", 3);
+
+ // always equal to self
+ assertEquals(0, fn1.compareTo(fn1));
+ assertEquals(0, fn2.compareTo(fn2));
+ assertEquals(0, fn3.compareTo(fn3));
+ assertEquals(0, fn4.compareTo(fn4));
+ assertEquals(0, fn5.compareTo(fn5));
+ assertEquals(0, fn6.compareTo(fn6));
+ assertEquals(0, fn7.compareTo(fn7));
+
+ // differing name/namespace
+ assertEquals(-1, fn5.compareTo(fn7));
+ assertEquals(1, fn7.compareTo(fn5));
+ assertEquals(-1, fn5.compareTo(fn6));
+ assertEquals(1, fn6.compareTo(fn5));
+ assertEquals(-1, fn1.compareTo(fn6));
+ assertEquals(1, fn6.compareTo(fn1));
+
+ // differing number of arguments
+ assertEquals(1, fn1.compareTo(fn2));
+ assertEquals(-1, fn2.compareTo(fn1));
+ assertEquals(1, fn2.compareTo(fn3));
+ assertEquals(-1, fn3.compareTo(fn2));
+ assertEquals(1, fn3.compareTo(fn5));
+ assertEquals(-1, fn5.compareTo(fn3));
+
+ // same number of arguments, differing range
+ assertEquals(1, fn3.compareTo(fn4));
+ assertEquals(-1, fn4.compareTo(fn3));
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|