KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > txt > Helper


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: Helper.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.txt;
21
22 /**
23  * This class has a few convenient static methods for number quantization.
24  */

25 public final class Helper {
26
27     /**
28      * Don't let anyone instantiate this class.
29      */

30     private Helper() { }
31
32     /**
33      * Returns nearest integer to <code>x</code>, divisible by
34      * <code>quantum</code>.
35      *
36      * @param x integer for quantization
37      * @param quantum integer, representing quantization
38      * @return computed nearest integer
39      */

40     public static int round(int x, int quantum) {
41         int ceil = ceil(x, quantum);
42         int floor = floor(x, quantum);
43         return (ceil - x < x - floor) ? ceil : floor;
44     }
45
46     /**
47      * Returns minimal possible integer, greater or equal than
48      * <code>x</code>, divisible by <code>quantum</code>.
49      *
50      * @param x integer for quantization
51      * @param quantum integer, representing quantization
52      * @return computed nearest integer
53      */

54     public static int ceil(int x, int quantum) {
55         int dx = (x < 0) || (x % quantum == 0) ? 0 : 1;
56         return (x / quantum + dx) * quantum;
57     }
58
59     /**
60      * Returns maximum possible integer, less or equal than
61      * <code>oldValue</code>, divisible by <code>quantum</code>.
62      *
63      * @param x integer for quantization
64      * @param quantum integer, representing quantization
65      * @return computed nearest integer
66      */

67     public static int floor(int x, int quantum) {
68         int dx = (x > 0) || (x % quantum == 0) ? 0 : -1;
69         return (x / quantum + dx) * quantum;
70     }
71
72     /**
73      * Returns the closest integer to <code>x/y</code> fraction.
74      * It's possible to consider this methos as a analog of Math.round(x/y),
75      * without having deal with non-integer.
76      *
77      * @param x integer, fraction numerator
78      * @param y integer, fraction denominator
79      * @return the value of the fraction rounded to the nearest
80      * @see java.lang.Math#round(double)
81      */

82     public static int roundPosition(int x, int y) {
83         return round(x, y) / y;
84     }
85
86     /**
87      * Returns the smallest integer that is greater than or equal to the
88      * <code>x/y</code> fraction.
89      * It's possible to consider this function as a analog of Math.ceil(x/y),
90      * without having deal with non-integer.
91      *
92      * @param x integer, fraction numerator
93      * @param y integer, fraction denominator
94      * @return the smallest integer that is greater than or equal to
95      * <code>x/y</code> fraction
96      * @see java.lang.Math#ceil(double)
97      */

98     public static int ceilPosition(int x, int y) {
99         return ceil(x, y) / y;
100     }
101     
102     
103     /**
104      * Returns the largest integer that is less than or equal to the
105      * argument and is equal to <code>x/y</code> fraction.
106      * It's possible to consider this function as a analog of Math.floor(x/y),
107      * without having deal with non-integer.
108      *
109      * @param x integer, fraction numerator
110      * @param y integer, fraction denominator
111      * @return the largest integer that is less than or equal to
112      * the argument and is equal to <code>x/y</code> fraction
113      * @see java.lang.Math#ceil(double)
114      */

115     public static int floorPosition(int x, int y) {
116         return floor(x, y) / y;
117     }
118 }
119
Popular Tags