You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RelativeNumericProperty.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fo.expr;
  19. import org.apache.fop.datatypes.Length;
  20. import org.apache.fop.datatypes.Numeric;
  21. import org.apache.fop.datatypes.PercentBaseContext;
  22. import org.apache.fop.fo.properties.Property;
  23. import org.apache.fop.fo.properties.TableColLength;
  24. import org.apache.fop.util.CompareUtil;
  25. /**
  26. * This class represent a node in a property expression tree.
  27. * It is created when an operation involve relative expression and is used
  28. * to delay evaluation of the operation until the time where getNumericValue()
  29. * or getValue() is called.
  30. */
  31. public class RelativeNumericProperty extends Property implements Length {
  32. /** ADDITION */
  33. public static final int ADDITION = 1;
  34. /** SUBTRACTION */
  35. public static final int SUBTRACTION = 2;
  36. /** MULTIPLY */
  37. public static final int MULTIPLY = 3;
  38. /** DIVIDE */
  39. public static final int DIVIDE = 4;
  40. /** MODULO */
  41. public static final int MODULO = 5;
  42. /** NEGATE */
  43. public static final int NEGATE = 6;
  44. /** ABS */
  45. public static final int ABS = 7;
  46. /** MAX */
  47. public static final int MAX = 8;
  48. /** MIN */
  49. public static final int MIN = 9;
  50. // Used in the toString() method, indexed by operation id.
  51. private static String operations = " +-*/%";
  52. /**
  53. * The operation identifier.
  54. */
  55. private int operation;
  56. /**
  57. * The first (or only) operand.
  58. */
  59. private Numeric op1;
  60. /**
  61. * The second operand.
  62. */
  63. private Numeric op2;
  64. /**
  65. * The dimension of the result.
  66. */
  67. private int dimension;
  68. /**
  69. * Constructor for a two argument operation.
  70. * @param operation the operation opcode: ADDITION, SUBTRACTION, ...
  71. * @param op1 the first operand.
  72. * @param op2 the second operand
  73. */
  74. public RelativeNumericProperty(int operation, Numeric op1, Numeric op2) {
  75. this.operation = operation;
  76. this.op1 = op1;
  77. this.op2 = op2;
  78. // Calculate the dimension. We can do now.
  79. switch (operation) {
  80. case MULTIPLY:
  81. dimension = op1.getDimension() + op2.getDimension();
  82. break;
  83. case DIVIDE:
  84. dimension = op1.getDimension() - op2.getDimension();
  85. break;
  86. default:
  87. dimension = op1.getDimension();
  88. }
  89. }
  90. /**
  91. * Constructor for a one argument operation.
  92. * @param operation the operation opcode: NEGATE, ABS
  93. * @param op the operand.
  94. */
  95. public RelativeNumericProperty(int operation, Numeric op) {
  96. this.operation = operation;
  97. this.op1 = op;
  98. this.dimension = op.getDimension();
  99. }
  100. /**
  101. * Return a resolved (calculated) Numeric with the value of the expression.
  102. * @param context Evaluation context
  103. * @return the resolved {@link Numeric} corresponding to the value of the expression
  104. * @throws PropertyException when an exception occur during evaluation.
  105. */
  106. private Numeric getResolved(PercentBaseContext context) throws PropertyException {
  107. switch (operation) {
  108. case ADDITION:
  109. return NumericOp.addition2(op1, op2, context);
  110. case SUBTRACTION:
  111. return NumericOp.subtraction2(op1, op2, context);
  112. case MULTIPLY:
  113. return NumericOp.multiply2(op1, op2, context);
  114. case DIVIDE:
  115. return NumericOp.divide2(op1, op2, context);
  116. case MODULO:
  117. return NumericOp.modulo2(op1, op2, context);
  118. case NEGATE:
  119. return NumericOp.negate2(op1, context);
  120. case ABS:
  121. return NumericOp.abs2(op1, context);
  122. case MAX:
  123. return NumericOp.max2(op1, op2, context);
  124. case MIN:
  125. return NumericOp.min2(op1, op2, context);
  126. default:
  127. throw new PropertyException("Unknown expr operation " + operation);
  128. }
  129. }
  130. /**
  131. * Return the resolved (calculated) value of the expression.
  132. * {@inheritDoc}
  133. */
  134. public double getNumericValue() {
  135. try {
  136. return getResolved(null).getNumericValue(null);
  137. } catch (PropertyException pe) {
  138. throw new RuntimeException(pe);
  139. }
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public double getNumericValue(PercentBaseContext context) {
  145. try {
  146. return getResolved(context).getNumericValue(context);
  147. } catch (PropertyException pe) {
  148. throw new RuntimeException(pe);
  149. }
  150. }
  151. /**
  152. * Return the dimension of the expression
  153. * @return numeric value as dimension
  154. */
  155. public int getDimension() {
  156. return dimension;
  157. }
  158. /**
  159. * Return false since an expression is only created when there is relative
  160. * numerics involved.
  161. * @return true if expression is absolute
  162. */
  163. public boolean isAbsolute() {
  164. return false;
  165. }
  166. /**
  167. * Cast this numeric as a Length.
  168. * @return numeric value as length
  169. */
  170. @Override
  171. public Length getLength() {
  172. if (dimension == 1) {
  173. return this;
  174. }
  175. log.error("Can't create length with dimension " + dimension);
  176. return null;
  177. }
  178. /** @return numeric value */
  179. @Override
  180. public Numeric getNumeric() {
  181. return this;
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public int getValue() {
  187. return (int) getNumericValue();
  188. }
  189. /**
  190. * {@inheritDoc}
  191. */
  192. public int getValue(PercentBaseContext context) {
  193. return (int) getNumericValue(context);
  194. }
  195. /**
  196. * Return the number of table units which are included in this length
  197. * specification. This will always be 0 unless the property specification
  198. * used the proportional-column-width() function (only on table column FOs).
  199. * <p>
  200. * If this value is not 0, the actual value of the Length cannot be known
  201. * without looking at all of the columns in the table to determine the value
  202. * of a "table-unit".
  203. *
  204. * @return The number of table units which are included in this length
  205. * specification.
  206. */
  207. public double getTableUnits() {
  208. double tu1 = 0.0;
  209. double tu2 = 0.0;
  210. if (op1 instanceof RelativeNumericProperty) {
  211. tu1 = ((RelativeNumericProperty) op1).getTableUnits();
  212. } else if (op1 instanceof TableColLength) {
  213. tu1 = ((TableColLength) op1).getTableUnits();
  214. }
  215. if (op2 instanceof RelativeNumericProperty) {
  216. tu2 = ((RelativeNumericProperty) op2).getTableUnits();
  217. } else if (op2 instanceof TableColLength) {
  218. tu2 = ((TableColLength) op2).getTableUnits();
  219. }
  220. if (tu1 != 0.0 && tu2 != 0.0) {
  221. switch (operation) {
  222. case ADDITION:
  223. return tu1 + tu2;
  224. case SUBTRACTION:
  225. return tu1 - tu2;
  226. case MULTIPLY:
  227. return tu1 * tu2;
  228. case DIVIDE:
  229. return tu1 / tu2;
  230. case MODULO:
  231. return tu1 % tu2;
  232. case MIN:
  233. return Math.min(tu1, tu2);
  234. case MAX:
  235. return Math.max(tu1, tu2);
  236. default:
  237. assert false;
  238. }
  239. } else if (tu1 != 0.0) {
  240. switch (operation) {
  241. case NEGATE:
  242. return -tu1;
  243. case ABS:
  244. return Math.abs(tu1);
  245. default:
  246. return tu1;
  247. }
  248. } else if (tu2 != 0.0) {
  249. return tu2;
  250. }
  251. return 0.0;
  252. }
  253. /**
  254. * Return a string represention of the expression. Only used for debugging.
  255. * @return the string representation.
  256. */
  257. @Override
  258. public String toString() {
  259. switch (operation) {
  260. case ADDITION: case SUBTRACTION:
  261. case DIVIDE: case MULTIPLY: case MODULO:
  262. return "(" + op1 + " " + operations.charAt(operation) + op2 + ")";
  263. case NEGATE:
  264. return "-" + op1;
  265. case MAX:
  266. return "max(" + op1 + ", " + op2 + ")";
  267. case MIN:
  268. return "min(" + op1 + ", " + op2 + ")";
  269. case ABS:
  270. return "abs(" + op1 + ")";
  271. default:
  272. return "unknown operation " + operation;
  273. }
  274. }
  275. @Override
  276. public int hashCode() {
  277. final int prime = 31;
  278. int result = 1;
  279. result = prime * result + dimension;
  280. result = prime * result + CompareUtil.getHashCode(op1);
  281. result = prime * result + CompareUtil.getHashCode(op2);
  282. result = prime * result + operation;
  283. return result;
  284. }
  285. @Override
  286. public boolean equals(Object obj) {
  287. if (this == obj) {
  288. return true;
  289. }
  290. if (!(obj instanceof RelativeNumericProperty)) {
  291. return false;
  292. }
  293. RelativeNumericProperty other = (RelativeNumericProperty) obj;
  294. return dimension == other.dimension
  295. && CompareUtil.equal(op1, other.op1)
  296. && CompareUtil.equal(op2, other.op2)
  297. && operation == other.operation;
  298. }
  299. }