FileDocCategorySizeDatePackage
ArrayConversion.javaAPI DocGlassfish v2 API7625Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

ArrayConversion

public final class ArrayConversion extends Object
Provides: - methods to convert arrays of primitive types to corresponding arrays of Object types - conversion to/from Set

Fields Summary
Constructors Summary
private ArrayConversion()

		// disallow instantiation
	
Methods Summary
public static java.util.SetarrayToSet(T[] names)

		final Set<T>	set	= new HashSet<T>();
		
		for( int i = 0; i < names.length; ++i )
		{
			set.add( names[ i ] );
		}

		return( set );
	
private static java.lang.Object[]convert(java.lang.Object simpleArray)

		if ( ! ClassUtil.objectIsPrimitiveArray( simpleArray ) )
		{
			throw new IllegalArgumentException();
		}
		
		final String className	= simpleArray.getClass().getName();
		
		final Class		theClass = ClassUtil.getArrayElementClass( simpleArray.getClass() );
		
		final int numItems	= Array.getLength( simpleArray );
		
		final Class elementClass	= ClassUtil.PrimitiveClassToObjectClass( theClass );
		
		final Object []	result	= (Object [])Array.newInstance( elementClass, numItems );
		
		for( int i = 0; i < numItems; ++i )
		{
			result[ i ]	= Array.get( simpleArray, i );
		}
		
		return( result );
	
public static java.lang.Object[]createObjectArrayType(java.lang.Class elementType, int size)
Create an array whose type is elementType[] of specified size.

param
elementType the type of each entry of the array
param
size the number of elements

		final Object [] result	= (Object []) Array.newInstance( elementType, size );
		
		return( result );
	
public static booleanhasIdenticalElementClasses(java.lang.Object[] a)

		boolean	isUniform	= true;
		
		if ( a.length > 0 )
		{
			final Class		matchType	= a[ 0 ].getClass();
			
			for( int i = 1; i < a.length; ++i )
			{
				if ( a[ i ].getClass() != matchType )
				{
					isUniform	= false;
					break;
				}
			}
		}
		
		return( isUniform );
	
public static java.lang.Object[]setToArray(java.util.Set s, boolean specialize)
Convert a Set to an array. If specialize is true, then provide the most specialized type possible via specializeArray()

param
s the Set to convert
param
specialize decide whether to specialize the type or not

		Object []	result	= setToArray( s );
		
		if ( specialize && result.length != 0)
		{
			result	= specializeArray( result );
		}
		
		return( result );
	
public static java.lang.Object[]setToArray(java.util.Set s)
Convert a Set to an Object[].

param
s the Set to convert

		final Object []	out	= new Object [ s.size() ];
		
		setToArray( s, out );
		
		return( out );
	
public static java.lang.Object[]setToArray(java.util.Set s, java.lang.Object[] out)
Convert a Set to an Object[].

param
s the Set to convert
param
out the output array, must be of size s.size()

		if ( out.length != s.size() )
		{
			throw new IllegalArgumentException();
		}
		
		int	i = 0;
		for( final Object o : s )
		{
			out[ i ]	= o;
			++i;
		}
		
		return( out );
	
public static java.lang.Object[]specializeArray(java.lang.Object[] a)
Specialize the type of the array (if possible). For example, if the array is an Object[] of Integer, return an Integer[] of Integer.

param
a the array to specialize
return
a specialized array (if possible) otherwise the original array

		Object[]	result	= a;
		
		if ( hasIdenticalElementClasses( a ) &&
			a.length != 0 &&
			a.getClass() == Object[].class )
		{
			result	= createObjectArrayType( a[0].getClass(), a.length );
		
			System.arraycopy( a, 0, result, 0, a.length );
		}
		
		return( result );
	
public static java.lang.Object[]subArray(java.lang.Object[] in, int start, int end)

		final int		count	= 1 + (end - start);
		final Object[]	result	= (Object[])
			Array.newInstance( ClassUtil.getArrayElementClass( in.getClass() ), count );
		
		for( int i = 0; i < count; ++i )
		{
			result[ i ]	= in[ i + start ];
		}
		
		return( result );
	
public static java.lang.Object[]toAppropriateType(java.lang.Object array)
Convert an an array of primitive types to an array of Objects of non-primitive types eg int to Integer.

param
array the array to convert

		return( (Object [])convert( array ) );
	
public static java.lang.Boolean[]toBooleans(boolean[] array)

		return( (Boolean [])convert( array ) );
	
public static java.lang.Byte[]toBytes(byte[] array)

		return( (Byte [])convert( array ) );
	
public static java.lang.Character[]toCharacters(char[] array)

		return( (Character [])convert( array ) );
	
public static java.lang.Double[]toDoubles(double[] array)

		return( (Double [])convert( array ) );
	
public static java.lang.Float[]toFloats(float[] array)

		return( (Float [])convert( array ) );
	
public static java.lang.Integer[]toIntegers(int[] array)

		return( (Integer [])convert( array ) );
	
public static java.lang.Long[]toLongs(long[] array)

		return( (Long [])convert( array ) );
	
public static java.util.SettoSet(T[] array)

		Set<T>	theSet	= null;
		if ( array.length == 0 )
		{
			theSet	= Collections.emptySet();
		}
		else if ( array.length == 1 )
		{
			theSet	= Collections.singleton( array[ 0 ] );
		}
		else
		{
			theSet	= new HashSet<T>();
			for( int i = 0; i < array.length; ++i )
			{
				theSet.add( array[ i ] );
			}
		}
		return( theSet );
	
public static java.lang.Short[]toShorts(short[] array)

		return( (Short [])convert( array ) );