FileDocCategorySizeDatePackage
ArrayHelper.javaAPI DocHibernate 3.2.58785Tue Jul 19 13:17:16 BST 2005org.hibernate.util

ArrayHelper

public final class ArrayHelper extends Object

Fields Summary
public static final boolean[]
TRUE
public static final boolean[]
FALSE
public static final String[]
EMPTY_STRING_ARRAY
public static final int[]
EMPTY_INT_ARRAY
public static final boolean[]
EMPTY_BOOLEAN_ARRAY
public static final Class[]
EMPTY_CLASS_ARRAY
public static final Object[]
EMPTY_OBJECT_ARRAY
public static final org.hibernate.type.Type[]
EMPTY_TYPE_ARRAY
private static int
SEED
private static int
PRIME_NUMER
Constructors Summary
private ArrayHelper()


	  
Methods Summary
public static voidaddAll(java.util.Collection collection, java.lang.Object[] array)

		for ( int i=0; i<array.length; i++ ) {
			collection.add( array[i] );
		}
	
public static intcountTrue(boolean[] array)

		int result=0;
		for ( int i=0; i<array.length; i++ ) {
			if ( array[i] ) result++;
		}
		return result;
	
public static java.lang.String[]fillArray(java.lang.String value, int length)

		String[] result = new String[length];
		Arrays.fill(result, value);
		return result;
	
public static int[]fillArray(int value, int length)

		int[] result = new int[length];
		Arrays.fill(result, value);
		return result;
	
public static org.hibernate.LockMode[]fillArray(org.hibernate.LockMode lockMode, int length)

		LockMode[] array = new LockMode[length];
		Arrays.fill(array, lockMode);
		return array;
	
public static int[]getBatchSizes(int maxBatchSize)

	
	     
		int batchSize = maxBatchSize;
		int n=1;
		while ( batchSize>1 ) {
			batchSize = getNextBatchSize(batchSize);
			n++;
		}
		int[] result = new int[n];
		batchSize = maxBatchSize;
		for ( int i=0; i<n; i++ ) {
			result[i] = batchSize;
			batchSize = getNextBatchSize(batchSize);
		}
		return result;
	
private static intgetNextBatchSize(int batchSize)

		if (batchSize<=10) {
			return batchSize-1; //allow 9,8,7,6,5,4,3,2,1
		}
		else if (batchSize/2 < 10) {
			return 10;
		}
		else {
			return batchSize / 2;
		}
	
public static inthash(java.lang.Object[] array)
calculate the array hash (only the first level)


	        	 
	     
		int length = array.length;
		int seed = SEED;
		for (int index = 0 ; index < length ; index++) {
			seed = hash( seed, array[index] == null ? 0 : array[index].hashCode() );
		}
		return seed;
	
public static inthash(char[] array)
calculate the array hash (only the first level)

		int length = array.length;
		int seed = SEED;
		for (int index = 0 ; index < length ; index++) {
			seed = hash( seed, (int) array[index] ) ;
		}
		return seed;
	
public static inthash(byte[] bytes)
calculate the array hash (only the first level)

		int length = bytes.length;
		int seed = SEED;
		for (int index = 0 ; index < length ; index++) {
			seed = hash( seed, (int) bytes[index] ) ;
		}
		return seed;
	
private static inthash(int seed, int i)

		return PRIME_NUMER * seed + i;
	
public static intindexOf(java.lang.Object[] array, java.lang.Object object)

		for ( int i=0; i<array.length; i++ ) {
			if ( array[i].equals(object) ) return i;
		}
		return -1;
	
public static booleanisAllFalse(boolean[] array)

		for ( int i=0; i<array.length; i++ ) {
			if ( array[i] ) return false;
		}
		return true;
	
public static booleanisAllNegative(int[] array)

		for ( int i=0; i<array.length; i++ ) {
			if ( array[i] >=0 ) return false;
		}
		return true;
	
public static booleanisAllTrue(boolean[] array)

		for ( int i=0; i<array.length; i++ ) {
			if ( !array[i] ) return false;
		}
		return true;
	
public static booleanisEquals(java.lang.Object[] o1, java.lang.Object[] o2)
Compare 2 arrays only at the first level

		if (o1 == o2) return true;
		if (o1 == null || o2 == null) return false;
		int length = o1.length;
		if (length != o2.length) return false;
		for (int index = 0 ; index < length ; index++) {
			if ( ! o1[index].equals( o2[index] ) ) return false;
		}
        return true;
	
