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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.litesoft.core.typeutils;

public class Integers extends Numerics
{
    public static final int[] EMPTY_ARRAY_PRIMITIVES = new int[0];
    public static final Integer[] EMPTY_ARRAY = new Integer[0];

    public static int deNull( Integer pValue )
    {
        return (pValue != null) ? pValue : 0;
    }

    public static Integer assertNonNegative( String pName, Integer pValue )
            throws IllegalArgumentException
    {
        if ( pValue != null )
        {
            assertNonNegative( pName, pValue.intValue() );
        }
        return pValue;
    }

    public static int assertNonNegative( String pName, int pValue )
            throws IllegalArgumentException
    {
        if ( pValue < 0 )
        {
            throw new IllegalArgumentException( pName + CANNOT_BE_NEGATIVE + ", but was: " + pValue );
        }
        return pValue;
    }

    public static int assertPositive( String pWhat, int pValue )
    {
        if ( 0 < pValue )
        {
            return pValue;
        }
        throw new IllegalArgumentException( pWhat + MUST_BE_POSITIVE + ", but was: " + pValue );
    }

    public static void assertNotEqual( String pObjectName, int pNotExpected, int pActual )
            throws IllegalArgumentException
    {
        if ( pNotExpected == pActual )
        {
            throw new IllegalArgumentException( pObjectName + ": '" + pNotExpected + "' Not allowed" );
        }
    }

    public static void assertEqual( String pObjectName, int pExpected, int pActual )
            throws IllegalArgumentException
    {
        if ( pExpected != pActual )
        {
            throw new IllegalArgumentException( pObjectName + ": Expected '" + pExpected + "', but was '" + pActual + "'" );
        }
    }

    public static String zeroPadIt( int pMinDesiredLength, int pIt )
    {
        String rv = String.valueOf( pIt );
        while ( rv.length() < pMinDesiredLength )
        {
            rv = "0" + rv;
        }
        return rv;
    }

    public static String padIt( int pMinDesiredLength, int pIt )
    {
        return Strings.padIt( pMinDesiredLength, "" + pIt );
    }

    public static String iTpad( int pIt, int pMinDesiredLength )
    {
        return Strings.iTpad( "" + pIt, pMinDesiredLength );
    }

    public static boolean isNullOrEmpty( int[] pArrayToCheck )
    {
        return (pArrayToCheck == null || pArrayToCheck.length == 0);
    }

    public static boolean areArraysEqual( int[] pThis, int[] pThat )
    {
        if ( pThis == pThat ) // handles if both are null
        {
            return true;
        }
        if ( (pThis != null) && (pThat != null) && (pThis.length == pThat.length) )
        {
            for ( int i = pThis.length; --i >= 0; )
            {
                if ( pThis[i] != pThat[i] )
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public static boolean checkIndex( int pIndex, int pMax, boolean pInclusive )
    {
        return ((0 <= pIndex) && (pIndex < pMax)) || (pInclusive && (pIndex == pMax));
    }

    public static void validateIndex( int pIndex, int pMax, boolean pInclusive )
            throws IllegalArgumentException
    {
        if ( !checkIndex( pIndex, pMax, pInclusive ) )
        {
            throw new IllegalArgumentException( "!( 0 <= " + pIndex + " " + (pInclusive ? "<=" : "<") + " " + pMax + " )" );
        }
    }

    /**
     * return pIndex constained between 0 (inclusive) and pMax (pInclusive).  If the effective 'max' is less than 0, then 0 will be returned.
     */
    public static int constrainIndex( int pIndex, int pMax, boolean pInclusive )
    {
        if ( (pMax <= pIndex) || (pInclusive && (pIndex == pMax)) )
        {
            pIndex = pInclusive ? pMax : pMax - 1;
        }
        return (pIndex < 0) ? 0 : pIndex;
    }

    public static String getMax2PlacePercentage( int pPortions )
    {
        if ( pPortions == 0 )
        {
            return null;
        }
        int portion = 10000 / pPortions;
        String s = "" + portion;
        while ( s.length() < 3 )
        {
            s = "0" + s;
        }
        int wholeLen = s.length() - 2;
        s = s.substring( 0, wholeLen ) + "." + s.substring( wholeLen );
        while ( s.charAt( s.length() - 1 ) == '0' )
        {
            s = s.substring( 0, s.length() - 1 );
        }
        if ( s.charAt( s.length() - 1 ) == '.' )
        {
            s = s.substring( 0, s.length() - 1 );
        }
        return s + "%";
    }

    public static boolean areNonArraysEqual( int pThis, int pThat )
    {
        return (pThis == pThat);
    }

    /**
     * Returns a String that represents <i>pThis</i> with english code tail,
     * such as "3rd".<p>
     * <p/>
     * If <i>pThis</i> is non-negative, then append on the appropriate two
     * letter <i>english</i> code:<p>
     * <pre>
     *      st,nd,rd,th
     * </pre><p>
     *
     * @param pThis the int to convert.<p>
     *
     * @return the String representation for <i>pThis</i> and if not negative,
     *         then appended with the appropriate two letter <i>english</i>
     *         junk.<p>
     */
    public static String toNth( int pThis )
    {
        String rv = Integer.toString( pThis );
        if ( pThis < 0 )
        {
            return rv;
        }

        if ( (pThis < 10) || ('1' != rv.charAt( rv.length() - 2 )) ) // not: 10-19 & 110-119, 210-219, ... 1010-1019, ...
        {
            switch ( rv.charAt( rv.length() - 1 ) )
            {
                case '1':
                    return rv + "st";
                case '2':
                    return rv + "nd";
                case '3':
                    return rv + "rd";
                default: // 0th and n0th and 4th - 9th and n4th - n9th
                    break;
            }
        }
        return rv + "th";
    }

    public static int insureNonNegative( int pValue )
    {
        return (pValue < 0) ? 0 : pValue;
    }

    public static int intValue( Integer pInteger, int pDefault )
    {
        return (pInteger != null) ? pInteger : pDefault;
    }

    public static int intValue( long pValue )
    {
        if ( (Integer.MIN_VALUE <= pValue) && (pValue <= Integer.MAX_VALUE) )
        {
            return (int) pValue;
        }
        throw new IllegalArgumentException( "Value (" + pValue + ") is outside the acceptable Integer Range" );
    }

    public static boolean isEven( int pInt )
    {
        return ((pInt & 1) == 0);
    }

    public static boolean isOdd( int pInt )
    {
        return ((pInt & 1) == 1);
    }
}

Commits for litesoft/trunk/DeviceDesktopTest/src/org/litesoft/core/typeutils/Integers.java

Diff revisions: vs.
Revision Author Commited Message
936 GeorgeS picture GeorgeS Sun 01 Jun, 2014 20:19:38 +0000

Language Support