Total Pageviews

2018/03/07

[commons-validator] numeric-related utilies

Scenario
In real world, we may need some common functions to fulfill our requirement, such as:
1. Validate the number string (including Integer and Double)
2. Convert Integer and Double to String, and apply 1000 sepearator 
3. Convert string to Integer and Double
4. Check the number is in specific range


We can use commons-validator to implement this function easily.


Remember to add dependency in your pom.xml

1
2
3
4
5
  <dependency>
   <groupId>commons-validator</groupId>
   <artifactId>commons-validator</artifactId>
   <version>1.6</version>
  </dependency>



Sample Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package albert.practice.validation;

import org.apache.commons.validator.routines.DoubleValidator;
import org.apache.commons.validator.routines.IntegerValidator;

public class NumericUtils {

    /**
     * 檢查輸入的 integer string 是否為數字.
     * 
     * @param number
     */
    public static void isValidInteger(String number) {
       if (IntegerValidator.getInstance().validate(number) == null) {
           throw new IllegalArgumentException("您輸入的非數字");
       }
    }

    /**
     * 檢查輸入的 double string 是否為數字.
     * 
     * @param number
    */
    public static void isValidDouble(String number) {
       if (DoubleValidator.getInstance().validate(number) == null) {
           throw new IllegalArgumentException("您輸入的非數字");
       }
    }

    /**
     * 將 integer 加上 1000 separator.
     * 
     * @param number
     * @return
     */
    public static String formatInteger(Integer number) {
        return IntegerValidator.getInstance().format(number, "#,##0");
    }

    /**
     * 將 Double 加上 1000 separator.
     * 
     * @param number
     * @return
     */
    public static String formatDouble(Double number) {
        return DoubleValidator.getInstance().format(number, "#,##0.00");
    }

    /**
     * 將integer string 轉成 Integer.
     * 
     * @param numberStr
     * @return
     */
    public static Integer formatIntegerString(String numberStr) {
        return IntegerValidator.getInstance().validate(numberStr, "#,##0");
    }

    /**
     * 將double string 轉成 Double.
     * 
     * @param numberStr
     * @return
     */
    public static Double formatDoubleString(String numberStr) {
        return DoubleValidator.getInstance().validate(numberStr, "#,##0.00");
    }

    /**
     * 檢查 integer 是否介於指定區間.
     * 
     * @param number
     * @param min
     * @param max
     */
    public static void isIntegerInValidRange(Integer number, int min, int max) {
        if (!IntegerValidator.getInstance().isInRange(number, min, max)) {
            throw new IllegalArgumentException("您輸入的數字必須介於 " + min + " ~ " + max + " 之間");
        }
    }

    /**
     * 檢查 double 是否介於指定區間.
     * 
     * @param number
     * @param min
     * @param max
     */
    public static void isDoubleInValidRange(Double number, double min, double max) {
        if (!DoubleValidator.getInstance().isInRange(number, min, max)) {
            throw new IllegalArgumentException("您輸入的數字必須介於 " + min + " ~ " + max + " 之間");
        }
    }

}



Test Code    
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package albert.practice.validation;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class NumericUtilsTest {

    private String integerString;
    private String invalidIntegerString;

    private String doubleString;
    private String invaliddoubleString;

    @Before
    public void setup() {
       integerString = "12345000";
       invalidIntegerString = "1000a";

       doubleString = "12400.60";
       invaliddoubleString = "12400a.60";
    }

    @Test
    public void testValidInteger() {
       NumericUtils.isValidInteger(integerString);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testInvalidInteger() {
       NumericUtils.isValidInteger(invalidIntegerString);
    }

    @Test
    public void testFormatIntegerToString() {
       String numberString = NumericUtils.formatInteger(12345600);
       assertEquals("12,345,600", numberString);
    }

    @Test
    public void testFormatNumStringToInteger() {
       Integer number = NumericUtils.formatIntegerString("12,345,600");
       assertEquals(new Integer(12345600), number);
    }

    @Test
    public void testIntegerIsInRange() {
       NumericUtils.isIntegerInValidRange(new Integer(22), 20, 30);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testIntegerIsNotInRange() {
       NumericUtils.isIntegerInValidRange(new Integer(35), 20, 30);
    }

    @Test
    public void testValidDouble() {
       NumericUtils.isValidDouble(doubleString);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testInvalidDouble() {
       NumericUtils.isValidDouble(invaliddoubleString);
    }

    @Test
    public void testFormatDouble(){
       String number = NumericUtils.formatDouble(new Double(12300));
       assertEquals("12,300.00", number);
    }
 
    @Test
    public void testFormatStringToDouble() {
       Double number = NumericUtils.formatDoubleString(doubleString);
       assertEquals(new Double(12400.60), number);
    }

    @Test
    public void testDoubleIsInRange() {
       NumericUtils.isDoubleInValidRange(new Double(20.5), 20.0, 30.0);
    }
 
    @Test(expected = IllegalArgumentException.class)
    public void testDoubleIsNotInRange() {
       NumericUtils.isDoubleInValidRange(new Double(30.5), 20.0, 30.0);
    }

}




    

No comments: