Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## de.jstacs.io
Class ArrayHandler

    java.lang.Object
      de.jstacs.io.ArrayHandler

    public final class ArrayHandler

extends Object

This class handles arrays with elements of generic type and enables the user to cast and clone arrays easily.

Author:
  Jens Keilwagen

Constructor Summary
`ArrayHandler()`

Method Summary
`static
`<S> S[]`
` | `cast(Class<? extends S> c, Object[] o)` This method returns an array of a user-specified class with all elements in the given array `o`.
`static Object[]` | `cast(Object[] o)` This method creates a new array of the superclass of all elements of the given array and copies the elements.
`static
`<T extends Cloneable> T[]`
` | `clone(T... t)` This method returns a deep copy of a (multi-dimensional) array of `Cloneable`s or primitives.
`static
`<T extends Cloneable> T[]`
` | `createArrayOf(T t, int l)` This method returns an array of length `l` that has at each position a clone of `t`.
`static
`<T> Class`
` | `getSuperClassOf(T... o)` This method returns the deepest class in the class hierarchy that is the class or a superclass of all instances in the array.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### ArrayHandler

    public ArrayHandler()

Method Detail

### getSuperClassOf

    public static <T> Class getSuperClassOf(T... o)

  This method returns the deepest class in the class hierarchy that is the class or a superclass of all instances in the array.

  Type Parameters:
    `T` - the type of the array elements
  Parameters:
    `o` - the array
  Returns:
    the superclass of all elements of the given array

### cast

    public static Object[] cast(Object[] o)

  This method creates a new array of the superclass of all elements of the given array and copies the elements. The order of the elements is not changed.
  Here is an example that demonstrates what can be done:
  `Object[] o = { new UniformModel( alphabetContainer ), new UniformModel( alphabetContainer ) };
  AbstractModel[] a = (AbstractModel[]) ArrayHandler.cast( o );
  `
  This should work fine, while
  `AbstractModel[] a = (AbstractModel[]) o;`
  will not.

  Parameters:
    `o` - the array
  Returns:
    the casted array with the copied elements
  See Also:
    `getSuperClassOf(Object[])`, `cast(Class, Object[])`

### cast

    public static <S> S[] cast(Class<? extends S> c,
                               Object[] o)

  This method returns an array of a user-specified class with all elements in the given array `o`. If the given array is already from the correct class it is directly returned, otherwise a new array is created and filled with the elements of `o`. In both cases, the order of the elements is not changed.
  Here is an example that demonstrates what can be done:
  `Object[] o = { new UniformModel( alphabetContainer ), new UniformModel( alphabetContainer ) };
  AbstractModel[] a = ArrayHandler.cast( AbstractModel.class, o );
  `
  This should work fine, while
  `AbstractModel[] a = (AbstractModel[]) o;`
  will not.

  Type Parameters:
    `S` - the type of the array elements
  Parameters:
    `c` - the class of the array items
    `o` - the array
  Returns:
    the casted array with the copied elements
  See Also:
    `getSuperClassOf(Object[])`

### clone

    public static <T extends Cloneable> T[] clone(T... t)
                                       throws CloneNotSupportedException

  This method returns a deep copy of a (multi-dimensional) array of `Cloneable`s or primitives. For arrays of `Cloneable`s, each element of the array is cloned using its `clone()`-method.

  Type Parameters:
    `T` - the type of the array elements
  Parameters:
    `t` - the array
  Returns:
    a deep copy of the given array
  Throws:
    `CloneNotSupportedException` - if an element could not be cloned
  See Also:
    `Cloneable`

### createArrayOf

    public static <T extends Cloneable> T[] createArrayOf(T t,
                                                          int l)
                                               throws CloneNotSupportedException

  This method returns an array of length `l` that has at each position a clone of `t`.

  Type Parameters:
    `T` - the type of the array elements that implements `Cloneable`
  Parameters:
    `t` - the original element
    `l` - the dimension of the new array
  Returns:
    an array of length `l` that has at each position a clone of `t`
  Throws:
    `CloneNotSupportedException` - if `t` could not be cloned
  See Also:
    `Cloneable`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD