Subversion Repository Public Repository

litesoft

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.temp.common.shared.utils;

/**
 * Utility methods for Parameter like validations / assertions.
 *
 * @author georgs
 */
public class Assert {

    /**
     * Assert that "value" is not null.
     *
     * @param what  "field" was the problem on
     * @param value to check
     */
    public static void notNull( String what, Object value ) {
        if ( value == null ) {
            throw nullValue( what );
        }
    }

    /**
     * Assert that "value" is not null or empty and return "value" trimmed.
     *
     * @param what  "field" was the problem on
     * @param value to check
     *
     * @return value minus any leading and trailing spaces
     */
    public static String noEmpty( String what, String value ) {
        value = StringUtils.noEmpty( value );
        if ( value == null ) {
            throw nullOrEmpty( what );
        }
        return value;
    }

    /**
     * Assert that "value" is not null or when converted to a String is not
     * empty and then return resulting "value" trimmed.
     *
     * @param what  "field" was the problem on
     * @param value to check
     *
     * @return value converted to a String minus any leading and trailing spaces
     */
    public static String noEmptyToString( String what, Object value ) {
        String strValue = StringUtils.noEmptyToString( value );
        if ( strValue == null ) {
            throw nullOrEmpty( what );
        }
        return strValue;
    }

    /**
     * Assert that "value" is not null or empty (if it is a String) and return
     * "value" trimmed (if it is a String).
     *
     * @param what  "field" was the problem on
     * @param value to check
     *
     * @return value minus any leading and trailing spaces
     */
    public static <T> T notNullOrEmpty( String what, T value ) {
        if ( value == null ) {
            throw nullOrEmpty( what );
        }
        if ( value instanceof String ) {
            value = ObjectUtils.cast( noEmpty( what, value.toString() ) );
        }
        return value;
    }

    public static IllegalArgumentException nullValue( String what ) {
        return new IllegalArgumentException( what + " not allowed to be null" );
    }

    private static IllegalArgumentException nullOrEmpty( String what ) {
        return new IllegalArgumentException( what + " not allowed to be null or empty" );
    }

    /**
     * Assert that "value" is not null or empty and is acceptable as a filename.
     * <p/>
     * While control characters are NOT allowed, all other characters are,
     * however certain characters (see
     * CharacterUtils.UNACCEPTABLE_NON_CONTROL_FILENAME_CHARACTERS) will be
     * replaced with an underscore ('_').
     *
     * @param what  "field" was the problem on
     * @param value to check - null or empty unacceptable
     *
     * @return value minus any leading and trailing spaces and any illegal
     * characters converted to an underscore.
     */
    public static String simpleFilenameRequired( String what, String value ) {
        StringBuilder sb = new StringBuilder( noEmpty( what, value ) );
        for ( int i = 0; i < sb.length(); i++ ) {
            char c = sb.charAt( i );
            if ( CharacterUtils.isControlChar( c ) ) {
                throw new IllegalArgumentException( what + " contained control character at index " + i + " : '" + value + "'" );
            }
            if ( CharacterUtils.isUnacceptableNonControlFilenameChar( c ) ) {
                sb.setCharAt( i, '_' );
            } else if ( c == CharacterUtils.HIBIT_SPACE ) {
                sb.setCharAt( i, ' ' );
            }
        }
        return sb.toString();
    }

    /**
     * Assert that "value" is either null or made up of only the acceptable
     * characters.
     * <p/>
     * The "acceptable" characters are Ascii values (33-126 inclusive), with the
     * spaces and newlines tolerated.
     *
     * @param what  "field" was the problem on
     * @param value to check - null unacceptable
     *
     * @return value deNulled for in-lining this method.
     */
    public static String displayable7BitAsciiAllowingSpacesAndNewlines( String what, String value ) {
        value = StringUtils.deNull( value );
        for ( int i = 0; i < value.length(); i++ ) {
            char c = value.charAt( i );
            if ( !CharacterUtils.isDisplayable7BitAsciiAllowingSpaceAndNewline( c ) ) {
                throw new IllegalArgumentException( what + " contained unacceptable character at index " + i + " : '" + value + "'" );
            }
        }
        return value;
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/common/shared/utils/Assert.java

Diff revisions: vs.
Revision Author Commited Message
964 GeorgeS picture GeorgeS Fri 01 Aug, 2014 03:18:23 +0000

!