1 55 package org.lateralnz.common.util; 56 57 import java.util.HashMap ; 58 import java.util.Locale ; 59 import java.text.ParseException ; 60 import java.text.DecimalFormat ; 61 62 67 public final class NumericUtils implements Constants { 68 private static final String CLASSNAME = NumericUtils.class.getName(); 69 private static final String DIGITS = "0123456789."; 70 71 private static HashMap numberFormatters = new HashMap (); 72 73 private NumericUtils() { } 74 75 78 private static final DecimalFormat getFormat(String className, Locale locale) { 79 DecimalFormat format; 80 String key = className + UNDERSCORE + UNDERSCORE + (locale != null ? locale.toString() : EMPTY); 81 if (!numberFormatters.containsKey(key)) { 82 if (locale != null) { 83 format = (DecimalFormat )DecimalFormat.getInstance(locale); 84 } 85 else { 86 format = (DecimalFormat )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 )numberFormatters.get(key); 97 } 98 99 return format; 100 } 101 102 105 public static final String format(String callerClassName, long l, String pattern) { 106 return format(callerClassName, l, null, pattern); 107 } 108 109 public static final String format(String callerClassName, float f, String pattern) { 110 return format(callerClassName, f, null, pattern); 111 } 112 113 public static final String format(String callerClassName, double d, String pattern) { 114 return format(callerClassName, d, null, pattern); 115 } 116 117 public static final String format(String callerClassName, long l, Locale locale, String pattern) { 118 DecimalFormat format = getFormat(callerClassName, locale); 119 synchronized (format) { 120 format.applyPattern(pattern); 121 return format.format(l); 122 } 123 } 124 125 public static final String format(String callerClassName, float f, Locale locale, String pattern) { 126 DecimalFormat format = getFormat(callerClassName, locale); 127 synchronized (format) { 128 format.applyPattern(pattern); 129 return format.format(f); 130 } 131 } 132 133 135 public static final String format(String callerClassName, double d, Locale locale, String pattern) { 136 DecimalFormat 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 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 160 public static final double parseDouble(String callerClassName, String s, String pattern) throws ParseException { 161 return parseDouble(callerClassName, s, null, pattern); 162 } 163 164 171 public static final double parseDouble(String callerClassName, String s, Locale locale, String pattern) throws ParseException { 172 if (StringUtils.isEmpty(s)) { 173 return 0.0d; 174 } 175 176 DecimalFormat format = getFormat(callerClassName, locale); 177 synchronized (format) { 178 format.applyPattern(pattern); 179 Number 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 toString(byte[] b) { 220 StringBuffer sb = new StringBuffer (); 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 |