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.

NumericOp.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.Numeric;
  20. import org.apache.fop.datatypes.PercentBaseContext;
  21. /**
  22. * This class contains static methods to evaluate operations on Numeric
  23. * operands. If the operands are absolute numerics the result is computed
  24. * rigth away and a new absolute numeric is return. If one of the operands are
  25. * relative a n operation node is created with the operation and the operands.
  26. * The evaluation of the operation can then occur when getNumericValue() is
  27. * called.
  28. */
  29. public final class NumericOp {
  30. private NumericOp() {
  31. }
  32. /**
  33. * Add the two operands and return a new Numeric representing the result.
  34. * @param op1 The first operand.
  35. * @param op2 The second operand.
  36. * @return A Numeric representing the result.
  37. * @throws PropertyException If the dimension of the operand is different
  38. * from the dimension of this Numeric.
  39. */
  40. public static Numeric addition(Numeric op1, Numeric op2) throws PropertyException {
  41. if (op1.isAbsolute() && op2.isAbsolute()) {
  42. return addition2(op1, op2, null);
  43. } else {
  44. return new RelativeNumericProperty(RelativeNumericProperty.ADDITION, op1, op2);
  45. }
  46. }
  47. /**
  48. * Add the two operands with a percentage context
  49. * and return a new Numeric representing the result.
  50. * @param op1 The first operand.
  51. * @param op2 The second operand.
  52. * @param context a percent base context
  53. * @return A Numeric representing the result.
  54. * @throws PropertyException If the dimension of the operand is different
  55. * from the dimension of this Numeric.
  56. */
  57. public static Numeric addition2(Numeric op1, Numeric op2, PercentBaseContext context)
  58. throws PropertyException {
  59. if (op1.getDimension() != op2.getDimension()) {
  60. throw new PropertyException("Can't subtract Numerics of different dimensions");
  61. }
  62. return numeric(op1.getNumericValue(context)
  63. + op2.getNumericValue(context), op1.getDimension());
  64. }
  65. /**
  66. * Add the second operand from the first and return a new Numeric
  67. * representing the result.
  68. * @param op1 The first operand.
  69. * @param op2 The second operand.
  70. * @return A Numeric representing the result.
  71. * @throws PropertyException If the dimension of the operand is different
  72. * from the dimension of this Numeric.
  73. */
  74. public static Numeric subtraction(Numeric op1, Numeric op2) throws PropertyException {
  75. if (op1.isAbsolute() && op2.isAbsolute()) {
  76. return subtraction2(op1, op2, null);
  77. } else {
  78. return new RelativeNumericProperty(RelativeNumericProperty.SUBTRACTION, op1, op2);
  79. }
  80. }
  81. /**
  82. * Subtract the two operands with a percentage context
  83. * and return a new Numeric representing the result.
  84. * @param op1 The first operand.
  85. * @param op2 The second operand.
  86. * @param context a percent base context
  87. * @return A Numeric representing the result.
  88. * @throws PropertyException If the dimension of the operand is different
  89. * from the dimension of this Numeric.
  90. */
  91. public static Numeric subtraction2(Numeric op1, Numeric op2, PercentBaseContext context)
  92. throws PropertyException {
  93. if (op1.getDimension() != op2.getDimension()) {
  94. throw new PropertyException("Can't subtract Numerics of different dimensions");
  95. }
  96. return numeric(op1.getNumericValue(context)
  97. - op2.getNumericValue(context), op1.getDimension());
  98. }
  99. /**
  100. * Multiply the two operands and return a new Numeric representing the
  101. * result.
  102. * @param op1 The first operand.
  103. * @param op2 The second operand.
  104. * @return A Numeric representing the result.
  105. * @throws PropertyException If the dimension of the operand is different
  106. * from the dimension of this Numeric.
  107. */
  108. public static Numeric multiply(Numeric op1, Numeric op2) throws PropertyException {
  109. if (op1.isAbsolute() && op2.isAbsolute()) {
  110. return multiply2(op1, op2, null);
  111. } else {
  112. return new RelativeNumericProperty(RelativeNumericProperty.MULTIPLY, op1, op2);
  113. }
  114. }
  115. /**
  116. * Multiply the two operands with a percentage context
  117. * and return a new Numeric representing the result.
  118. * @param op1 The first operand.
  119. * @param op2 The second operand.
  120. * @param context a percent base context
  121. * @return A Numeric representing the result.
  122. * @throws PropertyException If the dimension of the operand is different
  123. * from the dimension of this Numeric.
  124. */
  125. public static Numeric multiply2(Numeric op1, Numeric op2, PercentBaseContext context)
  126. throws PropertyException {
  127. return numeric(op1.getNumericValue(context) * op2.getNumericValue(context),
  128. op1.getDimension() + op2.getDimension());
  129. }
  130. /**
  131. * Divide the second operand into the first and return a new
  132. * Numeric representing the
  133. * result.
  134. * @param op1 The first operand.
  135. * @param op2 The second operand.
  136. * @return A Numeric representing the result.
  137. * @throws PropertyException If the dimension of the operand is different
  138. * from the dimension of this Numeric.
  139. */
  140. public static Numeric divide(Numeric op1, Numeric op2) throws PropertyException {
  141. if (op1.isAbsolute() && op2.isAbsolute()) {
  142. return divide2(op1, op2, null);
  143. } else {
  144. return new RelativeNumericProperty(RelativeNumericProperty.DIVIDE, op1, op2);
  145. }
  146. }
  147. /**
  148. * Divide the two operands with a percentage context
  149. * and return a new Numeric representing the result.
  150. * @param op1 The first operand.
  151. * @param op2 The second operand.
  152. * @param context a percent base context
  153. * @return A Numeric representing the result.
  154. * @throws PropertyException If the dimension of the operand is different
  155. * from the dimension of this Numeric.
  156. */
  157. public static Numeric divide2(Numeric op1, Numeric op2, PercentBaseContext context)
  158. throws PropertyException {
  159. return numeric(op1.getNumericValue(context) / op2.getNumericValue(context),
  160. op1.getDimension() - op2.getDimension());
  161. }
  162. /**
  163. * Return the remainder of a division of the two operand Numeric.
  164. * @param op1 The first operand.
  165. * @param op2 The second operand.
  166. * @return A new Numeric object representing the absolute value.
  167. * @throws PropertyException if a property exception occurs
  168. */
  169. public static Numeric modulo(Numeric op1, Numeric op2) throws PropertyException {
  170. if (op1.isAbsolute() && op2.isAbsolute()) {
  171. return modulo2(op1, op2, null);
  172. } else {
  173. return new RelativeNumericProperty(RelativeNumericProperty.MODULO, op1, op2);
  174. }
  175. }
  176. /**
  177. * Return the remainder of a division of the two operand Numeric.
  178. * @param op1 The first operand.
  179. * @param op2 The second operand.
  180. * @param context a percent base context
  181. * @return A Numeric representing the result.
  182. * @throws PropertyException If the dimension of the operand is different
  183. * from the dimension of this Numeric.
  184. */
  185. public static Numeric modulo2(Numeric op1, Numeric op2, PercentBaseContext context)
  186. throws PropertyException {
  187. return numeric(op1.getNumericValue(context)
  188. % op2.getNumericValue(context), op1.getDimension());
  189. }
  190. /**
  191. * Return the absolute value of a Numeric.
  192. * @param op the operand.
  193. * @return a new Numeric object representing the absolute value of the operand.
  194. * @throws PropertyException if a property exception occurs
  195. */
  196. public static Numeric abs(Numeric op) throws PropertyException {
  197. if (op.isAbsolute()) {
  198. return abs2(op, null);
  199. } else {
  200. return new RelativeNumericProperty(RelativeNumericProperty.ABS, op);
  201. }
  202. }
  203. /**
  204. * Return the absolute value of a Numeric.
  205. * @param op the operand.
  206. * @param context a percent base context
  207. * @return A Numeric representing the result.
  208. * @throws PropertyException If the dimension of the operand is different
  209. * from the dimension of this Numeric.
  210. */
  211. public static Numeric abs2(Numeric op, PercentBaseContext context) throws PropertyException {
  212. return numeric(Math.abs(op.getNumericValue(context)), op.getDimension());
  213. }
  214. /**
  215. * Return the negation of a Numeric.
  216. * @param op the operand.
  217. * @return a new Numeric object representing the negation of the operand.
  218. * @throws PropertyException if a property exception occurs
  219. */
  220. public static Numeric negate(Numeric op) throws PropertyException {
  221. if (op.isAbsolute()) {
  222. return negate2(op, null);
  223. } else {
  224. return new RelativeNumericProperty(RelativeNumericProperty.NEGATE, op);
  225. }
  226. }
  227. /**
  228. * Return the negation of a Numeric.
  229. * @param op the operand.
  230. * @param context a percent base context
  231. * @return A Numeric representing the result.
  232. * @throws PropertyException If the dimension of the operand is different
  233. * from the dimension of this Numeric.
  234. */
  235. public static Numeric negate2(Numeric op, PercentBaseContext context) throws PropertyException {
  236. return numeric(-op.getNumericValue(context), op.getDimension());
  237. }
  238. /**
  239. * Return the larger of the two Numerics.
  240. * @param op1 The first operand.
  241. * @param op2 The second operand.
  242. * @return a Numeric which is the maximum of the two operands.
  243. * @throws PropertyException if the dimensions or value types of the operands are different.
  244. */
  245. public static Numeric max(Numeric op1, Numeric op2) throws PropertyException {
  246. if (op1.isAbsolute() && op2.isAbsolute()) {
  247. return max2(op1, op2, null);
  248. } else {
  249. return new RelativeNumericProperty(RelativeNumericProperty.MAX, op1, op2);
  250. }
  251. }
  252. /**
  253. * Return the larger of the two Numerics.
  254. * @param op1 The first operand.
  255. * @param op2 The second operand.
  256. * @param context a percent base context
  257. * @return A Numeric representing the result.
  258. * @throws PropertyException If the dimension of the operand is different
  259. * from the dimension of this Numeric.
  260. */
  261. public static Numeric max2(Numeric op1, Numeric op2, PercentBaseContext context)
  262. throws PropertyException {
  263. if (op1.getDimension() != op2.getDimension()) {
  264. throw new PropertyException("Arguments to max() must have same dimensions");
  265. }
  266. return op1.getNumericValue(context) > op2.getNumericValue(context) ? op1 : op2;
  267. }
  268. /**
  269. * Return the smaller of two Numerics.
  270. * @param op1 The first operand.
  271. * @param op2 The second operand.
  272. * @return a Numeric which is the minimum of the two operands.
  273. * @throws PropertyException if the dimensions or value types of the operands are different.
  274. */
  275. public static Numeric min(Numeric op1, Numeric op2) throws PropertyException {
  276. if (op1.isAbsolute() && op2.isAbsolute()) {
  277. return min2(op1, op2, null);
  278. } else {
  279. return new RelativeNumericProperty(RelativeNumericProperty.MIN, op1, op2);
  280. }
  281. }
  282. /**
  283. * Return the smaller of the two Numerics.
  284. * @param op1 The first operand.
  285. * @param op2 The second operand.
  286. * @param context a percent base context
  287. * @return A Numeric representing the result.
  288. * @throws PropertyException If the dimension of the operand is different
  289. * from the dimension of this Numeric.
  290. */
  291. public static Numeric min2(Numeric op1, Numeric op2, PercentBaseContext context)
  292. throws PropertyException {
  293. if (op1.getDimension() != op2.getDimension()) {
  294. throw new PropertyException("Arguments to min() must have same dimensions");
  295. }
  296. return op1.getNumericValue(context) <= op2.getNumericValue(context) ? op1 : op2;
  297. }
  298. /**
  299. * Create a new absolute numeric with the specified value and dimension.
  300. * @param value of numeric
  301. * @param dimension of numeric
  302. * @return a new absolute numeric.
  303. */
  304. private static Numeric numeric(double value, int dimension) {
  305. return new NumericProperty(value, dimension);
  306. }
  307. }