Revision: 8441
http://svn.sourceforge.net/foray/?rev=8441&view=rev
Author: victormote
Date: 2006-10-27 14:42:33 -0700 (Fri, 27 Oct 2006)
Log Message:
-----------
Make all of the numeric Functions implement the ExprNumeric interface.
Modified Paths:
--------------
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnAbs.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnCeiling.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnFloor.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMax.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMin.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnRound.java
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnAbs.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnAbs.java 2006-10-27 18:17:38 UTC (rev 8440)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnAbs.java 2006-10-27 21:42:33 UTC (rev 8441)
@@ -34,7 +34,7 @@
/**
* The "abs" function in XSL-FO.
*/
-public class FnAbs extends Function {
+public class FnAbs extends Function implements ExprNumeric {
public FnAbs(final PropertyList propertyList, final Expr[] arguments)
throws PropertyException {
@@ -46,22 +46,47 @@
}
public boolean argumentsValid(final PropertyList propertyList) {
+ /* There must be exactly one argument. */
if (getArgs().length != 1) {
return false;
}
+ /* The argument must be numeric. */
if (! (getArgs()[0] instanceof ExprNumeric)) {
return false;
}
return true;
}
- public ExprNumeric eval(final PropertyList propertyList)
- throws PropertyException {
- double value = ((ExprNumeric) getArgs()[0]).getNumericValue();
- if (value < 0) {
- value *= -1;
- }
- return new DtNumber(value);
+ private ExprNumeric argumentAsNumeric() {
+ return (ExprNumeric) this.getArgs()[0];
}
+ /**
+ * {@inheritDoc}
+ */
+ public double getNumericValue() {
+ return Math.abs(argumentAsNumeric().getNumericValue());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public byte getUnitPower() {
+ return argumentAsNumeric().getUnitPower();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canEvaluateLength() {
+ return argumentAsNumeric().canEvaluateLength();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLength(final int pointSize, final int percentBase) {
+ return Math.abs(argumentAsNumeric().getLength(pointSize, percentBase));
+ }
+
}
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnCeiling.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnCeiling.java 2006-10-27 18:17:38 UTC (rev 8440)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnCeiling.java 2006-10-27 21:42:33 UTC (rev 8441)
@@ -34,7 +34,7 @@
/**
* The "ceiling" function in XSL-FO.
*/
-class FnCeiling extends Function {
+class FnCeiling extends Function implements ExprNumeric {
public FnCeiling(final PropertyList propertyList, final Expr[] arguments)
throws PropertyException {
@@ -46,20 +46,57 @@
}
public boolean argumentsValid(final PropertyList propertyList) {
+ /* There must be exactly one argument. */
if (getArgs().length != 1) {
return false;
}
+ /* The argument must be numeric. */
if (! (getArgs()[0] instanceof ExprNumeric)) {
return false;
}
+ /* The numeric must have a unit power of zero. */
+ if (argumentAsNumeric().getUnitPower() != 0) {
+ return false;
+ }
return true;
}
- public ExprNumeric eval()
- throws PropertyException {
- final ExprNumeric numeric = (ExprNumeric) getArgs()[0];
- final double value = Math.ceil(numeric.getNumericValue());
- return new DtNumber(value);
+ private ExprNumeric argumentAsNumeric() {
+ return (ExprNumeric) this.getArgs()[0];
}
+ /**
+ * {@inheritDoc}
+ */
+ public double getNumericValue() {
+ return Math.ceil(argumentAsNumeric().getNumericValue());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public byte getUnitPower() {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canEvaluateLength() {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLength(final int pointSize, final int percentBase) {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return Integer.MIN_VALUE;
+ }
+
}
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnFloor.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnFloor.java 2006-10-27 18:17:38 UTC (rev 8440)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnFloor.java 2006-10-27 21:42:33 UTC (rev 8441)
@@ -34,7 +34,7 @@
/**
* The "floor" function in XSL-FO.
*/
-class FnFloor extends Function {
+class FnFloor extends Function implements ExprNumeric {
public FnFloor(final PropertyList propertyList, final Expr[] arguments)
throws PropertyException {
@@ -46,19 +46,57 @@
}
public boolean argumentsValid(final PropertyList propertyList) {
+ /* There must be exactly one argument. */
if (getArgs().length != 1) {
return false;
}
+ /* The argument must be numeric. */
if (! (getArgs()[0] instanceof ExprNumeric)) {
return false;
}
+ /* The numeric must have a unit power of zero. */
+ if (argumentAsNumeric().getUnitPower() != 0) {
+ return false;
+ }
return true;
}
- public ExprNumeric eval() throws PropertyException {
- final ExprNumeric numeric = (ExprNumeric) getArgs()[0];
- final double value = Math.floor(numeric.getNumericValue());
- return new DtNumber(value);
+ private ExprNumeric argumentAsNumeric() {
+ return (ExprNumeric) this.getArgs()[0];
}
+ /**
+ * {@inheritDoc}
+ */
+ public double getNumericValue() {
+ return Math.floor(argumentAsNumeric().getNumericValue());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public byte getUnitPower() {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canEvaluateLength() {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLength(final int pointSize, final int percentBase) {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return Integer.MIN_VALUE;
+ }
+
}
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMax.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMax.java 2006-10-27 18:17:38 UTC (rev 8440)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMax.java 2006-10-27 21:42:33 UTC (rev 8441)
@@ -34,7 +34,7 @@
/**
* The "max" function in XSL-FO.
*/
-public class FnMax extends Function {
+public class FnMax extends Function implements ExprNumeric {
public FnMax(final PropertyList propertyList, final Expr[] arguments)
throws PropertyException {
@@ -49,21 +49,70 @@
if (getArgs().length != 2) {
return false;
}
+ /* The arguments must be numeric. */
if (! (getArgs()[0] instanceof ExprNumeric)) {
return false;
}
if (! (getArgs()[1] instanceof ExprNumeric)) {
return false;
}
+ /* The arguments must have the same unit power. */
+ if (argument1AsNumeric().getUnitPower()
+ != argument2AsNumeric().getUnitPower()) {
+ return false;
+ }
return true;
}
- public ExprNumeric eval() throws PropertyException {
- final ExprNumeric numeric1 = (ExprNumeric) getArgs()[0];
- final ExprNumeric numeric2 = (ExprNumeric) getArgs()[1];
- final double value = Math.max(numeric1.getNumericValue(),
- numeric2.getNumericValue());
- return new DtNumber(value);
+ /**
+ * Returns the first argument (index 0) as a Numeric.
+ * @return The first argument as a Numeric.
+ */
+ private ExprNumeric argument1AsNumeric() {
+ return (ExprNumeric) this.getArgs()[0];
}
+ /**
+ * Returns the second argument (index 1) as a Numeric.
+ * @return The second argument as a Numeric.
+ */
+ private ExprNumeric argument2AsNumeric() {
+ return (ExprNumeric) this.getArgs()[1];
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public double getNumericValue() {
+ return Math.max(argument1AsNumeric().getNumericValue(),
+ argument2AsNumeric().getNumericValue());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public byte getUnitPower() {
+ /* The equality of the unit powers of the two arguments is a
+ * requirement, and is tested in the argument validation. */
+ return argument1AsNumeric().getUnitPower();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canEvaluateLength() {
+ /* The equality of the unit powers of the two arguments is a
+ * requirement, and is tested in the argument validation. */
+ return argument1AsNumeric().canEvaluateLength();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLength(final int pointSize, final int percentBase) {
+ return Math.max(argument1AsNumeric().getLength(pointSize, percentBase),
+ argument2AsNumeric().getLength(pointSize, percentBase));
+ }
+
+
}
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMin.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMin.java 2006-10-27 18:17:38 UTC (rev 8440)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnMin.java 2006-10-27 21:42:33 UTC (rev 8441)
@@ -34,7 +34,7 @@
/**
* The "min" function in XSL-FO.
*/
-public class FnMin extends Function {
+public class FnMin extends Function implements ExprNumeric {
public FnMin(final PropertyList propertyList, final Expr[] arguments)
throws PropertyException {
@@ -46,26 +46,73 @@
}
public boolean argumentsValid(final PropertyList propertyList) {
+ /* There must be exactly two arguments. */
if (getArgs().length != 2) {
return false;
}
+ /* The arguments must be numeric. */
if (! (getArgs()[0] instanceof ExprNumeric)) {
return false;
}
if (! (getArgs()[1] instanceof ExprNumeric)) {
return false;
}
+ /* The arguments must have the same unit power. */
+ if (argument1AsNumeric().getUnitPower()
+ != argument2AsNumeric().getUnitPower()) {
+ return false;
+ }
return true;
}
- public ExprNumeric eval(final PropertyList propertyList)
- throws PropertyException {
- //Already validated as existing and as Numeric at parse-time.
- final ExprNumeric numeric1 = (ExprNumeric) getArgs()[0];
- final ExprNumeric numeric2 = (ExprNumeric) getArgs()[1];
- final double value = Math.min(numeric1.getNumericValue(),
- numeric2.getNumericValue());
- return new DtNumber(value);
+ /**
+ * Returns the first argument (index 0) as a Numeric.
+ * @return The first argument as a Numeric.
+ */
+ private ExprNumeric argument1AsNumeric() {
+ return (ExprNumeric) this.getArgs()[0];
}
+ /**
+ * Returns the second argument (index 1) as a Numeric.
+ * @return The second argument as a Numeric.
+ */
+ private ExprNumeric argument2AsNumeric() {
+ return (ExprNumeric) this.getArgs()[1];
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public double getNumericValue() {
+ return Math.min(argument1AsNumeric().getNumericValue(),
+ argument2AsNumeric().getNumericValue());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public byte getUnitPower() {
+ /* The equality of the unit powers of the two arguments is a
+ * requirement, and is tested in the argument validation. */
+ return argument1AsNumeric().getUnitPower();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canEvaluateLength() {
+ /* The equality of the unit powers of the two arguments is a
+ * requirement, and is tested in the argument validation. */
+ return argument1AsNumeric().canEvaluateLength();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLength(final int pointSize, final int percentBase) {
+ return Math.min(argument1AsNumeric().getLength(pointSize, percentBase),
+ argument2AsNumeric().getLength(pointSize, percentBase));
+ }
+
}
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnRound.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnRound.java 2006-10-27 18:17:38 UTC (rev 8440)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/FnRound.java 2006-10-27 21:42:33 UTC (rev 8441)
@@ -34,7 +34,7 @@
/**
* The "round" function in XSL-FO.
*/
-class FnRound extends Function {
+class FnRound extends Function implements ExprNumeric {
public FnRound(final PropertyList propertyList, final Expr[] arguments)
throws PropertyException {
@@ -46,23 +46,57 @@
}
public boolean argumentsValid(final PropertyList propertyList) {
+ /* There must be exactly one argument. */
if (getArgs().length != 1) {
return false;
}
+ /* The argument must be numeric. */
if (! (getArgs()[0] instanceof ExprNumeric)) {
return false;
}
+ /* The numeric must have a unit power of zero. */
+ if (argumentAsNumeric().getUnitPower() != 0) {
+ return false;
+ }
return true;
}
- public ExprNumeric eval() throws PropertyException {
- final PropertyValue pv = getArgs()[0];
- if (pv instanceof DtInteger) {
- return (DtInteger) pv;
- }
- final ExprNumeric numeric = (ExprNumeric) pv;
- final long value = Math.round(numeric.getNumericValue());
- return new DtInteger((int) value);
+ private ExprNumeric argumentAsNumeric() {
+ return (ExprNumeric) this.getArgs()[0];
}
+ /**
+ * {@inheritDoc}
+ */
+ public double getNumericValue() {
+ return Math.round(argumentAsNumeric().getNumericValue());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public byte getUnitPower() {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean canEvaluateLength() {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getLength(final int pointSize, final int percentBase) {
+ /* Unit power of zero is a requirement, and is tested in the argument
+ * validation. */
+ return Integer.MIN_VALUE;
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|