| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NumberUtils |
|
| 6.104166666666667;6.104 |
| 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 | package org.apache.commons.lang3.math; | |
| 18 | ||
| 19 | import java.lang.reflect.Array; | |
| 20 | import java.math.BigDecimal; | |
| 21 | import java.math.BigInteger; | |
| 22 | ||
| 23 | import org.apache.commons.lang3.StringUtils; | |
| 24 | ||
| 25 | /** | |
| 26 | * <p>Provides extra functionality for Java Number classes.</p> | |
| 27 | * | |
| 28 | * @since 2.0 | |
| 29 | * @version $Id: NumberUtils.java 1448286 2013-02-20 16:47:18Z tn $ | |
| 30 | */ | |
| 31 | public class NumberUtils { | |
| 32 | ||
| 33 | /** Reusable Long constant for zero. */ | |
| 34 | 1 | public static final Long LONG_ZERO = Long.valueOf(0L); |
| 35 | /** Reusable Long constant for one. */ | |
| 36 | 1 | public static final Long LONG_ONE = Long.valueOf(1L); |
| 37 | /** Reusable Long constant for minus one. */ | |
| 38 | 1 | public static final Long LONG_MINUS_ONE = Long.valueOf(-1L); |
| 39 | /** Reusable Integer constant for zero. */ | |
| 40 | 1 | public static final Integer INTEGER_ZERO = Integer.valueOf(0); |
| 41 | /** Reusable Integer constant for one. */ | |
| 42 | 1 | public static final Integer INTEGER_ONE = Integer.valueOf(1); |
| 43 | /** Reusable Integer constant for minus one. */ | |
| 44 | 1 | public static final Integer INTEGER_MINUS_ONE = Integer.valueOf(-1); |
| 45 | /** Reusable Short constant for zero. */ | |
| 46 | 1 | public static final Short SHORT_ZERO = Short.valueOf((short) 0); |
| 47 | /** Reusable Short constant for one. */ | |
| 48 | 1 | public static final Short SHORT_ONE = Short.valueOf((short) 1); |
| 49 | /** Reusable Short constant for minus one. */ | |
| 50 | 1 | public static final Short SHORT_MINUS_ONE = Short.valueOf((short) -1); |
| 51 | /** Reusable Byte constant for zero. */ | |
| 52 | 1 | public static final Byte BYTE_ZERO = Byte.valueOf((byte) 0); |
| 53 | /** Reusable Byte constant for one. */ | |
| 54 | 1 | public static final Byte BYTE_ONE = Byte.valueOf((byte) 1); |
| 55 | /** Reusable Byte constant for minus one. */ | |
| 56 | 1 | public static final Byte BYTE_MINUS_ONE = Byte.valueOf((byte) -1); |
| 57 | /** Reusable Double constant for zero. */ | |
| 58 | 1 | public static final Double DOUBLE_ZERO = Double.valueOf(0.0d); |
| 59 | /** Reusable Double constant for one. */ | |
| 60 | 1 | public static final Double DOUBLE_ONE = Double.valueOf(1.0d); |
| 61 | /** Reusable Double constant for minus one. */ | |
| 62 | 1 | public static final Double DOUBLE_MINUS_ONE = Double.valueOf(-1.0d); |
| 63 | /** Reusable Float constant for zero. */ | |
| 64 | 1 | public static final Float FLOAT_ZERO = Float.valueOf(0.0f); |
| 65 | /** Reusable Float constant for one. */ | |
| 66 | 1 | public static final Float FLOAT_ONE = Float.valueOf(1.0f); |
| 67 | /** Reusable Float constant for minus one. */ | |
| 68 | 1 | public static final Float FLOAT_MINUS_ONE = Float.valueOf(-1.0f); |
| 69 | ||
| 70 | /** | |
| 71 | * <p><code>NumberUtils</code> instances should NOT be constructed in standard programming. | |
| 72 | * Instead, the class should be used as <code>NumberUtils.toInt("6");</code>.</p> | |
| 73 | * | |
| 74 | * <p>This constructor is public to permit tools that require a JavaBean instance | |
| 75 | * to operate.</p> | |
| 76 | */ | |
| 77 | public NumberUtils() { | |
| 78 | 1 | super(); |
| 79 | 1 | } |
| 80 | ||
| 81 | //----------------------------------------------------------------------- | |
| 82 | /** | |
| 83 | * <p>Convert a <code>String</code> to an <code>int</code>, returning | |
| 84 | * <code>zero</code> if the conversion fails.</p> | |
| 85 | * | |
| 86 | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> | |
| 87 | * | |
| 88 | * <pre> | |
| 89 | * NumberUtils.toInt(null) = 0 | |
| 90 | * NumberUtils.toInt("") = 0 | |
| 91 | * NumberUtils.toInt("1") = 1 | |
| 92 | * </pre> | |
| 93 | * | |
| 94 | * @param str the string to convert, may be null | |
| 95 | * @return the int represented by the string, or <code>zero</code> if | |
| 96 | * conversion fails | |
| 97 | * @since 2.1 | |
| 98 | */ | |
| 99 | public static int toInt(final String str) { | |
| 100 | 4 | return toInt(str, 0); |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * <p>Convert a <code>String</code> to an <code>int</code>, returning a | |
| 105 | * default value if the conversion fails.</p> | |
| 106 | * | |
| 107 | * <p>If the string is <code>null</code>, the default value is returned.</p> | |
| 108 | * | |
| 109 | * <pre> | |
| 110 | * NumberUtils.toInt(null, 1) = 1 | |
| 111 | * NumberUtils.toInt("", 1) = 1 | |
| 112 | * NumberUtils.toInt("1", 0) = 1 | |
| 113 | * </pre> | |
| 114 | * | |
| 115 | * @param str the string to convert, may be null | |
| 116 | * @param defaultValue the default value | |
| 117 | * @return the int represented by the string, or the default if conversion fails | |
| 118 | * @since 2.1 | |
| 119 | */ | |
| 120 | public static int toInt(final String str, final int defaultValue) { | |
| 121 | 6 | if(str == null) { |
| 122 | 1 | return defaultValue; |
| 123 | } | |
| 124 | try { | |
| 125 | 5 | return Integer.parseInt(str); |
| 126 | 3 | } catch (final NumberFormatException nfe) { |
| 127 | 3 | return defaultValue; |
| 128 | } | |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * <p>Convert a <code>String</code> to a <code>long</code>, returning | |
| 133 | * <code>zero</code> if the conversion fails.</p> | |
| 134 | * | |
| 135 | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> | |
| 136 | * | |
| 137 | * <pre> | |
| 138 | * NumberUtils.toLong(null) = 0L | |
| 139 | * NumberUtils.toLong("") = 0L | |
| 140 | * NumberUtils.toLong("1") = 1L | |
| 141 | * </pre> | |
| 142 | * | |
| 143 | * @param str the string to convert, may be null | |
| 144 | * @return the long represented by the string, or <code>0</code> if | |
| 145 | * conversion fails | |
| 146 | * @since 2.1 | |
| 147 | */ | |
| 148 | public static long toLong(final String str) { | |
| 149 | 8 | return toLong(str, 0L); |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * <p>Convert a <code>String</code> to a <code>long</code>, returning a | |
| 154 | * default value if the conversion fails.</p> | |
| 155 | * | |
| 156 | * <p>If the string is <code>null</code>, the default value is returned.</p> | |
| 157 | * | |
| 158 | * <pre> | |
| 159 | * NumberUtils.toLong(null, 1L) = 1L | |
| 160 | * NumberUtils.toLong("", 1L) = 1L | |
| 161 | * NumberUtils.toLong("1", 0L) = 1L | |
| 162 | * </pre> | |
| 163 | * | |
| 164 | * @param str the string to convert, may be null | |
| 165 | * @param defaultValue the default value | |
| 166 | * @return the long represented by the string, or the default if conversion fails | |
| 167 | * @since 2.1 | |
| 168 | */ | |
| 169 | public static long toLong(final String str, final long defaultValue) { | |
| 170 | 10 | if (str == null) { |
| 171 | 1 | return defaultValue; |
| 172 | } | |
| 173 | try { | |
| 174 | 9 | return Long.parseLong(str); |
| 175 | 5 | } catch (final NumberFormatException nfe) { |
| 176 | 5 | return defaultValue; |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 180 | /** | |
| 181 | * <p>Convert a <code>String</code> to a <code>float</code>, returning | |
| 182 | * <code>0.0f</code> if the conversion fails.</p> | |
| 183 | * | |
| 184 | * <p>If the string <code>str</code> is <code>null</code>, | |
| 185 | * <code>0.0f</code> is returned.</p> | |
| 186 | * | |
| 187 | * <pre> | |
| 188 | * NumberUtils.toFloat(null) = 0.0f | |
| 189 | * NumberUtils.toFloat("") = 0.0f | |
| 190 | * NumberUtils.toFloat("1.5") = 1.5f | |
| 191 | * </pre> | |
| 192 | * | |
| 193 | * @param str the string to convert, may be <code>null</code> | |
| 194 | * @return the float represented by the string, or <code>0.0f</code> | |
| 195 | * if conversion fails | |
| 196 | * @since 2.1 | |
| 197 | */ | |
| 198 | public static float toFloat(final String str) { | |
| 199 | 7 | return toFloat(str, 0.0f); |
| 200 | } | |
| 201 | ||
| 202 | /** | |
| 203 | * <p>Convert a <code>String</code> to a <code>float</code>, returning a | |
| 204 | * default value if the conversion fails.</p> | |
| 205 | * | |
| 206 | * <p>If the string <code>str</code> is <code>null</code>, the default | |
| 207 | * value is returned.</p> | |
| 208 | * | |
| 209 | * <pre> | |
| 210 | * NumberUtils.toFloat(null, 1.1f) = 1.0f | |
| 211 | * NumberUtils.toFloat("", 1.1f) = 1.1f | |
| 212 | * NumberUtils.toFloat("1.5", 0.0f) = 1.5f | |
| 213 | * </pre> | |
| 214 | * | |
| 215 | * @param str the string to convert, may be <code>null</code> | |
| 216 | * @param defaultValue the default value | |
| 217 | * @return the float represented by the string, or defaultValue | |
| 218 | * if conversion fails | |
| 219 | * @since 2.1 | |
| 220 | */ | |
| 221 | public static float toFloat(final String str, final float defaultValue) { | |
| 222 | 9 | if (str == null) { |
| 223 | 1 | return defaultValue; |
| 224 | } | |
| 225 | try { | |
| 226 | 8 | return Float.parseFloat(str); |
| 227 | 3 | } catch (final NumberFormatException nfe) { |
| 228 | 3 | return defaultValue; |
| 229 | } | |
| 230 | } | |
| 231 | ||
| 232 | /** | |
| 233 | * <p>Convert a <code>String</code> to a <code>double</code>, returning | |
| 234 | * <code>0.0d</code> if the conversion fails.</p> | |
| 235 | * | |
| 236 | * <p>If the string <code>str</code> is <code>null</code>, | |
| 237 | * <code>0.0d</code> is returned.</p> | |
| 238 | * | |
| 239 | * <pre> | |
| 240 | * NumberUtils.toDouble(null) = 0.0d | |
| 241 | * NumberUtils.toDouble("") = 0.0d | |
| 242 | * NumberUtils.toDouble("1.5") = 1.5d | |
| 243 | * </pre> | |
| 244 | * | |
| 245 | * @param str the string to convert, may be <code>null</code> | |
| 246 | * @return the double represented by the string, or <code>0.0d</code> | |
| 247 | * if conversion fails | |
| 248 | * @since 2.1 | |
| 249 | */ | |
| 250 | public static double toDouble(final String str) { | |
| 251 | 7 | return toDouble(str, 0.0d); |
| 252 | } | |
| 253 | ||
| 254 | /** | |
| 255 | * <p>Convert a <code>String</code> to a <code>double</code>, returning a | |
| 256 | * default value if the conversion fails.</p> | |
| 257 | * | |
| 258 | * <p>If the string <code>str</code> is <code>null</code>, the default | |
| 259 | * value is returned.</p> | |
| 260 | * | |
| 261 | * <pre> | |
| 262 | * NumberUtils.toDouble(null, 1.1d) = 1.1d | |
| 263 | * NumberUtils.toDouble("", 1.1d) = 1.1d | |
| 264 | * NumberUtils.toDouble("1.5", 0.0d) = 1.5d | |
| 265 | * </pre> | |
| 266 | * | |
| 267 | * @param str the string to convert, may be <code>null</code> | |
| 268 | * @param defaultValue the default value | |
| 269 | * @return the double represented by the string, or defaultValue | |
| 270 | * if conversion fails | |
| 271 | * @since 2.1 | |
| 272 | */ | |
| 273 | public static double toDouble(final String str, final double defaultValue) { | |
| 274 | 9 | if (str == null) { |
| 275 | 1 | return defaultValue; |
| 276 | } | |
| 277 | try { | |
| 278 | 8 | return Double.parseDouble(str); |
| 279 | 3 | } catch (final NumberFormatException nfe) { |
| 280 | 3 | return defaultValue; |
| 281 | } | |
| 282 | } | |
| 283 | ||
| 284 | //----------------------------------------------------------------------- | |
| 285 | /** | |
| 286 | * <p>Convert a <code>String</code> to a <code>byte</code>, returning | |
| 287 | * <code>zero</code> if the conversion fails.</p> | |
| 288 | * | |
| 289 | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> | |
| 290 | * | |
| 291 | * <pre> | |
| 292 | * NumberUtils.toByte(null) = 0 | |
| 293 | * NumberUtils.toByte("") = 0 | |
| 294 | * NumberUtils.toByte("1") = 1 | |
| 295 | * </pre> | |
| 296 | * | |
| 297 | * @param str the string to convert, may be null | |
| 298 | * @return the byte represented by the string, or <code>zero</code> if | |
| 299 | * conversion fails | |
| 300 | * @since 2.5 | |
| 301 | */ | |
| 302 | public static byte toByte(final String str) { | |
| 303 | 4 | return toByte(str, (byte) 0); |
| 304 | } | |
| 305 | ||
| 306 | /** | |
| 307 | * <p>Convert a <code>String</code> to a <code>byte</code>, returning a | |
| 308 | * default value if the conversion fails.</p> | |
| 309 | * | |
| 310 | * <p>If the string is <code>null</code>, the default value is returned.</p> | |
| 311 | * | |
| 312 | * <pre> | |
| 313 | * NumberUtils.toByte(null, 1) = 1 | |
| 314 | * NumberUtils.toByte("", 1) = 1 | |
| 315 | * NumberUtils.toByte("1", 0) = 1 | |
| 316 | * </pre> | |
| 317 | * | |
| 318 | * @param str the string to convert, may be null | |
| 319 | * @param defaultValue the default value | |
| 320 | * @return the byte represented by the string, or the default if conversion fails | |
| 321 | * @since 2.5 | |
| 322 | */ | |
| 323 | public static byte toByte(final String str, final byte defaultValue) { | |
| 324 | 6 | if(str == null) { |
| 325 | 1 | return defaultValue; |
| 326 | } | |
| 327 | try { | |
| 328 | 5 | return Byte.parseByte(str); |
| 329 | 3 | } catch (final NumberFormatException nfe) { |
| 330 | 3 | return defaultValue; |
| 331 | } | |
| 332 | } | |
| 333 | ||
| 334 | /** | |
| 335 | * <p>Convert a <code>String</code> to a <code>short</code>, returning | |
| 336 | * <code>zero</code> if the conversion fails.</p> | |
| 337 | * | |
| 338 | * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p> | |
| 339 | * | |
| 340 | * <pre> | |
| 341 | * NumberUtils.toShort(null) = 0 | |
| 342 | * NumberUtils.toShort("") = 0 | |
| 343 | * NumberUtils.toShort("1") = 1 | |
| 344 | * </pre> | |
| 345 | * | |
| 346 | * @param str the string to convert, may be null | |
| 347 | * @return the short represented by the string, or <code>zero</code> if | |
| 348 | * conversion fails | |
| 349 | * @since 2.5 | |
| 350 | */ | |
| 351 | public static short toShort(final String str) { | |
| 352 | 4 | return toShort(str, (short) 0); |
| 353 | } | |
| 354 | ||
| 355 | /** | |
| 356 | * <p>Convert a <code>String</code> to an <code>short</code>, returning a | |
| 357 | * default value if the conversion fails.</p> | |
| 358 | * | |
| 359 | * <p>If the string is <code>null</code>, the default value is returned.</p> | |
| 360 | * | |
| 361 | * <pre> | |
| 362 | * NumberUtils.toShort(null, 1) = 1 | |
| 363 | * NumberUtils.toShort("", 1) = 1 | |
| 364 | * NumberUtils.toShort("1", 0) = 1 | |
| 365 | * </pre> | |
| 366 | * | |
| 367 | * @param str the string to convert, may be null | |
| 368 | * @param defaultValue the default value | |
| 369 | * @return the short represented by the string, or the default if conversion fails | |
| 370 | * @since 2.5 | |
| 371 | */ | |
| 372 | public static short toShort(final String str, final short defaultValue) { | |
| 373 | 6 | if(str == null) { |
| 374 | 1 | return defaultValue; |
| 375 | } | |
| 376 | try { | |
| 377 | 5 | return Short.parseShort(str); |
| 378 | 3 | } catch (final NumberFormatException nfe) { |
| 379 | 3 | return defaultValue; |
| 380 | } | |
| 381 | } | |
| 382 | ||
| 383 | //----------------------------------------------------------------------- | |
| 384 | // must handle Long, Float, Integer, Float, Short, | |
| 385 | // BigDecimal, BigInteger and Byte | |
| 386 | // useful methods: | |
| 387 | // Byte.decode(String) | |
| 388 | // Byte.valueOf(String,int radix) | |
| 389 | // Byte.valueOf(String) | |
| 390 | // Double.valueOf(String) | |
| 391 | // Float.valueOf(String) | |
| 392 | // Float.valueOf(String) | |
| 393 | // Integer.valueOf(String,int radix) | |
| 394 | // Integer.valueOf(String) | |
| 395 | // Integer.decode(String) | |
| 396 | // Integer.getInteger(String) | |
| 397 | // Integer.getInteger(String,int val) | |
| 398 | // Integer.getInteger(String,Integer val) | |
| 399 | // Integer.valueOf(String) | |
| 400 | // Double.valueOf(String) | |
| 401 | // new Byte(String) | |
| 402 | // Long.valueOf(String) | |
| 403 | // Long.getLong(String) | |
| 404 | // Long.getLong(String,int) | |
| 405 | // Long.getLong(String,Integer) | |
| 406 | // Long.valueOf(String,int) | |
| 407 | // Long.valueOf(String) | |
| 408 | // Short.valueOf(String) | |
| 409 | // Short.decode(String) | |
| 410 | // Short.valueOf(String,int) | |
| 411 | // Short.valueOf(String) | |
| 412 | // new BigDecimal(String) | |
| 413 | // new BigInteger(String) | |
| 414 | // new BigInteger(String,int radix) | |
| 415 | // Possible inputs: | |
| 416 | // 45 45.5 45E7 4.5E7 Hex Oct Binary xxxF xxxD xxxf xxxd | |
| 417 | // plus minus everything. Prolly more. A lot are not separable. | |
| 418 | ||
| 419 | /** | |
| 420 | * <p>Turns a string value into a java.lang.Number.</p> | |
| 421 | * | |
| 422 | * <p>If the string starts with <code>0x</code> or <code>-0x</code> (lower or upper case), it | |
| 423 | * will be interpreted as a hexadecimal integer - or long, if the number of digits after the 0x | |
| 424 | * prefix is more than 8. | |
| 425 | * Values with leading <code>0</code>'s will not be interpreted as octal.</p> | |
| 426 | * | |
| 427 | * <p>Then, the value is examined for a type qualifier on the end, i.e. one of | |
| 428 | * <code>'f','F','d','D','l','L'</code>. If it is found, it starts | |
| 429 | * trying to create successively larger types from the type specified | |
| 430 | * until one is found that can represent the value.</p> | |
| 431 | * | |
| 432 | * <p>If a type specifier is not found, it will check for a decimal point | |
| 433 | * and then try successively larger types from <code>Integer</code> to | |
| 434 | * <code>BigInteger</code> and from <code>Float</code> to | |
| 435 | * <code>BigDecimal</code>.</p> | |
| 436 | * | |
| 437 | * <p>Returns <code>null</code> if the string is <code>null</code>.</p> | |
| 438 | * | |
| 439 | * <p>This method does not trim the input string, i.e., strings with leading | |
| 440 | * or trailing spaces will generate NumberFormatExceptions.</p> | |
| 441 | * | |
| 442 | * @param str String containing a number, may be null | |
| 443 | * @return Number created from the string (or null if the input is null) | |
| 444 | * @throws NumberFormatException if the value cannot be converted | |
| 445 | */ | |
| 446 | public static Number createNumber(final String str) throws NumberFormatException { | |
| 447 | 91 | if (str == null) { |
| 448 | 2 | return null; |
| 449 | } | |
| 450 | 89 | if (StringUtils.isBlank(str)) { |
| 451 | 1 | throw new NumberFormatException("A blank string is not a valid number"); |
| 452 | } | |
| 453 | // Need to deal with all possible hex prefixes here | |
| 454 | 88 | final String[] hex_prefixes = {"0x", "0X", "-0x", "-0X", "#", "-#"}; |
| 455 | 88 | int pfxLen = 0; |
| 456 | 546 | for(final String pfx : hex_prefixes) { |
| 457 | 472 | if (str.startsWith(pfx)) { |
| 458 | 14 | pfxLen += pfx.length(); |
| 459 | 14 | break; |
| 460 | } | |
| 461 | } | |
| 462 | 88 | if (pfxLen > 0) { |
| 463 | 14 | final int hexDigits = str.length() - pfxLen; |
| 464 | 14 | if (hexDigits > 16) { // too many for Long |
| 465 | 2 | return createBigInteger(str); |
| 466 | } | |
| 467 | 12 | if (hexDigits > 8) { // too many for an int |
| 468 | 3 | return createLong(str); |
| 469 | } | |
| 470 | 9 | return createInteger(str); |
| 471 | } | |
| 472 | 74 | final char lastChar = str.charAt(str.length() - 1); |
| 473 | String mant; | |
| 474 | String dec; | |
| 475 | String exp; | |
| 476 | 74 | final int decPos = str.indexOf('.'); |
| 477 | 74 | final int expPos = str.indexOf('e') + str.indexOf('E') + 1; |
| 478 | ||
| 479 | 74 | if (decPos > -1) { |
| 480 | ||
| 481 | 32 | if (expPos > -1) { |
| 482 | 17 | if (expPos < decPos || expPos > str.length()) { |
| 483 | 1 | throw new NumberFormatException(str + " is not a valid number."); |
| 484 | } | |
| 485 | 16 | dec = str.substring(decPos + 1, expPos); |
| 486 | } else { | |
| 487 | 15 | dec = str.substring(decPos + 1); |
| 488 | } | |
| 489 | 31 | mant = str.substring(0, decPos); |
| 490 | } else { | |
| 491 | 42 | if (expPos > -1) { |
| 492 | 9 | if (expPos > str.length()) { |
| 493 | 1 | throw new NumberFormatException(str + " is not a valid number."); |
| 494 | } | |
| 495 | 8 | mant = str.substring(0, expPos); |
| 496 | } else { | |
| 497 | 33 | mant = str; |
| 498 | } | |
| 499 | 41 | dec = null; |
| 500 | } | |
| 501 | 72 | if (!Character.isDigit(lastChar) && lastChar != '.') { |
| 502 | 28 | if (expPos > -1 && expPos < str.length() - 1) { |
| 503 | 8 | exp = str.substring(expPos + 1, str.length() - 1); |
| 504 | } else { | |
| 505 | 20 | exp = null; |
| 506 | } | |
| 507 | //Requesting a specific type.. | |
| 508 | 28 | final String numeric = str.substring(0, str.length() - 1); |
| 509 | 28 | final boolean allZeros = isAllZeros(mant) && isAllZeros(exp); |
| 510 | 28 | switch (lastChar) { |
| 511 | case 'l' : | |
| 512 | case 'L' : | |
| 513 | 9 | if (dec == null |
| 514 | && exp == null | |
| 515 | && (numeric.charAt(0) == '-' && isDigits(numeric.substring(1)) || isDigits(numeric))) { | |
| 516 | try { | |
| 517 | 8 | return createLong(numeric); |
| 518 | 1 | } catch (final NumberFormatException nfe) { // NOPMD |
| 519 | // Too big for a long | |
| 520 | } | |
| 521 | 1 | return createBigInteger(numeric); |
| 522 | ||
| 523 | } | |
| 524 | 1 | throw new NumberFormatException(str + " is not a valid number."); |
| 525 | case 'f' : | |
| 526 | case 'F' : | |
| 527 | try { | |
| 528 | 7 | final Float f = NumberUtils.createFloat(numeric); |
| 529 | 5 | if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) { |
| 530 | //If it's too big for a float or the float value = 0 and the string | |
| 531 | //has non-zeros in it, then float does not have the precision we want | |
| 532 | 3 | return f; |
| 533 | } | |
| 534 | ||
| 535 | 2 | } catch (final NumberFormatException nfe) { // NOPMD |
| 536 | // ignore the bad number | |
| 537 | 2 | } |
| 538 | //$FALL-THROUGH$ | |
| 539 | case 'd' : | |
| 540 | case 'D' : | |
| 541 | try { | |
| 542 | 9 | final Double d = NumberUtils.createDouble(numeric); |
| 543 | 5 | if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) { |
| 544 | 3 | return d; |
| 545 | } | |
| 546 | 4 | } catch (final NumberFormatException nfe) { // NOPMD |
| 547 | // ignore the bad number | |
| 548 | 2 | } |
| 549 | try { | |
| 550 | 6 | return createBigDecimal(numeric); |
| 551 | 4 | } catch (final NumberFormatException e) { // NOPMD |
| 552 | // ignore the bad number | |
| 553 | } | |
| 554 | //$FALL-THROUGH$ | |
| 555 | default : | |
| 556 | 11 | throw new NumberFormatException(str + " is not a valid number."); |
| 557 | ||
| 558 | } | |
| 559 | } else { | |
| 560 | //User doesn't have a preference on the return type, so let's start | |
| 561 | //small and go from there... | |
| 562 | 44 | if (expPos > -1 && expPos < str.length() - 1) { |
| 563 | 15 | exp = str.substring(expPos + 1, str.length()); |
| 564 | } else { | |
| 565 | 29 | exp = null; |
| 566 | } | |
| 567 | 44 | if (dec == null && exp == null) { |
| 568 | //Must be an int,long,bigint | |
| 569 | try { | |
| 570 | 19 | return createInteger(str); |
| 571 | 10 | } catch (final NumberFormatException nfe) { // NOPMD |
| 572 | // ignore the bad number | |
| 573 | } | |
| 574 | try { | |
| 575 | 10 | return createLong(str); |
| 576 | 6 | } catch (final NumberFormatException nfe) { // NOPMD |
| 577 | // ignore the bad number | |
| 578 | } | |
| 579 | 6 | return createBigInteger(str); |
| 580 | ||
| 581 | } else { | |
| 582 | //Must be a float,double,BigDec | |
| 583 | 25 | final boolean allZeros = isAllZeros(mant) && isAllZeros(exp); |
| 584 | try { | |
| 585 | 25 | final Float f = createFloat(str); |
| 586 | 21 | if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) { |
| 587 | 14 | return f; |
| 588 | } | |
| 589 | 4 | } catch (final NumberFormatException nfe) { // NOPMD |
| 590 | // ignore the bad number | |
| 591 | 7 | } |
| 592 | try { | |
| 593 | 11 | final Double d = createDouble(str); |
| 594 | 7 | if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) { |
| 595 | 6 | return d; |
| 596 | } | |
| 597 | 4 | } catch (final NumberFormatException nfe) { // NOPMD |
| 598 | // ignore the bad number | |
| 599 | 1 | } |
| 600 | ||
| 601 | 5 | return createBigDecimal(str); |
| 602 | ||
| 603 | } | |
| 604 | } | |
| 605 | } | |
| 606 | ||
| 607 | /** | |
| 608 | * <p>Utility method for {@link #createNumber(java.lang.String)}.</p> | |
| 609 | * | |
| 610 | * <p>Returns <code>true</code> if s is <code>null</code>.</p> | |
| 611 | * | |
| 612 | * @param str the String to check | |
| 613 | * @return if it is all zeros or <code>null</code> | |
| 614 | */ | |
| 615 | private static boolean isAllZeros(final String str) { | |
| 616 | 53 | if (str == null) { |
| 617 | 0 | return true; |
| 618 | } | |
| 619 | 53 | for (int i = str.length() - 1; i >= 0; i--) { |
| 620 | 50 | if (str.charAt(i) != '0') { |
| 621 | 50 | return false; |
| 622 | } | |
| 623 | } | |
| 624 | 3 | return str.length() > 0; |
| 625 | } | |
| 626 | ||
| 627 | //----------------------------------------------------------------------- | |
| 628 | /** | |
| 629 | * <p>Convert a <code>String</code> to a <code>Float</code>.</p> | |
| 630 | * | |
| 631 | * <p>Returns <code>null</code> if the string is <code>null</code>.</p> | |
| 632 | * | |
| 633 | * @param str a <code>String</code> to convert, may be null | |
| 634 | * @return converted <code>Float</code> (or null if the input is null) | |
| 635 | * @throws NumberFormatException if the value cannot be converted | |
| 636 | */ | |
| 637 | public static Float createFloat(final String str) { | |
| 638 | 38 | if (str == null) { |
| 639 | 1 | return null; |
| 640 | } | |
| 641 | 37 | return Float.valueOf(str); |
| 642 | } | |
| 643 | ||
| 644 | /** | |
| 645 | * <p>Convert a <code>String</code> to a <code>Double</code>.</p> | |
| 646 | * | |
| 647 | * <p>Returns <code>null</code> if the string is <code>null</code>.</p> | |
| 648 | * | |
| 649 | * @param str a <code>String</code> to convert, may be null | |
| 650 | * @return converted <code>Double</code> (or null if the input is null) | |
| 651 | * @throws NumberFormatException if the value cannot be converted | |
| 652 | */ | |
| 653 | public static Double createDouble(final String str) { | |
| 654 | 26 | if (str == null) { |
| 655 | 1 | return null; |
| 656 | } | |
| 657 | 25 | return Double.valueOf(str); |
| 658 | } | |
| 659 | ||
| 660 | /** | |
| 661 | * <p>Convert a <code>String</code> to a <code>Integer</code>, handling | |
| 662 | * hex and octal notations.</p> | |
| 663 | * | |
| 664 | * <p>Returns <code>null</code> if the string is <code>null</code>.</p> | |
| 665 | * | |
| 666 | * @param str a <code>String</code> to convert, may be null | |
| 667 | * @return converted <code>Integer</code> (or null if the input is null) | |
| 668 | * @throws NumberFormatException if the value cannot be converted | |
| 669 | */ | |
| 670 | public static Integer createInteger(final String str) { | |
| 671 | 34 | if (str == null) { |
| 672 | 1 | return null; |
| 673 | } | |
| 674 | // decode() handles 0xAABD and 0777 (hex and octal) as well. | |
| 675 | 33 | return Integer.decode(str); |
| 676 | } | |
| 677 | ||
| 678 | /** | |
| 679 | * <p>Convert a <code>String</code> to a <code>Long</code>; | |
| 680 | * since 3.1 it handles hex and octal notations.</p> | |
| 681 | * | |
| 682 | * <p>Returns <code>null</code> if the string is <code>null</code>.</p> | |
| 683 | * | |
| 684 | * @param str a <code>String</code> to convert, may be null | |
| 685 | * @return converted <code>Long</code> (or null if the input is null) | |
| 686 | * @throws NumberFormatException if the value cannot be converted | |
| 687 | */ | |
| 688 | public static Long createLong(final String str) { | |
| 689 | 27 | if (str == null) { |
| 690 | 1 | return null; |
| 691 | } | |
| 692 | 26 | return Long.decode(str); |
| 693 | } | |
| 694 | ||
| 695 | /** | |
| 696 | * <p>Convert a <code>String</code> to a <code>BigInteger</code>; | |
| 697 | * since 3.2 it handles hex (0x or #) and octal (0) notations.</p> | |
| 698 | * | |
| 699 | * <p>Returns <code>null</code> if the string is <code>null</code>.</p> | |
| 700 | * | |
| 701 | * @param str a <code>String</code> to convert, may be null | |
| 702 | * @return converted <code>BigInteger</code> (or null if the input is null) | |
| 703 | * @throws NumberFormatException if the value cannot be converted | |
| 704 | */ | |
| 705 | public static BigInteger createBigInteger(final String str) { | |
| 706 | 27 | if (str == null) { |
| 707 | 1 | return null; |
| 708 | } | |
| 709 | 26 | int pos = 0; // offset within string |
| 710 | 26 | int radix = 10; |
| 711 | 26 | boolean negate = false; // need to negate later? |
| 712 | 26 | if (str.startsWith("-")) { |
| 713 | 7 | negate = true; |
| 714 | 7 | pos = 1; |
| 715 | } | |
| 716 | 26 | if (str.startsWith("0x", pos) || str.startsWith("0x", pos)) { // hex |
| 717 | 5 | radix = 16; |
| 718 | 5 | pos += 2; |
| 719 | 21 | } else if (str.startsWith("#", pos)) { // alternative hex (allowed by Long/Integer) |
| 720 | 4 | radix = 16; |
| 721 | 4 | pos ++; |
| 722 | 17 | } else if (str.startsWith("0", pos) && str.length() > pos + 1) { // octal; so long as there are additional digits |
| 723 | 5 | radix = 8; |
| 724 | 5 | pos ++; |
| 725 | } // default is to treat as decimal | |
| 726 | ||
| 727 | 26 | final BigInteger value = new BigInteger(str.substring(pos), radix); |
| 728 | 14 | return negate ? value.negate() : value; |
| 729 | } | |
| 730 | ||
| 731 | /** | |
| 732 | * <p>Convert a <code>String</code> to a <code>BigDecimal</code>.</p> | |
| 733 | * | |
| 734 | * <p>Returns <code>null</code> if the string is <code>null</code>.</p> | |
| 735 | * | |
| 736 | * @param str a <code>String</code> to convert, may be null | |
| 737 | * @return converted <code>BigDecimal</code> (or null if the input is null) | |
| 738 | * @throws NumberFormatException if the value cannot be converted | |
| 739 | */ | |
| 740 | public static BigDecimal createBigDecimal(final String str) { | |
| 741 | 23 | if (str == null) { |
| 742 | 1 | return null; |
| 743 | } | |
| 744 | // handle JDK1.3.1 bug where "" throws IndexOutOfBoundsException | |
| 745 | 22 | if (StringUtils.isBlank(str)) { |
| 746 | 2 | throw new NumberFormatException("A blank string is not a valid number"); |
| 747 | } | |
| 748 | 20 | if (str.trim().startsWith("--")) { |
| 749 | // this is protection for poorness in java.lang.BigDecimal. | |
| 750 | // it accepts this as a legal value, but it does not appear | |
| 751 | // to be in specification of class. OS X Java parses it to | |
| 752 | // a wrong value. | |
| 753 | 4 | throw new NumberFormatException(str + " is not a valid number."); |
| 754 | } | |
| 755 | 16 | return new BigDecimal(str); |
| 756 | } | |
| 757 | ||
| 758 | // Min in array | |
| 759 | //-------------------------------------------------------------------- | |
| 760 | /** | |
| 761 | * <p>Returns the minimum value in an array.</p> | |
| 762 | * | |
| 763 | * @param array an array, must not be null or empty | |
| 764 | * @return the minimum value in the array | |
| 765 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 766 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 767 | */ | |
| 768 | public static long min(final long[] array) { | |
| 769 | // Validates input | |
| 770 | 6 | validateArray(array); |
| 771 | ||
| 772 | // Finds and returns min | |
| 773 | 4 | long min = array[0]; |
| 774 | 13 | for (int i = 1; i < array.length; i++) { |
| 775 | 9 | if (array[i] < min) { |
| 776 | 1 | min = array[i]; |
| 777 | } | |
| 778 | } | |
| 779 | ||
| 780 | 4 | return min; |
| 781 | } | |
| 782 | ||
| 783 | /** | |
| 784 | * <p>Returns the minimum value in an array.</p> | |
| 785 | * | |
| 786 | * @param array an array, must not be null or empty | |
| 787 | * @return the minimum value in the array | |
| 788 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 789 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 790 | */ | |
| 791 | public static int min(final int[] array) { | |
| 792 | // Validates input | |
| 793 | 6 | validateArray(array); |
| 794 | ||
| 795 | // Finds and returns min | |
| 796 | 4 | int min = array[0]; |
| 797 | 13 | for (int j = 1; j < array.length; j++) { |
| 798 | 9 | if (array[j] < min) { |
| 799 | 1 | min = array[j]; |
| 800 | } | |
| 801 | } | |
| 802 | ||
| 803 | 4 | return min; |
| 804 | } | |
| 805 | ||
| 806 | /** | |
| 807 | * <p>Returns the minimum value in an array.</p> | |
| 808 | * | |
| 809 | * @param array an array, must not be null or empty | |
| 810 | * @return the minimum value in the array | |
| 811 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 812 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 813 | */ | |
| 814 | public static short min(final short[] array) { | |
| 815 | // Validates input | |
| 816 | 6 | validateArray(array); |
| 817 | ||
| 818 | // Finds and returns min | |
| 819 | 4 | short min = array[0]; |
| 820 | 13 | for (int i = 1; i < array.length; i++) { |
| 821 | 9 | if (array[i] < min) { |
| 822 | 1 | min = array[i]; |
| 823 | } | |
| 824 | } | |
| 825 | ||
| 826 | 4 | return min; |
| 827 | } | |
| 828 | ||
| 829 | /** | |
| 830 | * <p>Returns the minimum value in an array.</p> | |
| 831 | * | |
| 832 | * @param array an array, must not be null or empty | |
| 833 | * @return the minimum value in the array | |
| 834 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 835 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 836 | */ | |
| 837 | public static byte min(final byte[] array) { | |
| 838 | // Validates input | |
| 839 | 6 | validateArray(array); |
| 840 | ||
| 841 | // Finds and returns min | |
| 842 | 4 | byte min = array[0]; |
| 843 | 13 | for (int i = 1; i < array.length; i++) { |
| 844 | 9 | if (array[i] < min) { |
| 845 | 1 | min = array[i]; |
| 846 | } | |
| 847 | } | |
| 848 | ||
| 849 | 4 | return min; |
| 850 | } | |
| 851 | ||
| 852 | /** | |
| 853 | * <p>Returns the minimum value in an array.</p> | |
| 854 | * | |
| 855 | * @param array an array, must not be null or empty | |
| 856 | * @return the minimum value in the array | |
| 857 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 858 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 859 | * @see IEEE754rUtils#min(double[]) IEEE754rUtils for a version of this method that handles NaN differently | |
| 860 | */ | |
| 861 | public static double min(final double[] array) { | |
| 862 | // Validates input | |
| 863 | 9 | validateArray(array); |
| 864 | ||
| 865 | // Finds and returns min | |
| 866 | 7 | double min = array[0]; |
| 867 | 21 | for (int i = 1; i < array.length; i++) { |
| 868 | 16 | if (Double.isNaN(array[i])) { |
| 869 | 2 | return Double.NaN; |
| 870 | } | |
| 871 | 14 | if (array[i] < min) { |
| 872 | 1 | min = array[i]; |
| 873 | } | |
| 874 | } | |
| 875 | ||
| 876 | 5 | return min; |
| 877 | } | |
| 878 | ||
| 879 | /** | |
| 880 | * <p>Returns the minimum value in an array.</p> | |
| 881 | * | |
| 882 | * @param array an array, must not be null or empty | |
| 883 | * @return the minimum value in the array | |
| 884 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 885 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 886 | * @see IEEE754rUtils#min(float[]) IEEE754rUtils for a version of this method that handles NaN differently | |
| 887 | */ | |
| 888 | public static float min(final float[] array) { | |
| 889 | // Validates input | |
| 890 | 7 | validateArray(array); |
| 891 | ||
| 892 | // Finds and returns min | |
| 893 | 5 | float min = array[0]; |
| 894 | 18 | for (int i = 1; i < array.length; i++) { |
| 895 | 13 | if (Float.isNaN(array[i])) { |
| 896 | 0 | return Float.NaN; |
| 897 | } | |
| 898 | 13 | if (array[i] < min) { |
| 899 | 1 | min = array[i]; |
| 900 | } | |
| 901 | } | |
| 902 | ||
| 903 | 5 | return min; |
| 904 | } | |
| 905 | ||
| 906 | // Max in array | |
| 907 | //-------------------------------------------------------------------- | |
| 908 | /** | |
| 909 | * <p>Returns the maximum value in an array.</p> | |
| 910 | * | |
| 911 | * @param array an array, must not be null or empty | |
| 912 | * @return the minimum value in the array | |
| 913 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 914 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 915 | */ | |
| 916 | public static long max(final long[] array) { | |
| 917 | // Validates input | |
| 918 | 7 | validateArray(array); |
| 919 | ||
| 920 | // Finds and returns max | |
| 921 | 5 | long max = array[0]; |
| 922 | 18 | for (int j = 1; j < array.length; j++) { |
| 923 | 13 | if (array[j] > max) { |
| 924 | 11 | max = array[j]; |
| 925 | } | |
| 926 | } | |
| 927 | ||
| 928 | 5 | return max; |
| 929 | } | |
| 930 | ||
| 931 | /** | |
| 932 | * <p>Returns the maximum value in an array.</p> | |
| 933 | * | |
| 934 | * @param array an array, must not be null or empty | |
| 935 | * @return the minimum value in the array | |
| 936 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 937 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 938 | */ | |
| 939 | public static int max(final int[] array) { | |
| 940 | // Validates input | |
| 941 | 7 | validateArray(array); |
| 942 | ||
| 943 | // Finds and returns max | |
| 944 | 5 | int max = array[0]; |
| 945 | 18 | for (int j = 1; j < array.length; j++) { |
| 946 | 13 | if (array[j] > max) { |
| 947 | 11 | max = array[j]; |
| 948 | } | |
| 949 | } | |
| 950 | ||
| 951 | 5 | return max; |
| 952 | } | |
| 953 | ||
| 954 | /** | |
| 955 | * <p>Returns the maximum value in an array.</p> | |
| 956 | * | |
| 957 | * @param array an array, must not be null or empty | |
| 958 | * @return the minimum value in the array | |
| 959 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 960 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 961 | */ | |
| 962 | public static short max(final short[] array) { | |
| 963 | // Validates input | |
| 964 | 7 | validateArray(array); |
| 965 | ||
| 966 | // Finds and returns max | |
| 967 | 5 | short max = array[0]; |
| 968 | 18 | for (int i = 1; i < array.length; i++) { |
| 969 | 13 | if (array[i] > max) { |
| 970 | 11 | max = array[i]; |
| 971 | } | |
| 972 | } | |
| 973 | ||
| 974 | 5 | return max; |
| 975 | } | |
| 976 | ||
| 977 | /** | |
| 978 | * <p>Returns the maximum value in an array.</p> | |
| 979 | * | |
| 980 | * @param array an array, must not be null or empty | |
| 981 | * @return the minimum value in the array | |
| 982 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 983 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 984 | */ | |
| 985 | public static byte max(final byte[] array) { | |
| 986 | // Validates input | |
| 987 | 7 | validateArray(array); |
| 988 | ||
| 989 | // Finds and returns max | |
| 990 | 5 | byte max = array[0]; |
| 991 | 18 | for (int i = 1; i < array.length; i++) { |
| 992 | 13 | if (array[i] > max) { |
| 993 | 11 | max = array[i]; |
| 994 | } | |
| 995 | } | |
| 996 | ||
| 997 | 5 | return max; |
| 998 | } | |
| 999 | ||
| 1000 | /** | |
| 1001 | * <p>Returns the maximum value in an array.</p> | |
| 1002 | * | |
| 1003 | * @param array an array, must not be null or empty | |
| 1004 | * @return the minimum value in the array | |
| 1005 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 1006 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 1007 | * @see IEEE754rUtils#max(double[]) IEEE754rUtils for a version of this method that handles NaN differently | |
| 1008 | */ | |
| 1009 | public static double max(final double[] array) { | |
| 1010 | // Validates input | |
| 1011 | 11 | validateArray(array); |
| 1012 | ||
| 1013 | // Finds and returns max | |
| 1014 | 7 | double max = array[0]; |
| 1015 | 21 | for (int j = 1; j < array.length; j++) { |
| 1016 | 16 | if (Double.isNaN(array[j])) { |
| 1017 | 2 | return Double.NaN; |
| 1018 | } | |
| 1019 | 14 | if (array[j] > max) { |
| 1020 | 11 | max = array[j]; |
| 1021 | } | |
| 1022 | } | |
| 1023 | ||
| 1024 | 5 | return max; |
| 1025 | } | |
| 1026 | ||
| 1027 | /** | |
| 1028 | * <p>Returns the maximum value in an array.</p> | |
| 1029 | * | |
| 1030 | * @param array an array, must not be null or empty | |
| 1031 | * @return the minimum value in the array | |
| 1032 | * @throws IllegalArgumentException if <code>array</code> is <code>null</code> | |
| 1033 | * @throws IllegalArgumentException if <code>array</code> is empty | |
| 1034 | * @see IEEE754rUtils#max(float[]) IEEE754rUtils for a version of this method that handles NaN differently | |
| 1035 | */ | |
| 1036 | public static float max(final float[] array) { | |
| 1037 | // Validates input | |
| 1038 | 9 | validateArray(array); |
| 1039 | ||
| 1040 | // Finds and returns max | |
| 1041 | 7 | float max = array[0]; |
| 1042 | 21 | for (int j = 1; j < array.length; j++) { |
| 1043 | 16 | if (Float.isNaN(array[j])) { |
| 1044 | 2 | return Float.NaN; |
| 1045 | } | |
| 1046 | 14 | if (array[j] > max) { |
| 1047 | 11 | max = array[j]; |
| 1048 | } | |
| 1049 | } | |
| 1050 | ||
| 1051 | 5 | return max; |
| 1052 | } | |
| 1053 | ||
| 1054 | /** | |
| 1055 | * Checks if the specified array is neither null nor empty. | |
| 1056 | * | |
| 1057 | * @param array the array to check | |
| 1058 | * @throws IllegalArgumentException if {@code array} is either {@code null} or empty | |
| 1059 | */ | |
| 1060 | private static void validateArray(final Object array) { | |
| 1061 | 88 | if (array == null) { |
| 1062 | 13 | throw new IllegalArgumentException("The Array must not be null"); |
| 1063 | 75 | } else if (Array.getLength(array) == 0) { |
| 1064 | 13 | throw new IllegalArgumentException("Array cannot be empty."); |
| 1065 | } | |
| 1066 | 62 | } |
| 1067 | ||
| 1068 | // 3 param min | |
| 1069 | //----------------------------------------------------------------------- | |
| 1070 | /** | |
| 1071 | * <p>Gets the minimum of three <code>long</code> values.</p> | |
| 1072 | * | |
| 1073 | * @param a value 1 | |
| 1074 | * @param b value 2 | |
| 1075 | * @param c value 3 | |
| 1076 | * @return the smallest of the values | |
| 1077 | */ | |
| 1078 | public static long min(long a, final long b, final long c) { | |
| 1079 | 5 | if (b < a) { |
| 1080 | 2 | a = b; |
| 1081 | } | |
| 1082 | 5 | if (c < a) { |
| 1083 | 1 | a = c; |
| 1084 | } | |
| 1085 | 5 | return a; |
| 1086 | } | |
| 1087 | ||
| 1088 | /** | |
| 1089 | * <p>Gets the minimum of three <code>int</code> values.</p> | |
| 1090 | * | |
| 1091 | * @param a value 1 | |
| 1092 | * @param b value 2 | |
| 1093 | * @param c value 3 | |
| 1094 | * @return the smallest of the values | |
| 1095 | */ | |
| 1096 | public static int min(int a, final int b, final int c) { | |
| 1097 | 5 | if (b < a) { |
| 1098 | 2 | a = b; |
| 1099 | } | |
| 1100 | 5 | if (c < a) { |
| 1101 | 1 | a = c; |
| 1102 | } | |
| 1103 | 5 | return a; |
| 1104 | } | |
| 1105 | ||
| 1106 | /** | |
| 1107 | * <p>Gets the minimum of three <code>short</code> values.</p> | |
| 1108 | * | |
| 1109 | * @param a value 1 | |
| 1110 | * @param b value 2 | |
| 1111 | * @param c value 3 | |
| 1112 | * @return the smallest of the values | |
| 1113 | */ | |
| 1114 | public static short min(short a, final short b, final short c) { | |
| 1115 | 4 | if (b < a) { |
| 1116 | 1 | a = b; |
| 1117 | } | |
| 1118 | 4 | if (c < a) { |
| 1119 | 1 | a = c; |
| 1120 | } | |
| 1121 | 4 | return a; |
| 1122 | } | |
| 1123 | ||
| 1124 | /** | |
| 1125 | * <p>Gets the minimum of three <code>byte</code> values.</p> | |
| 1126 | * | |
| 1127 | * @param a value 1 | |
| 1128 | * @param b value 2 | |
| 1129 | * @param c value 3 | |
| 1130 | * @return the smallest of the values | |
| 1131 | */ | |
| 1132 | public static byte min(byte a, final byte b, final byte c) { | |
| 1133 | 4 | if (b < a) { |
| 1134 | 1 | a = b; |
| 1135 | } | |
| 1136 | 4 | if (c < a) { |
| 1137 | 1 | a = c; |
| 1138 | } | |
| 1139 | 4 | return a; |
| 1140 | } | |
| 1141 | ||
| 1142 | /** | |
| 1143 | * <p>Gets the minimum of three <code>double</code> values.</p> | |
| 1144 | * | |
| 1145 | * <p>If any value is <code>NaN</code>, <code>NaN</code> is | |
| 1146 | * returned. Infinity is handled.</p> | |
| 1147 | * | |
| 1148 | * @param a value 1 | |
| 1149 | * @param b value 2 | |
| 1150 | * @param c value 3 | |
| 1151 | * @return the smallest of the values | |
| 1152 | * @see IEEE754rUtils#min(double, double, double) for a version of this method that handles NaN differently | |
| 1153 | */ | |
| 1154 | public static double min(final double a, final double b, final double c) { | |
| 1155 | 6 | return Math.min(Math.min(a, b), c); |
| 1156 | } | |
| 1157 | ||
| 1158 | /** | |
| 1159 | * <p>Gets the minimum of three <code>float</code> values.</p> | |
| 1160 | * | |
| 1161 | * <p>If any value is <code>NaN</code>, <code>NaN</code> is | |
| 1162 | * returned. Infinity is handled.</p> | |
| 1163 | * | |
| 1164 | * @param a value 1 | |
| 1165 | * @param b value 2 | |
| 1166 | * @param c value 3 | |
| 1167 | * @return the smallest of the values | |
| 1168 | * @see IEEE754rUtils#min(float, float, float) for a version of this method that handles NaN differently | |
| 1169 | */ | |
| 1170 | public static float min(final float a, final float b, final float c) { | |
| 1171 | 6 | return Math.min(Math.min(a, b), c); |
| 1172 | } | |
| 1173 | ||
| 1174 | // 3 param max | |
| 1175 | //----------------------------------------------------------------------- | |
| 1176 | /** | |
| 1177 | * <p>Gets the maximum of three <code>long</code> values.</p> | |
| 1178 | * | |
| 1179 | * @param a value 1 | |
| 1180 | * @param b value 2 | |
| 1181 | * @param c value 3 | |
| 1182 | * @return the largest of the values | |
| 1183 | */ | |
| 1184 | public static long max(long a, final long b, final long c) { | |
| 1185 | 5 | if (b > a) { |
| 1186 | 2 | a = b; |
| 1187 | } | |
| 1188 | 5 | if (c > a) { |
| 1189 | 1 | a = c; |
| 1190 | } | |
| 1191 | 5 | return a; |
| 1192 | } | |
| 1193 | ||
| 1194 | /** | |
| 1195 | * <p>Gets the maximum of three <code>int</code> values.</p> | |
| 1196 | * | |
| 1197 | * @param a value 1 | |
| 1198 | * @param b value 2 | |
| 1199 | * @param c value 3 | |
| 1200 | * @return the largest of the values | |
| 1201 | */ | |
| 1202 | public static int max(int a, final int b, final int c) { | |
| 1203 | 5 | if (b > a) { |
| 1204 | 2 | a = b; |
| 1205 | } | |
| 1206 | 5 | if (c > a) { |
| 1207 | 1 | a = c; |
| 1208 | } | |
| 1209 | 5 | return a; |
| 1210 | } | |
| 1211 | ||
| 1212 | /** | |
| 1213 | * <p>Gets the maximum of three <code>short</code> values.</p> | |
| 1214 | * | |
| 1215 | * @param a value 1 | |
| 1216 | * @param b value 2 | |
| 1217 | * @param c value 3 | |
| 1218 | * @return the largest of the values | |
| 1219 | */ | |
| 1220 | public static short max(short a, final short b, final short c) { | |
| 1221 | 4 | if (b > a) { |
| 1222 | 2 | a = b; |
| 1223 | } | |
| 1224 | 4 | if (c > a) { |
| 1225 | 2 | a = c; |
| 1226 | } | |
| 1227 | 4 | return a; |
| 1228 | } | |
| 1229 | ||
| 1230 | /** | |
| 1231 | * <p>Gets the maximum of three <code>byte</code> values.</p> | |
| 1232 | * | |
| 1233 | * @param a value 1 | |
| 1234 | * @param b value 2 | |
| 1235 | * @param c value 3 | |
| 1236 | * @return the largest of the values | |
| 1237 | */ | |
| 1238 | public static byte max(byte a, final byte b, final byte c) { | |
| 1239 | 4 | if (b > a) { |
| 1240 | 2 | a = b; |
| 1241 | } | |
| 1242 | 4 | if (c > a) { |
| 1243 | 2 | a = c; |
| 1244 | } | |
| 1245 | 4 | return a; |
| 1246 | } | |
| 1247 | ||
| 1248 | /** | |
| 1249 | * <p>Gets the maximum of three <code>double</code> values.</p> | |
| 1250 | * | |
| 1251 | * <p>If any value is <code>NaN</code>, <code>NaN</code> is | |
| 1252 | * returned. Infinity is handled.</p> | |
| 1253 | * | |
| 1254 | * @param a value 1 | |
| 1255 | * @param b value 2 | |
| 1256 | * @param c value 3 | |
| 1257 | * @return the largest of the values | |
| 1258 | * @see IEEE754rUtils#max(double, double, double) for a version of this method that handles NaN differently | |
| 1259 | */ | |
| 1260 | public static double max(final double a, final double b, final double c) { | |
| 1261 | 6 | return Math.max(Math.max(a, b), c); |
| 1262 | } | |
| 1263 | ||
| 1264 | /** | |
| 1265 | * <p>Gets the maximum of three <code>float</code> values.</p> | |
| 1266 | * | |
| 1267 | * <p>If any value is <code>NaN</code>, <code>NaN</code> is | |
| 1268 | * returned. Infinity is handled.</p> | |
| 1269 | * | |
| 1270 | * @param a value 1 | |
| 1271 | * @param b value 2 | |
| 1272 | * @param c value 3 | |
| 1273 | * @return the largest of the values | |
| 1274 | * @see IEEE754rUtils#max(float, float, float) for a version of this method that handles NaN differently | |
| 1275 | */ | |
| 1276 | public static float max(final float a, final float b, final float c) { | |
| 1277 | 6 | return Math.max(Math.max(a, b), c); |
| 1278 | } | |
| 1279 | ||
| 1280 | //----------------------------------------------------------------------- | |
| 1281 | /** | |
| 1282 | * <p>Checks whether the <code>String</code> contains only | |
| 1283 | * digit characters.</p> | |
| 1284 | * | |
| 1285 | * <p><code>Null</code> and empty String will return | |
| 1286 | * <code>false</code>.</p> | |
| 1287 | * | |
| 1288 | * @param str the <code>String</code> to check | |
| 1289 | * @return <code>true</code> if str contains only Unicode numeric | |
| 1290 | */ | |
| 1291 | public static boolean isDigits(final String str) { | |
| 1292 | 14 | if (StringUtils.isEmpty(str)) { |
| 1293 | 2 | return false; |
| 1294 | } | |
| 1295 | 73 | for (int i = 0; i < str.length(); i++) { |
| 1296 | 64 | if (!Character.isDigit(str.charAt(i))) { |
| 1297 | 3 | return false; |
| 1298 | } | |
| 1299 | } | |
| 1300 | 9 | return true; |
| 1301 | } | |
| 1302 | ||
| 1303 | /** | |
| 1304 | * <p>Checks whether the String a valid Java number.</p> | |
| 1305 | * | |
| 1306 | * <p>Valid numbers include hexadecimal marked with the <code>0x</code> | |
| 1307 | * qualifier, scientific notation and numbers marked with a type | |
| 1308 | * qualifier (e.g. 123L).</p> | |
| 1309 | * | |
| 1310 | * <p><code>Null</code> and empty String will return | |
| 1311 | * <code>false</code>.</p> | |
| 1312 | * | |
| 1313 | * @param str the <code>String</code> to check | |
| 1314 | * @return <code>true</code> if the string is a correctly formatted number | |
| 1315 | */ | |
| 1316 | public static boolean isNumber(final String str) { | |
| 1317 | 46 | if (StringUtils.isEmpty(str)) { |
| 1318 | 2 | return false; |
| 1319 | } | |
| 1320 | 44 | final char[] chars = str.toCharArray(); |
| 1321 | 44 | int sz = chars.length; |
| 1322 | 44 | boolean hasExp = false; |
| 1323 | 44 | boolean hasDecPoint = false; |
| 1324 | 44 | boolean allowSigns = false; |
| 1325 | 44 | boolean foundDigit = false; |
| 1326 | // deal with any possible sign up front | |
| 1327 | 44 | final int start = (chars[0] == '-') ? 1 : 0; |
| 1328 | 44 | if (sz > start + 1 && chars[start] == '0' && chars[start + 1] == 'x') { |
| 1329 | 4 | int i = start + 2; |
| 1330 | 4 | if (i == sz) { |
| 1331 | 0 | return false; // str == "0x" |
| 1332 | } | |
| 1333 | // checking hex (it can't be anything else) | |
| 1334 | 24 | for (; i < chars.length; i++) { |
| 1335 | 12 | if ((chars[i] < '0' || chars[i] > '9') |
| 1336 | && (chars[i] < 'a' || chars[i] > 'f') | |
| 1337 | && (chars[i] < 'A' || chars[i] > 'F')) { | |
| 1338 | 2 | return false; |
| 1339 | } | |
| 1340 | } | |
| 1341 | 2 | return true; |
| 1342 | } | |
| 1343 | 40 | sz--; // don't want to loop to the last char, check it afterwords |
| 1344 | // for type qualifiers | |
| 1345 | 40 | int i = start; |
| 1346 | // loop to the next to last char or to the last char if we need another digit to | |
| 1347 | // make a valid number (e.g. chars[0..5] = "1234E") | |
| 1348 | 179 | while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { |
| 1349 | 148 | if (chars[i] >= '0' && chars[i] <= '9') { |
| 1350 | 113 | foundDigit = true; |
| 1351 | 113 | allowSigns = false; |
| 1352 | ||
| 1353 | 35 | } else if (chars[i] == '.') { |
| 1354 | 13 | if (hasDecPoint || hasExp) { |
| 1355 | // two decimal points or dec in exponent | |
| 1356 | 2 | return false; |
| 1357 | } | |
| 1358 | 11 | hasDecPoint = true; |
| 1359 | 22 | } else if (chars[i] == 'e' || chars[i] == 'E') { |
| 1360 | // we've already taken care of hex. | |
| 1361 | 11 | if (hasExp) { |
| 1362 | // two E's | |
| 1363 | 0 | return false; |
| 1364 | } | |
| 1365 | 11 | if (!foundDigit) { |
| 1366 | 0 | return false; |
| 1367 | } | |
| 1368 | 11 | hasExp = true; |
| 1369 | 11 | allowSigns = true; |
| 1370 | 11 | } else if (chars[i] == '+' || chars[i] == '-') { |
| 1371 | 6 | if (!allowSigns) { |
| 1372 | 2 | return false; |
| 1373 | } | |
| 1374 | 4 | allowSigns = false; |
| 1375 | 4 | foundDigit = false; // we need a digit after the E |
| 1376 | } else { | |
| 1377 | 5 | return false; |
| 1378 | } | |
| 1379 | 139 | i++; |
| 1380 | } | |
| 1381 | 31 | if (i < chars.length) { |
| 1382 | 31 | if (chars[i] >= '0' && chars[i] <= '9') { |
| 1383 | // no type qualifier, OK | |
| 1384 | 15 | return true; |
| 1385 | } | |
| 1386 | 16 | if (chars[i] == 'e' || chars[i] == 'E') { |
| 1387 | // can't have an E at the last byte | |
| 1388 | 1 | return false; |
| 1389 | } | |
| 1390 | 15 | if (chars[i] == '.') { |
| 1391 | 2 | if (hasDecPoint || hasExp) { |
| 1392 | // two decimal points or dec in exponent | |
| 1393 | 0 | return false; |
| 1394 | } | |
| 1395 | // single trailing decimal point after non-exponent is ok | |
| 1396 | 2 | return foundDigit; |
| 1397 | } | |
| 1398 | 13 | if (!allowSigns |
| 1399 | && (chars[i] == 'd' | |
| 1400 | || chars[i] == 'D' | |
| 1401 | || chars[i] == 'f' | |
| 1402 | || chars[i] == 'F')) { | |
| 1403 | 3 | return foundDigit; |
| 1404 | } | |
| 1405 | 10 | if (chars[i] == 'l' |
| 1406 | || chars[i] == 'L') { | |
| 1407 | // not allowing L with an exponent or decimal point | |
| 1408 | 3 | return foundDigit && !hasExp && !hasDecPoint; |
| 1409 | } | |
| 1410 | // last character is illegal | |
| 1411 | 7 | return false; |
| 1412 | } | |
| 1413 | // allowSigns is true iff the val ends in 'E' | |
| 1414 | // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass | |
| 1415 | 0 | return !allowSigns && foundDigit; |
| 1416 | } | |
| 1417 | ||
| 1418 | } |