1 17 18 19 20 package org.apache.fop.fo.expr; 21 22 import org.apache.fop.datatypes.PercentBaseContext; 23 import org.apache.fop.datatypes.Numeric; 24 25 33 public class NumericOp { 34 42 public static Numeric addition(Numeric op1, Numeric op2) throws PropertyException { 43 if (op1.isAbsolute() && op2.isAbsolute()) { 44 return addition2(op1, op2, null); 45 } else { 46 return new RelativeNumericProperty(RelativeNumericProperty.ADDITION, op1, op2); 47 } 48 } 49 50 public static Numeric addition2(Numeric op1, Numeric op2, PercentBaseContext context) throws PropertyException { 51 if (op1.getDimension() != op2.getDimension()) { 52 throw new PropertyException("Can't subtract Numerics of different dimensions"); 53 } 54 return numeric(op1.getNumericValue(context) + op2.getNumericValue(context), op1.getDimension()); 55 } 56 57 66 public static Numeric subtraction(Numeric op1, Numeric op2) throws PropertyException { 67 if (op1.isAbsolute() && op2.isAbsolute()) { 68 return subtraction2(op1, op2, null); 69 } else { 70 return new RelativeNumericProperty(RelativeNumericProperty.SUBTRACTION, op1, op2); 71 } 72 } 73 74 public static Numeric subtraction2(Numeric op1, Numeric op2, PercentBaseContext context) throws PropertyException { 75 if (op1.getDimension() != op2.getDimension()) { 76 throw new PropertyException("Can't subtract Numerics of different dimensions"); 77 } 78 return numeric(op1.getNumericValue(context) - op2.getNumericValue(context), op1.getDimension()); 79 } 80 81 90 public static Numeric multiply(Numeric op1, Numeric op2) throws PropertyException { 91 if (op1.isAbsolute() && op2.isAbsolute()) { 92 return multiply2(op1, op2, null); 93 } else { 94 return new RelativeNumericProperty(RelativeNumericProperty.MULTIPLY, op1, op2); 95 } 96 } 97 98 public static Numeric multiply2(Numeric op1, Numeric op2, PercentBaseContext context) throws PropertyException { 99 return numeric(op1.getNumericValue(context) * op2.getNumericValue(context), 100 op1.getDimension() + op2.getDimension()); 101 } 102 103 113 public static Numeric divide(Numeric op1, Numeric op2) throws PropertyException { 114 if (op1.isAbsolute() && op2.isAbsolute()) { 115 return divide2(op1, op2, null); 116 } else { 117 return new RelativeNumericProperty(RelativeNumericProperty.DIVIDE, op1, op2); 118 } 119 } 120 121 public static Numeric divide2(Numeric op1, Numeric op2, PercentBaseContext context) throws PropertyException { 122 return numeric(op1.getNumericValue(context) / op2.getNumericValue(context), 123 op1.getDimension() - op2.getDimension()); 124 } 125 126 132 public static Numeric modulo(Numeric op1, Numeric op2) throws PropertyException { 133 if (op1.isAbsolute() && op2.isAbsolute()) { 134 return modulo2(op1, op2, null); 135 } else { 136 return new RelativeNumericProperty(RelativeNumericProperty.MODULO, op1, op2); 137 } 138 } 139 140 public static Numeric modulo2(Numeric op1, Numeric op2, PercentBaseContext context) throws PropertyException { 141 return numeric(op1.getNumericValue(context) % op2.getNumericValue(context), op1.getDimension()); 142 } 143 144 149 public static Numeric abs(Numeric op) throws PropertyException { 150 if (op.isAbsolute()) { 151 return abs2(op, null); 152 } else { 153 return new RelativeNumericProperty(RelativeNumericProperty.ABS, op); 154 } 155 } 156 157 public static Numeric abs2(Numeric op, PercentBaseContext context) throws PropertyException { 158 return numeric(Math.abs(op.getNumericValue(context)), op.getDimension()); 159 } 160 161 166 public static Numeric negate(Numeric op) throws PropertyException { 167 if (op.isAbsolute()) { 168 return negate2(op, null); 169 } else { 170 return new RelativeNumericProperty(RelativeNumericProperty.NEGATE, op); 171 } 172 } 173 174 public static Numeric negate2(Numeric op, PercentBaseContext context) throws PropertyException { 175 return numeric(- op.getNumericValue(context), op.getDimension()); 176 } 177 178 185 public static Numeric max(Numeric op1, Numeric op2) throws PropertyException { 186 if (op1.isAbsolute() && op2.isAbsolute()) { 187 return max2(op1, op2, null); 188 } else { 189 return new RelativeNumericProperty(RelativeNumericProperty.MAX, op1, op2); 190 } 191 } 192 193 public static Numeric max2(Numeric op1, Numeric op2, PercentBaseContext context) throws PropertyException { 194 if (op1.getDimension() != op2.getDimension()) { 195 throw new PropertyException("Arguments to max() must have same dimensions"); 196 } 197 return op1.getNumericValue(context) > op2.getNumericValue(context) ? op1 : op2; 198 } 199 200 207 public static Numeric min(Numeric op1, Numeric op2) throws PropertyException { 208 if (op1.isAbsolute() && op2.isAbsolute()) { 209 return min2(op1, op2, null); 210 } else { 211 return new RelativeNumericProperty(RelativeNumericProperty.MIN, op1, op2); 212 } 213 } 214 215 public static Numeric min2(Numeric op1, Numeric op2, PercentBaseContext context) throws PropertyException { 216 if (op1.getDimension() != op2.getDimension()) { 217 throw new PropertyException("Arguments to min() must have same dimensions"); 218 } 219 return op1.getNumericValue(context) <= op2.getNumericValue(context) ? op1 : op2; 220 } 221 222 228 private static Numeric numeric(double value, int dimension) { 229 return new NumericProperty(value, dimension); 230 } 231 } 232 | Popular Tags |