Subversion Repository Public Repository

litesoft

Diff Revisions 810 vs 811 for /trunk/Java/core/Anywhere/src/org/litesoft/core/typeutils/Objects.java

Diff revisions: vs.
  @@ -182,4 +182,190 @@
182 182 }
183 183 return false;
184 184 }
185 +
186 + /**
187 + * This method strips the package name off a fully qualified class name returning just the substring
188 + * beginning one character beyond the last ".".
189 + *
190 + * @return the substring beginning one character beyond the last "."; null or no "." just returns pFullyQualifiedClassName
191 + */
192 + public static String justClassName( String pFullyQualifiedClassName )
193 + {
194 + int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '.' ) : -1;
195 + return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : pFullyQualifiedClassName;
196 + }
197 +
198 + /**
199 + * This method strips the package name off a fully qualified class name returning just the substring
200 + * beginning one character beyond the last ".".
201 + *
202 + * @return the substring beginning one character beyond the last "."; null or no "." just returns
203 + */
204 + public static String justClassName( Class<?> pClass )
205 + {
206 + return (pClass != null) ? justClassName( pClass.getName() ) : null;
207 + }
208 +
209 + /**
210 + * This method strips the package name off the class name of the pObject returning just the substring
211 + * beginning one character beyond the last ".".
212 + *
213 + * @see Objects#justClassName(Class
214 + */
215 + public static String justClassNameOf( Object pObject )
216 + {
217 + return (pObject != null) ? justClassName( pObject.getClass() ) : null;
218 + }
219 +
220 + /**
221 + * This method strips the package name off the class name of the pObject returning just the substring
222 + * beginning one character beyond the last ".", and if it ends with "Impl" remove that.
223 + *
224 + * @see Objects#justClassName(Class
225 + */
226 + public static String justClassNameNoImplOf( Object pObject )
227 + {
228 + String zName = justClassNameOf( pObject );
229 + return ((zName != null) && zName.endsWith( "Impl" )) ? zName.substring( 0, zName.length() - 4 ) : zName;
230 + }
231 +
232 + public static String justClassNameIfPackage( String pClassName, String pPackage )
233 + {
234 + if ( pClassName != null )
235 + {
236 + if ( (pPackage != null) && pPackage.endsWith( "." ) && (pPackage.length() < pClassName.length()) )
237 + {
238 + String zSimpleName = justClassName( pClassName );
239 + if ( pClassName.equals( pPackage + zSimpleName ) )
240 + {
241 + return zSimpleName;
242 + }
243 + }
244 + }
245 + return pClassName;
246 + }
247 +
248 + public static String justClassNameIfPackage( Class<?> pClass, String pPackage )
249 + {
250 + return (pClass != null) ? justClassNameIfPackage( pClass.getName(), pPackage ) : null;
251 + }
252 +
253 + /**
254 + * This method strips the package name off a fully qualified class name returning just the substring
255 + * beginning one character beyond the last "." (And removes the wrapping Class names if Any).
256 + *
257 + * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName )
258 + */
259 + public static String justSimpleName( String pFullyQualifiedClassName )
260 + {
261 + int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '$' ) : -1;
262 + return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : justClassName( pFullyQualifiedClassName );
263 + }
264 +
265 + /**
266 + * This method strips the package name off the fully qualified class name of the pObject returning just the substring
267 + * beginning one character beyond the last "." (And removes the wrapping Class names if Any).
268 + *
269 + * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName )
270 + */
271 + public static String justSimpleName( Class pClass )
272 + {
273 + return justSimpleName( (pClass != null) ? pClass.getName() : null );
274 + }
275 +
276 + /**
277 + * This method strips the package name off the fully qualified class name of the pObject returning just the substring
278 + * beginning one character beyond the last "." (And removes the wrapping Class names if Any).
279 + *
280 + * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName )
281 + */
282 + public static String justSimpleName( Object pObject )
283 + {
284 + return justSimpleName( (pObject != null) ? pObject.getClass() : null );
285 + }
286 +
287 + public static String classNameOf( Object pObject )
288 + {
289 + return (pObject != null) ? pObject.getClass().getName() : null;
290 + }
291 +
292 + /**
293 + * @param pCount - < 1 means copy nothing
294 + * @param pTo - is NOT extended
295 + * @param pFromIndex - 0 based, < 0 indicates from end (eg -1 == Last)
296 + * @param pToIndex - 0 based, < 0 indicates from end (eg -1 == Last)
297 + *
298 + * @return pTo
299 + */
300 + public static Object[] copySubArrayTo( int pCount, //
301 + Object[] pFrom, int pFromIndex, //
302 + Object[] pTo, int pToIndex )
303 + {
304 + if ( (pCount > 0) && //
305 + isNotNullOrEmpty( pFrom ) && (pFromIndex < pFrom.length) && //
306 + isNotNullOrEmpty( pTo ) && (pToIndex < pTo.length) )
307 + {
308 + pCount = lesserOf( pCount, pTo, pToIndex = unNegateIndex( pTo, pToIndex ) );
309 + pCount = lesserOf( pCount, pFrom, pFromIndex = unNegateIndex( pFrom, pFromIndex ) );
310 + while ( pCount-- > 0 )
311 + {
312 + pTo[pToIndex++] = pFrom[pFromIndex++];
313 + }
314 + }
315 + return pTo;
316 + }
317 +
318 + private static int lesserOf( int pCount, Object[] pObjects, int pIndex )
319 + {
320 + if ( pCount > 0 )
321 + {
322 + int zMoveLen = pObjects.length - pIndex;
323 + if ( zMoveLen < pCount )
324 + {
325 + return zMoveLen;
326 + }
327 + }
328 + return pCount;
329 + }
330 +
331 + private static int unNegateIndex( Object[] pFrom, int pFromIndex )
332 + {
333 + if ( pFromIndex < 0 )
334 + {
335 + if ( (pFromIndex = pFrom.length - pFromIndex) < 0 )
336 + {
337 + pFromIndex = 0;
338 + }
339 + }
340 + return pFromIndex;
341 + }
342 +
343 + /**
344 + * @param pTo - is NOT extended
345 + * @param pToIndex - 0 based, < 0 indicates from end (eg -1 == Last)
346 + *
347 + * @return pTo
348 + */
349 + public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo, int pToIndex )
350 + {
351 + if ( (pFrom != null) && (pTo != null) )
352 + {
353 + copySubArrayTo( Math.min( pFrom.length, pTo.length - pToIndex ), pFrom, 0, pTo, pToIndex );
354 + }
355 + return pTo;
356 + }
357 +
358 + /**
359 + * @param pTo - is NOT extended
360 + *
361 + * @return pTo
362 + */
363 + public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo )
364 + {
365 + if ( (pFrom != null) && (pTo != null) )
366 + {
367 + copySubArrayTo( Math.min( pFrom.length, pTo.length ), pFrom, 0, pTo, 0 );
368 + }
369 + return pTo;
370 + }
185 371 }