KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fo > expr > NumericOp


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
18 /* $Id: NumericOp.java 426576 2006-07-28 15:44:37Z jeremias $ */
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 /**
26  * This class contains static methods to evaluate operations on Numeric
27  * operands. If the operands are absolute numerics the result is computed
28  * rigth away and a new absolute numeric is return. If one of the operands are
29  * relative a n operation node is created with the operation and the operands.
30  * The evaluation of the operation can then occur when getNumericValue() is
31  * called.
32  */

33 public class NumericOp {
34     /**
35      * Add the two operands and return a new Numeric representing the result.
36      * @param op1 The first operand.
37      * @param op2 The second operand.
38      * @return A Numeric representing the result.
39      * @throws PropertyException If the dimension of the operand is different
40      * from the dimension of this Numeric.
41      */

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     /**
58      * Add the second operand from the first and return a new Numeric
59      * representing the result.
60      * @param op1 The first operand.
61      * @param op2 The second operand.
62      * @return A Numeric representing the result.
63      * @throws PropertyException If the dimension of the operand is different
64      * from the dimension of this Numeric.
65      */

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     /**
82      * Multiply the two operands and return a new Numeric representing the
83      * result.
84      * @param op1 The first operand.
85      * @param op2 The second operand.
86      * @return A Numeric representing the result.
87      * @throws PropertyException If the dimension of the operand is different
88      * from the dimension of this Numeric.
89      */

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     /**
104      * Divide the second operand into the first and return a new
105      * Numeric representing the
106      * result.
107      * @param op1 The first operand.
108      * @param op2 The second operand.
109      * @return A Numeric representing the result.
110      * @throws PropertyException If the dimension of the operand is different
111      * from the dimension of this Numeric.
112      */

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     /**
127      * Return the remainder of a division of the two operand Numeric.
128      * @param op1 The first operand.
129      * @param op2 The second operand.
130      * @return A new Numeric object representing the absolute value.
131      */

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     /**
145      * Return the absolute value of a Numeric.
146      * @param op the operand.
147      * @return a new Numeric object representing the absolute value of the operand.
148      */

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     /**
162      * Return the negation of a Numeric.
163      * @param op the operand.
164      * @return a new Numeric object representing the negation of the operand.
165      */

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     /**
179      * Return the larger of the two Numerics.
180      * @param op1 The first operand.
181      * @param op2 The second operand.
182      * @return a Numeric which is the maximum of the two operands.
183      * @throws PropertyException if the dimensions or value types of the operands are different.
184      */

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     /**
201      * Return the smaller of two Numerics.
202      * @param op1 The first operand.
203      * @param op2 The second operand.
204      * @return a Numeric which is the minimum of the two operands.
205      * @throws PropertyException if the dimensions or value types of the operands are different.
206      */

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     /**
223      * Create a new absolute numeric with the specified value and dimension.
224      * @param value
225      * @param dimension
226      * @return a new absolute numeric.
227      */

228     private static Numeric numeric(double value, int dimension) {
229         return new NumericProperty(value, dimension);
230     }
231 }
232
Popular Tags