public static booleanisEquals(char[] o1, char[] o2)
Compare 2 arrays only at the first level

		if (o1 == o2) return true;
		if (o1 == null || o2 == null) return false;
		int length = o1.length;
		if (length != o2.length) return false;
		for (int index = 0 ; index < length ; index++) {
			if ( ! ( o1[index] == o2[index] ) ) return false;
		}
        return true;
	
public static booleanisEquals(byte[] b1, byte[] b2)
Compare 2 arrays only at the first level

		if (b1 == b2) return true;
		if (b1 == null || b2 == null) return false;
		int length = b1.length;
		if (length != b2.length) return false;
		for (int index = 0 ; index < length ; index++) {
			if ( ! ( b1[index] == b2[index] ) ) return false;
		}
        return true;
	
public static java.lang.String[]join(java.lang.String[] x, java.lang.String[] y)

		String[] result = new String[ x.length + y.length ];
		for ( int i=0; i<x.length; i++ ) result[i] = x[i];
		for ( int i=0; i<y.length; i++ ) result[i+x.length] = y[i];
		return result;
	
public static java.lang.String[]join(java.lang.String[] x, java.lang.String[] y, boolean[] use)

		String[] result = new String[ x.length + countTrue(use) ];
		for ( int i=0; i<x.length; i++ ) result[i] = x[i];
		int k = x.length;
		for ( int i=0; i<y.length; i++ ) {
			if ( use[i] ) result[k++] = y[i];
		}
		return result;
	
public static int[]join(int[] x, int[] y)

		int[] result = new int[ x.length + y.length ];
		for ( int i=0; i<x.length; i++ ) result[i] = x[i];
		for ( int i=0; i<y.length; i++ ) result[i+x.length] = y[i];
		return result;
	
public static java.lang.String[]slice(java.lang.String[] strings, int begin, int length)

		String[] result = new String[length];
		for ( int i=0; i<length; i++ ) {
			result[i] = strings[begin+i];
		}
		return result;
	
public static java.lang.Object[]slice(java.lang.Object[] objects, int begin, int length)

		Object[] result = new Object[length];
		for ( int i=0; i<length; i++ ) {
			result[i] = objects[begin+i];
		}
		return result;
	
public static int[][]to2DIntArray(java.util.Collection coll)

		return (int[][]) coll.toArray( new int[ coll.size() ][] );
	
public static java.lang.String[][]to2DStringArray(java.util.Collection coll)

		return (String[][]) coll.toArray( new String[ coll.size() ][] );
	
public static boolean[]toBooleanArray(java.util.Collection coll)

		Iterator iter = coll.iterator();
		boolean[] arr = new boolean[ coll.size() ];
		int i=0;
		while( iter.hasNext() ) {
			arr[i++] = ( (Boolean) iter.next() ).booleanValue();
		}
		return arr;
	
public static int[]toIntArray(java.util.Collection coll)

		Iterator iter = coll.iterator();
		int[] arr = new int[ coll.size() ];
		int i=0;
		while( iter.hasNext() ) {
			arr[i++] = ( (Integer) iter.next() ).intValue();
		}
		return arr;
	
public static java.util.ListtoList(java.lang.Object array)

		if ( array instanceof Object[] ) return Arrays.asList( (Object[]) array ); //faster?
		int size = Array.getLength(array);
		ArrayList list = new ArrayList(size);
		for (int i=0; i<size; i++) {
			list.add( Array.get(array, i) );
		}
		return list;
	
public static java.util.ListtoList(java.util.Iterator iter)

		List list = new ArrayList();
		while ( iter.hasNext() ) {
			list.add( iter.next() );
		}
		return list;
	
public static java.lang.StringtoString(java.lang.Object[] array)

		StringBuffer sb = new StringBuffer();
		sb.append("[");
		for (int i = 0; i < array.length; i++) {
			sb.append( array[i] );
			if( i<array.length-1 ) sb.append(",");
		}
		sb.append("]");
		return sb.toString();
	
public static java.lang.String[]toStringArray(java.lang.Object[] objects)

		int length=objects.length;
		String[] result = new String[length];
		for (int i=0; i<length; i++) {
			result[i] = objects[i].toString();
		}
		return result;
	
public static java.lang.String[]toStringArray(java.util.Collection coll)

		return (String[]) coll.toArray(EMPTY_STRING_ARRAY);
	
public static org.hibernate.type.Type[]toTypeArray(java.util.Collection coll)

		return (Type[]) coll.toArray(EMPTY_TYPE_ARRAY);
	
public static java.lang.Object[]typecast(java.lang.Object[] array, java.lang.Object[] to)

		return java.util.Arrays.asList(array).toArray(to);