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