Commit 25e5e9ed authored by Bernd Kiefer's avatar Bernd Kiefer
Browse files

add generic number comparison

parent 215a3f40
......@@ -7,7 +7,7 @@
<groupId>de.dfki.lt.hfc</groupId>
<artifactId>hfc</artifactId>
<version>1.2.2-SNAPSHOT</version>
<version>1.2.3-SNAPSHOT</version>
<packaging>jar</packaging>
<inceptionYear>2015</inceptionYear>
......
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
/**
* checks whether the first argument is equal to the second argument;
* arguments are assumed to be numbers of type xsd:int
*
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
*
* @see FunctionalOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Wed Jun 23 11:04:27 CEST 2010
*/
public final class Equal extends FunctionalOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD ints;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return getObject(args[0]).compareTo(getObject(args[1])) == 0
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
/**
* checks whether the first argument is greater than the second argument;
* arguments are assumed to be xsd:longs;
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
*
* @see FunctionalOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Tue Sep 29 11:11:19 CEST 2009
*/
public final class Greater extends FunctionalOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD longs;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return getObject(args[0]).compareTo(getObject(args[1])) > 0
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
/**
* checks whether the first argument is greater or equal than the second argument;
* arguments are assumed to be xsd:floats;
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
*
* @see FunctionalOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Tue Sep 29 11:11:19 CEST 2009
*/
public final class GreaterEqual extends FunctionalOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return getObject(args[0]).compareTo(getObject(args[1])) >= 0
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
/**
* checks whether the first argument is less than the second argument;
* arguments are assumed to be xsd:floats;
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
*
* @see FunctionalOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Tue Sep 29 11:11:19 CEST 2009
*/
public final class Less extends FunctionalOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD floats;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return getObject(args[0]).compareTo(getObject(args[1])) < 0
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
}
package de.dfki.lt.hfc.operators;
import de.dfki.lt.hfc.FunctionalOperator;
/**
* checks whether the first argument is less or equal than the second argument;
* arguments are assumed to be xsd:ints;
* @return FunctionalOperator.TRUE or FunctionalOperator.FALSE
*
* @see FunctionalOperator
*
* @author (C) Hans-Ulrich Krieger
* @since JDK 1.5
* @version Tue Sep 29 11:11:19 CEST 2009
*/
public final class LessEqual extends FunctionalOperator {
/**
* note that apply() does NOT check at the moment whether the int args
* represent in fact XSD ints;
* note that apply() does NOT check whether it is given exactly two arguments
*/
public int apply(int[] args) {
return getObject(args[0]).compareTo(getObject(args[1])) <= 0
? FunctionalOperator.TRUE : FunctionalOperator.FALSE;
}
}
......@@ -5,7 +5,7 @@ package de.dfki.lt.hfc.types;
* @author Christian Willms - Date: 28.11.17 17:11.
* @version 28.11.17
*/
public class XsdDecimal extends XsdAnySimpleType{
public class XsdDecimal extends XsdNumber{
public final static String NAME = "decimal";
......@@ -73,10 +73,11 @@ public class XsdDecimal extends XsdAnySimpleType{
AnyType.MinMaxValue minMaxValue = (MinMaxValue) o;
return minMaxValue.compareTo(this);
}
if (! (o instanceof XsdDecimal)){
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
return Double.compare(this.value,((XsdDecimal) o).value);
if (o instanceof XsdDecimal)
return Double.compare(this.value,((XsdDecimal) o).value);
else if (o instanceof XsdNumber)
return Double.compare(this.value, ((XsdNumber)o).toNumber().doubleValue());
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
}
......@@ -8,7 +8,7 @@ package de.dfki.lt.hfc.types;
* @since JDK 1.5
* @version Fri Jan 29 19:28:31 CET 2016
*/
public final class XsdDouble extends XsdAnySimpleType {
public final class XsdDouble extends XsdNumber {
public final static String NAME = "double";
......@@ -85,10 +85,11 @@ public final class XsdDouble extends XsdAnySimpleType {
AnyType.MinMaxValue minMaxValue = (MinMaxValue) o;
return minMaxValue.compareTo(this);
}
if (! (o instanceof XsdDouble)){
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
return Double.compare(this.value,((XsdDouble) o).value);
if (o instanceof XsdDouble)
return Double.compare(this.value,((XsdDouble) o).value);
else if (o instanceof XsdNumber)
return Double.compare(this.value, ((XsdNumber)o).toNumber().doubleValue());
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
}
......@@ -6,7 +6,7 @@ package de.dfki.lt.hfc.types;
* @since JDK 1.5
* @version Fri Jan 29 19:29:19 CET 2016
*/
public final class XsdFloat extends XsdAnySimpleType {
public final class XsdFloat extends XsdNumber {
public final static String NAME = "float";
public final static String SHORT_NAME = '<' + SHORT_PREFIX + NAME + '>';
......@@ -87,10 +87,11 @@ public final class XsdFloat extends XsdAnySimpleType {
AnyType.MinMaxValue minMaxValue = (MinMaxValue) o;
return minMaxValue.compareTo(this);
}
if (! (o instanceof XsdFloat)){
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
return Float.compare(this.value,((XsdFloat) o).value);
if (o instanceof XsdFloat)
return Float.compare(this.value,((XsdFloat) o).value);
else if (o instanceof XsdNumber)
return Float.compare(this.value, ((XsdNumber)o).toNumber().floatValue());
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
}
......@@ -5,7 +5,7 @@ package de.dfki.lt.hfc.types;
* @since JDK 1.5
* @version Fri Jan 29 19:30:23 CET 2016
*/
public final class XsdInt extends XsdAnySimpleType {
public final class XsdInt extends XsdNumber {
public final static String NAME = "int";
public final static String SHORT_NAME = '<' + SHORT_PREFIX + NAME + '>';
......@@ -86,10 +86,11 @@ public final class XsdInt extends XsdAnySimpleType {
AnyType.MinMaxValue minMaxValue = (MinMaxValue) o;
return minMaxValue.compareTo(this);
}
if (! (o instanceof XsdInt )){
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
return Integer.compare(this.value,((XsdInt) o).value);
if (o instanceof XsdInt)
return Integer.compare(this.value,((XsdInt) o).value);
else if (o instanceof XsdNumber)
return Integer.compare(this.value, ((XsdNumber)o).toNumber().intValue());
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
}
......@@ -9,7 +9,7 @@ package de.dfki.lt.hfc.types;
* @since JDK 1.5
* @version Fri Jan 29 19:32:15 CET 2016
*/
public final class XsdLong extends XsdAnySimpleType {
public final class XsdLong extends XsdNumber {
public final static String NAME = "long";
public final static String SHORT_NAME = '<' + SHORT_PREFIX + NAME + '>';
......@@ -91,10 +91,10 @@ public final class XsdLong extends XsdAnySimpleType {
AnyType.MinMaxValue minMaxValue = (MinMaxValue) o;
return minMaxValue.compareTo(this);
}
int res;
if (! (o instanceof XsdLong)){
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
return Long.compare(this.value,((XsdLong) o).value);
if (o instanceof XsdLong)
return Long.compare(this.value,((XsdLong) o).value);
else if (o instanceof XsdNumber)
return Long.compare(this.value, ((XsdNumber)o).toNumber().longValue());
throw new IllegalArgumentException("Can't compare " + this.getClass()+" and " + o.getClass() );
}
}
\ No newline at end of file
package de.dfki.lt.hfc.types;
/** An abstract generic class for numbers, to facilitate comparison, conversion,
* etc.
*/
public abstract class XsdNumber extends XsdAnySimpleType {
protected Number toNumber() { return (Number)toJava(); }
}
package de.dfki.lt.hfc;
import static de.dfki.lt.hfc.TestingUtils.*;
import static org.junit.Assert.assertEquals;
import java.io.FileNotFoundException;
import java.io.IOException;
import de.dfki.lt.hfc.*;
import de.dfki.lt.hfc.types.*;
import org.junit.Test;
public class LessTest {
@Test
public void testLess()
throws FileNotFoundException, IOException, WrongFormatException {
// load Namespace
Namespace namespace = new Namespace(getTestResource("default.ns"), false);
// create TupleStore
TupleStore store =
new TupleStore(false, true, true, 2, 5, 4, 2, namespace,
getTestResource("default.nt"));
int[] args = new int[5];
// create FunctionalOperator
FunctionalOperator fop =
(FunctionalOperator)store.operatorRegistry
.checkAndRegister("de.dfki.lt.hfc.operators.Less");
args[0] = store.putObject((new XsdDouble(3.1).toString(true)));
args[1] = store.putObject((new XsdLong(7l)).toString(true));
args[2] = store.putObject((new XsdInt(6)).toString(true));
args[3] = store.putObject((new XsdFloat(5.0f)).toString(true));
args[4] = store.putObject((new XsdDecimal(4.0)).toString(true));
// do operation
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[4], args[2]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[0]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[4], args[1]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[3], args[4]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[2], args[1]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[1], args[0]}));
}
@Test
public void testLessEq()
throws FileNotFoundException, IOException, WrongFormatException {
// load Namespace
Namespace namespace = new Namespace(getTestResource("default.ns"), false);
// create TupleStore
TupleStore store =
new TupleStore(false, true, true, 2, 5, 4, 2, namespace,
getTestResource("default.nt"));
int[] args = new int[5];
// create FunctionalOperator
FunctionalOperator fop =
(FunctionalOperator)store.operatorRegistry
.checkAndRegister("de.dfki.lt.hfc.operators.LessEqual");
args[0] = store.putObject((new XsdDouble(3.1).toString(true)));
args[1] = store.putObject((new XsdLong(7l)).toString(true));
args[2] = store.putObject((new XsdInt(6)).toString(true));
args[3] = store.putObject((new XsdFloat(5.0f)).toString(true));
args[4] = store.putObject((new XsdDecimal(4.0)).toString(true));
// do operation
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[4], args[2]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[0]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[4], args[1]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[3], args[4]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[2], args[1]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[1], args[0]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[2], args[2]}));
}
@Test
public void testGreater()
throws FileNotFoundException, IOException, WrongFormatException {
// load Namespace
Namespace namespace = new Namespace(getTestResource("default.ns"), false);
// create TupleStore
TupleStore store =
new TupleStore(false, true, true, 2, 5, 4, 2, namespace,
getTestResource("default.nt"));
int[] args = new int[5];
// create FunctionalOperator
FunctionalOperator fop =
(FunctionalOperator)store.operatorRegistry
.checkAndRegister("de.dfki.lt.hfc.operators.Greater");
args[0] = store.putObject((new XsdDouble(3.1).toString(true)));
args[1] = store.putObject((new XsdLong(7l)).toString(true));
args[2] = store.putObject((new XsdInt(6)).toString(true));
args[3] = store.putObject((new XsdFloat(5.0f)).toString(true));
args[4] = store.putObject((new XsdDecimal(4.0)).toString(true));
// do operation
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[2]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[4], args[0]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[1]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[3], args[4]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[2], args[1]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[1], args[0]}));
}
@Test
public void testGreaterEq()
throws FileNotFoundException, IOException, WrongFormatException {
// load Namespace
Namespace namespace = new Namespace(getTestResource("default.ns"), false);
// create TupleStore
TupleStore store =
new TupleStore(false, true, true, 2, 5, 4, 2, namespace,
getTestResource("default.nt"));
int[] args = new int[5];
// create FunctionalOperator
FunctionalOperator fop =
(FunctionalOperator)store.operatorRegistry
.checkAndRegister("de.dfki.lt.hfc.operators.GreaterEqual");
args[0] = store.putObject((new XsdDouble(3.1).toString(true)));
args[1] = store.putObject((new XsdLong(7l)).toString(true));
args[2] = store.putObject((new XsdInt(6)).toString(true));
args[3] = store.putObject((new XsdFloat(5.0f)).toString(true));
args[4] = store.putObject((new XsdDecimal(4.0)).toString(true));
// do operation
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[2]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[4], args[0]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[1]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[3], args[4]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[2], args[1]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[1], args[0]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[2], args[2]}));
}
@Test
public void testEqual()
throws FileNotFoundException, IOException, WrongFormatException {
// load Namespace
Namespace namespace = new Namespace(getTestResource("default.ns"), false);
// create TupleStore
TupleStore store =
new TupleStore(false, true, true, 2, 5, 4, 2, namespace,
getTestResource("default.nt"));
int[] args = new int[5];
// create FunctionalOperator
FunctionalOperator fop =
(FunctionalOperator)store.operatorRegistry
.checkAndRegister("de.dfki.lt.hfc.operators.GreaterEqual");
args[0] = store.putObject((new XsdDouble(3.1).toString(true)));
args[1] = store.putObject((new XsdLong(7l)).toString(true));
args[2] = store.putObject((new XsdInt(6)).toString(true));
args[3] = store.putObject((new XsdFloat(5.0f)).toString(true));
args[4] = store.putObject((new XsdDecimal(4.0)).toString(true));
// do operation
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[2]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[4], args[4]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[4], args[1]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[3], args[3]}));
assertEquals(FunctionalOperator.FALSE, fop.apply(new int[]{args[2], args[1]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[0], args[0]}));
assertEquals(FunctionalOperator.TRUE, fop.apply(new int[]{args[2], args[2]}));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment