KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > util > NumericUtils


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.util;
56
57 import java.util.HashMap JavaDoc;
58 import java.util.Locale JavaDoc;
59 import java.text.ParseException JavaDoc;
60 import java.text.DecimalFormat JavaDoc;
61
62 /**
63  * command number formatting utilities
64  *
65  * @author J R Briggs
66  */

67 public final class NumericUtils implements Constants {
68   private static final String JavaDoc CLASSNAME = NumericUtils.class.getName();
69   private static final String JavaDoc DIGITS = "0123456789.";
70   
71   private static HashMap JavaDoc numberFormatters = new HashMap JavaDoc();
72   
73   private NumericUtils() { }
74
75  /**
76   * loads and caches a numberformat
77   */

78   private static final DecimalFormat JavaDoc getFormat(String JavaDoc className, Locale JavaDoc locale) {
79     DecimalFormat JavaDoc format;
80     String JavaDoc key = className + UNDERSCORE + UNDERSCORE + (locale != null ? locale.toString() : EMPTY);
81     if (!numberFormatters.containsKey(key)) {
82       if (locale != null) {
83         format = (DecimalFormat JavaDoc)DecimalFormat.getInstance(locale);
84       }
85       else {
86         format = (DecimalFormat JavaDoc)DecimalFormat.getInstance();
87       }
88
89       synchronized (numberFormatters) {
90         if (!numberFormatters.containsKey(key)) {
91           numberFormatters.put(key, format);
92         }
93       }
94     }
95     else {
96       format = (DecimalFormat JavaDoc)numberFormatters.get(key);
97     }
98
99     return format;
100   }
101
102  /**
103   *
104   */

105   public static final String JavaDoc format(String JavaDoc callerClassName, long l, String JavaDoc pattern) {
106     return format(callerClassName, l, null, pattern);
107   }
108
109   public static final String JavaDoc format(String JavaDoc callerClassName, float f, String JavaDoc pattern) {
110     return format(callerClassName, f, null, pattern);
111   }
112   
113   public static final String JavaDoc format(String JavaDoc callerClassName, double d, String JavaDoc pattern) {
114     return format(callerClassName, d, null, pattern);
115   }
116
117   public static final String JavaDoc format(String JavaDoc callerClassName, long l, Locale JavaDoc locale, String JavaDoc pattern) {
118     DecimalFormat JavaDoc format = getFormat(callerClassName, locale);
119     synchronized (format) {
120       format.applyPattern(pattern);
121       return format.format(l);
122     }
123   }
124   
125   public static final String JavaDoc format(String JavaDoc callerClassName, float f, Locale JavaDoc locale, String JavaDoc pattern) {
126     DecimalFormat JavaDoc format = getFormat(callerClassName, locale);
127     synchronized (format) {
128       format.applyPattern(pattern);
129       return format.format(f);
130     }
131   }
132   
133  /**
134   */

135   public static final String JavaDoc format(String JavaDoc callerClassName, double d, Locale JavaDoc locale, String JavaDoc pattern) {
136     DecimalFormat JavaDoc format = getFormat(callerClassName, locale);
137     synchronized (format) {
138       format.applyPattern(pattern);
139       return format.format(d);
140     }
141   }
142   
143   public static final boolean isNumber(String JavaDoc s) {
144     if (StringUtils.isEmpty(s)) {
145       return false;
146     }
147     else {
148       for (int i = 0; i < s.length(); i++) {
149         char c = s.charAt(i);
150         if (DIGITS.indexOf(c) < 0) {
151           return false;
152         }
153       }
154       return true;
155     }
156   }
157
158  /**
159   */

160   public static final double parseDouble(String JavaDoc callerClassName, String JavaDoc s, String JavaDoc pattern) throws ParseException JavaDoc {
161     return parseDouble(callerClassName, s, null, pattern);
162   }
163
164  /**
165   * turns a string representation of a date into a date object using a cached format
166   * @param callerClassName the class of the object calling this method (used for caching)
167   * @param s the string date to convert
168   * @param locale the locale to use in conversion
169   * @param pattern the date format to use
170   */

171   public static final double parseDouble(String JavaDoc callerClassName, String JavaDoc s, Locale JavaDoc locale, String JavaDoc pattern) throws ParseException JavaDoc {
172     if (StringUtils.isEmpty(s)) {
173       return 0.0d;
174     }
175
176     DecimalFormat JavaDoc format = getFormat(callerClassName, locale);
177     synchronized (format) {
178       format.applyPattern(pattern);
179       Number JavaDoc n = format.parse(s);
180       return n.doubleValue();
181     }
182   }
183   
184   public static final byte[] toByteArray(int n) {
185     return toByteArray(n, true);
186   }
187   
188   public static final byte[] toByteArray(int n, boolean calcLength) {
189     int len;
190     if (calcLength) {
191       len = (int)(Math.ceil(Math.ceil(Math.log(n) / Math.log(2)) / 8));
192     }
193     else {
194       len = 4;
195     }
196     byte[] b = new byte[len];
197     int shift = 8 * (len-1);
198     for (int i = 0; i < len; i++) {
199       b[i] = (byte)(n >>> shift);
200       shift -= 8;
201     }
202     return b;
203   }
204   
205   public static final int toInt(byte[] b) {
206     return toInt(b, 0, b.length);
207   }
208   
209   public static final int toInt(byte[] b, int offset, int len) {
210     int n = 0;
211     int shift = 8 * (len-1);
212     for (int i = offset; i < offset+len; i++) {
213       n |= (int)(b[i] & 0xff) << shift;
214       shift -= 8;
215     }
216     return n;
217   }
218   
219   public static final String JavaDoc toString(byte[] b) {
220     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
221     int len = b.length - 1;
222     for (int i = 0; i <= len; i++) {
223       sb.append(Integer.toHexString(b[i] & 0xff));
224       if (i < len) {
225         sb.append(SPACE);
226       }
227     }
228     return sb.toString();
229   }
230   
231
232 }
Popular Tags