jarrayliterals
Class ArrayShortcuts

java.lang.Object
  extended by jarrayliterals.ArrayShortcuts

public class ArrayShortcuts
extends java.lang.Object

Shortcuts for creating arbitrary arrays (e.g. for parameterized unit tests).

This class has two overloaded method. The $(T...): T[] method is used for creating an array of a generic element type and expects a non-zero, variable amount of arguments. The $(T): T[] method is used for creating a higher-dimension array of a generic element type and expects a single argument.
This class has fields that start with $. The fields represent zero-sized arrays of a specific element type.
Note that both the methods and fields return boxed wrapper arrays instead of their respective, primitive data type arrays.

Be sure to use $null instead of the regular null when nesting arrays.
Additionally, there are methods which automatically cast int arguments to Bytes $B(int...): Byte[] or shorts $S(int...): Short[]. Be careful that these 2 methods trade performance for brevity.

An utility method toString which prints anything (including multidimensional arrays) is available.

Author:
mucaho

Field Summary
static java.lang.Object $
          Shortcut for an empty Object[0] array.
static java.lang.Object $b
          Shortcut for an empty Boolean[0] array.
static java.lang.Object $B
          Shortcut for an empty Byte[0] array.
static java.lang.Object $C
          Shortcut for an empty Character[0] array.
static java.lang.Object $D
          Shortcut for an empty Double[0] array.
static java.lang.Object $F
          Shortcut for an empty Float[0] array.
static java.lang.Object $I
          Shortcut for an empty Integer[0] array.
static java.lang.Object $L
          Shortcut for an empty Long[0] array.
static java.lang.Void $null
          Shortcut for null.
static java.lang.Object $S
          Shortcut for an empty Short[0] array.
 
Constructor Summary
ArrayShortcuts()
           
 
Method Summary
static java.lang.Object $()
          Shortcut for returning an empty array of objects.
static
<T> java.lang.Object
$(T... params)
          Shortcut for returning an array containing the parameters(s).
static
<T> java.lang.Object
$(T param)
          Shortcut for returning a higher dimension array containing only the parameter.
static java.lang.Object $B(int... params)
          Shortcut for returning an array of Bytes.
static java.lang.Object $S(int... params)
          Shortcut for returning an array of Shorts.
static java.lang.String toString(java.lang.Object arr)
          Prints a generic object (this includes single-/multi-dimensional arrays).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

$null

public static final java.lang.Void $null
Shortcut for null. Use this instead of the regular null when nesting arrays.


$

public static final java.lang.Object $
Shortcut for an empty Object[0] array.


$B

public static final java.lang.Object $B
Shortcut for an empty Byte[0] array.
Do not confuse with $b. B stands for "Byte".


$S

public static final java.lang.Object $S
Shortcut for an empty Short[0] array.


$I

public static final java.lang.Object $I
Shortcut for an empty Integer[0] array.


$L

public static final java.lang.Object $L
Shortcut for an empty Long[0] array.


$F

public static final java.lang.Object $F
Shortcut for an empty Float[0] array.


$D

public static final java.lang.Object $D
Shortcut for an empty Double[0] array.


$b

public static final java.lang.Object $b
Shortcut for an empty Boolean[0] array.
Do not confuse with $B. b stands for "bit".


$C

public static final java.lang.Object $C
Shortcut for an empty Character[0] array.

Constructor Detail

ArrayShortcuts

public ArrayShortcuts()
Method Detail

toString

public static java.lang.String toString(java.lang.Object arr)
Prints a generic object (this includes single-/multi-dimensional arrays). In essence prints anything (as long as it has the toString() implemented).
Numbers are suffixed with the first capital letter of their respective types.

Parameters:
arr - anything
Returns:
String representation of arr

$

public static <T> java.lang.Object $(T param)
Shortcut for returning a higher dimension array containing only the parameter. Note that the parameter itself can be an array.

Parameters:
param - A single parameter.
Returns:
Values Array containing the parameter only.

$

public static <T> java.lang.Object $(T... params)
Shortcut for returning an array containing the parameters(s).
(1) Parameters are interpreted as a list of parameters. In this case the parameters are returned as an array of those parameters. Note that the parameters themselves can be arrays.
(2) Parameter is interpreted as a single array. In that case the same array will be returned. However, if you want to create a higher dimension array instead, you can call the other method by casting the array to an Object $( (Object) params ).

Parameters:
params - (1) A list of parameters OR (2) A single parameter array.
Returns:
(1) Array containing the list of parameters OR (2) The single parameter array.

$B

public static java.lang.Object $B(int... params)
Shortcut for returning an array of Bytes. All parameters passed to this method are returned in an Byte[] array.

Parameters:
params - Values to be returned in an Byte[] array.
Returns:
Values passed to this method.

$S

public static java.lang.Object $S(int... params)
Shortcut for returning an array of Shorts. All parameters passed to this method are returned in an Short[] array.

Parameters:
params - Values to be returned in an Short[] array.
Returns:
Values passed to this method.

$

public static java.lang.Object $()
Shortcut for returning an empty array of objects.

Returns:
an empty Object[] array.