KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > MathHelper


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util;
25
26 import java.text.DecimalFormat JavaDoc;
27 import java.text.NumberFormat JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Random JavaDoc;
30
31 /**
32  * @author ss
33  *
34  * Various mathematical functions accesible to templateEditors
35  */

36
37 public class MathHelper
38 {
39     
40     /* Convert a hex string to a decimal value, useful
41      * for color specifications.
42      */

43     public int hexToDecimal(String JavaDoc hex)
44     {
45         return new Integer JavaDoc(Integer.parseInt(hex,16)).intValue();
46     }
47     
48     /*
49      * Should be better off in the visual formatter class (ss)
50      */

51
52     public String JavaDoc fileSize(long size)
53     {
54         return fileSize(size, Locale.getDefault());
55     }
56
57     public String JavaDoc fileSize(long size, Locale JavaDoc locale)
58     {
59         String JavaDoc[] pfix = { "byte", "Kb", "Mb", "Gb" };
60         double c = new Long JavaDoc(size).doubleValue();
61         int cnt=0;
62         
63         while (c > 512 && cnt < 3) {
64             c /= 1024; cnt++;
65         }
66         
67         NumberFormat JavaDoc nf = NumberFormat.getInstance(locale);
68         if(c > 9) nf.setMaximumFractionDigits(0); else nf.setMaximumFractionDigits(2);
69          
70         return nf.format(c) + " " + pfix[cnt];
71     }
72     
73     /* Convert a string value to a Integer object
74      * Useful in template when comparing numbers that
75      * resides in strings.
76      */

77     public Integer JavaDoc stringToInteger(String JavaDoc value)
78     {
79         if(value == null)
80             return null;
81         
82         return new Integer JavaDoc(Integer.parseInt(value));
83     }
84     
85     /* Convert a string value to a Integer object
86      * Useful in template when comparing numbers that
87      * resides in strings.
88      */

89     public Integer JavaDoc floatStringToInteger(String JavaDoc value)
90     {
91         if(value == null)
92             return null;
93
94         return new Integer JavaDoc(new Float JavaDoc(value).intValue());
95     }
96
97     /* Convert a string value to a Float object
98      * Useful in template when comparing numbers that
99      * resides in strings.
100      */

101     public Float JavaDoc floatStringToFloat(String JavaDoc value)
102     {
103         if(value == null)
104             return null;
105
106         return new Float JavaDoc(value);
107     }
108     
109     /**
110      * This method returns a string represented a formatted float value.
111      * The formatting is controlled with the locale and the pattern sent in.
112      */

113     
114     public String JavaDoc getNumberAsString(Object JavaDoc value, Locale JavaDoc locale, String JavaDoc pattern)
115     {
116         NumberFormat JavaDoc nf = NumberFormat.getNumberInstance(locale);
117         DecimalFormat JavaDoc df = (DecimalFormat JavaDoc)nf;
118         df.applyPattern(pattern);
119         String JavaDoc output = df.format(value);
120         return output;
121     }
122     
123     /**
124      * This method returns a string represented a formatted float value.
125      * The formatting is controlled with the pattern sent in.
126      */

127     
128     public String JavaDoc getNumberAsString(Object JavaDoc value, String JavaDoc pattern)
129     {
130         NumberFormat JavaDoc nf = NumberFormat.getNumberInstance();
131         DecimalFormat JavaDoc df = (DecimalFormat JavaDoc)nf;
132         df.applyPattern(pattern);
133         String JavaDoc output = df.format(value);
134         return output;
135     }
136
137     /*
138      * Divides to values
139      */

140
141     public Float JavaDoc divide(Float JavaDoc value, int divider)
142     {
143         if(value == null)
144             return null;
145
146         return new Float JavaDoc(value.floatValue() / divider);
147     }
148     
149     
150     /**
151      * Gets a random number.
152      */

153     
154     public int getRandom()
155     {
156         Random JavaDoc generator = new Random JavaDoc();
157         return generator.nextInt();
158     }
159
160     /**
161      * Gets a random number with an upper limit.
162      */

163     
164     public int getRandom(int upperLimit)
165     {
166         Random JavaDoc generator = new Random JavaDoc();
167         return generator.nextInt(upperLimit);
168     }
169     
170     /**
171      * This method rounds an float to an long.
172      */

173     
174     public int round(float floatNumber)
175     {
176         return (int)Math.round(floatNumber);
177     }
178
179
180     /**
181      * This method multiplies two numbers.
182      */

183     
184     public float multiply(Number JavaDoc first, Number JavaDoc second)
185     {
186         return first.floatValue() * second.floatValue();
187     }
188     
189     /**
190      * This method multiplies two floats.
191      */

192     
193     public float multiply(float first, float second)
194     {
195         return first * second;
196     }
197     
198     /**
199      * This method divides two numbers.
200      */

201     
202     public float divide(Number JavaDoc first, Number JavaDoc second)
203     {
204         return first.floatValue() / second.floatValue();
205     }
206     
207     /**
208      * This method divides two numbers.
209      */

210     
211     public float divide(float first, float second)
212     {
213         return first / second;
214     }
215 }
216
Popular Tags