From: <kon...@us...> - 2010-12-20 22:16:14
|
Revision: 1180 http://cishell.svn.sourceforge.net/cishell/?rev=1180&view=rev Author: kongchinhua Date: 2010-12-20 22:16:06 +0000 (Mon, 20 Dec 2010) Log Message: ----------- - Add ArrayUtilities.copyOf() to support expend array while copy - Use Arrays.copyOf instead of ArrayUtilities.copyOf due to JRE1.5 doesn't support Arrays.copyOf - Fixed style guide warnings in ArrayUtilities.java - Reviewed by Joseph Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java 2010-12-16 22:44:07 UTC (rev 1179) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java 2010-12-20 22:16:06 UTC (rev 1180) @@ -3,7 +3,11 @@ import java.util.Iterator; import java.util.List; -public class ArrayUtilities { +public final class ArrayUtilities { + + private ArrayUtilities() { + } + public static int indexOf(Object[] array, Object target) { for (int ii = 0; ii < array.length; ii++) { if (array[ii].equals(target)) { @@ -14,12 +18,12 @@ return -1; } - public static void swapFirstMatchToFront(Object[] array, List targets) { - for (Iterator targetsIt = targets.iterator(); targetsIt.hasNext();) { + public static void swapFirstMatchToFront(Object[] array, List<?> targets) { + for (Iterator<?> targetsIt = targets.iterator(); targetsIt.hasNext();) { Object target = targetsIt.next(); int index = ArrayUtilities.indexOf(array, target); - if ( index != -1 ) { + if (index != -1) { swap(array, 0, index); return; } @@ -42,6 +46,31 @@ return clone; } + + /** + * This method will return a shadow copy of the given array with the desired size. + * If the given disiredSize exceeds the original array size, the remaining items + * will be filled with empty string. + * @param array - The original array to be copied + * @param desiredSize - The desired size of the new array + * @return The new copy of the original array with the desired size + */ + public static String[] copyOf(String[] array, int desiredSize) { + String[] newArray = new String[desiredSize]; + int copiedSize = Math.min(array.length, desiredSize); + + /* Copy items from the original array to new array */ + for (int i = 0; i < copiedSize; i++) { + newArray[i] = array[i]; + } + + /* Fill the extra items will empty string if there are */ + for (int i = copiedSize; i < desiredSize; i++) { + newArray[i] = ""; + } + + return newArray; + } // TODO: Find a better place to put this? public static <T> boolean allAreNull(T... objects) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |