Subversion Repository Public Repository

litesoft

Diff Revisions 818 vs 819 for /trunk/Java/core/Anywhere/src/org/litesoft/core/typeutils/Integers.java

Diff revisions: vs.
  @@ -87,4 +87,111 @@
87 87 }
88 88 return false;
89 89 }
90 +
91 + public static boolean checkIndex( int pIndex, int pMax, boolean pInclusive )
92 + {
93 + return ((0 <= pIndex) && (pIndex < pMax)) || (pInclusive && (pIndex == pMax));
94 + }
95 +
96 + public static void validateIndex( int pIndex, int pMax, boolean pInclusive )
97 + throws IllegalArgumentException
98 + {
99 + if ( !checkIndex( pIndex, pMax, pInclusive ) )
100 + {
101 + throw new IllegalArgumentException( "!( 0 <= " + pIndex + " " + (pInclusive ? "<=" : "<") + " " + pMax + " )" );
102 + }
103 + }
104 +
105 + /**
106 + * return pIndex constained between 0 (inclusive) and pMax (pInclusive). If the effective 'max' is less than 0, then 0 will be returned.
107 + */
108 + public static int constrainIndex( int pIndex, int pMax, boolean pInclusive )
109 + {
110 + if ( (pMax <= pIndex) || (pInclusive && (pIndex == pMax)) )
111 + {
112 + pIndex = pInclusive ? pMax : pMax - 1;
113 + }
114 + return (pIndex < 0) ? 0 : pIndex;
115 + }
116 +
117 + public static String getMax2PlacePercentage( int pPortions )
118 + {
119 + if ( pPortions == 0 )
120 + {
121 + return null;
122 + }
123 + int portion = 10000 / pPortions;
124 + String s = "" + portion;
125 + while ( s.length() < 3 )
126 + {
127 + s = "0" + s;
128 + }
129 + int wholeLen = s.length() - 2;
130 + s = s.substring( 0, wholeLen ) + "." + s.substring( wholeLen );
131 + while ( s.charAt( s.length() - 1 ) == '0' )
132 + {
133 + s = s.substring( 0, s.length() - 1 );
134 + }
135 + if ( s.charAt( s.length() - 1 ) == '.' )
136 + {
137 + s = s.substring( 0, s.length() - 1 );
138 + }
139 + return s + "%";
140 + }
141 +
142 + public static boolean areNonArraysEqual( int pThis, int pThat )
143 + {
144 + return (pThis == pThat);
145 + }
146 +
147 + /**
148 + * Returns a String that represents <i>pThis</i> with english code tail,
149 + * such as "3rd".<p>
150 + * <p/>
151 + * If <i>pThis</i> is non-negative, then append on the appropriate two
152 + * letter <i>english</i> code:<p>
153 + * <pre>
154 + * st,nd,rd,th
155 + * </pre><p>
156 + *
157 + * @param pThis the int to convert.<p>
158 + *
159 + * @return the String representation for <i>pThis</i> and if not negative,
160 + * then appended with the appropriate two letter <i>english</i>
161 + * junk.<p>
162 + */
163 + public static String toNth( int pThis )
164 + {
165 + String rv = Integer.toString( pThis );
166 + if ( pThis < 0 )
167 + {
168 + return rv;
169 + }
170 +
171 + if ( (pThis < 10) || ('1' != rv.charAt( rv.length() - 2 )) ) // not: 10-19 & 110-119, 210-219, ... 1010-1019, ...
172 + {
173 + switch ( rv.charAt( rv.length() - 1 ) )
174 + {
175 + case '1':
176 + return rv + "st";
177 + case '2':
178 + return rv + "nd";
179 + case '3':
180 + return rv + "rd";
181 + default: // 0th and n0th and 4th - 9th and n4th - n9th
182 + break;
183 + }
184 + }
185 + return rv + "th";
186 + }
187 +
188 + public static int insureNonNegative( int pValue )
189 + {
190 + return (pValue < 0) ? 0 : pValue;
191 + }
192 +
193 + public static int intValue( Integer pInteger, int pDefault )
194 + {
195 + return (pInteger != null) ? pInteger : pDefault;
196 + }
90 197 }