org.joe_e.array
Class ConstArray<E>

java.lang.Object
  extended by org.joe_e.array.ConstArray<E>
Type Parameters:
E - the element type of objects contained in the array
All Implemented Interfaces:
Serializable, Iterable<E>, Selfless
Direct Known Subclasses:
ImmutableArray

public class ConstArray<E>
extends Object
implements Selfless, Iterable<E>, Serializable

A read-only array containing elements of an arbitrary type.

Note: this class implements Serializable in order to avoid preventing trusted (non-Joe-E) code from serializing it. The Java Serialization API is tamed away as unsafe, and thus is not available to Joe-E code.

See Also:
Serialized Form

Nested Class Summary
static class ConstArray.Builder<E>
          A ConstArray factory.
 
Method Summary
static
<T> ConstArray<T>
array()
          Construct an empty ConstArray.
static
<T> ConstArray<T>
array(T... values)
          Construct a ConstArray.
static
<T> ConstArray<T>
array(T value)
          Construct a ConstArray with one element.
static
<T> ConstArray<T>
array(T value1, T value2)
          Construct a ConstArray with two elements.
static
<T> ConstArray<T>
array(T value1, T value2, T value3)
          Construct a ConstArray with three elements.
static
<T> ConstArray<T>
array(T value1, T value2, T value3, T value4)
          Construct a ConstArray with four elements.
static
<E> ConstArray.Builder<E>
builder()
          Get a ConstArray.Builder.
static
<E> ConstArray.Builder<E>
builder(int estimate)
          Get a ConstArray.Builder.
 boolean equals(Object other)
          Test for equality with another object.
 E get(int i)
          Gets the element at a specified position.
 int hashCode()
          Computes a digest of the array for hashing.
 org.joe_e.array.ArrayIterator<E> iterator()
          Return a new iterator over the array
 int length()
          Return the length of the array
<T> T[]
toArray(T[] prototype)
          Return a mutable copy of the array
 String toString()
          Return a string representation of the array.
 ConstArray<E> with(E newE)
          Return a new ConstArray that contains the same elements as this one but with a new element added to the end
 ConstArray<E> without(int i)
          Return a new ConstArray that contains the same elements as this one excluding the element at a specified index
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

array

public static <T> ConstArray<T> array(T... values)
Construct a ConstArray.

Parameters:
values - each value

array

public static <T> ConstArray<T> array()
Construct an empty ConstArray.


array

public static <T> ConstArray<T> array(T value)
Construct a ConstArray with one element.

Parameters:
value - the value

array

public static <T> ConstArray<T> array(T value1,
                                      T value2)
Construct a ConstArray with two elements.

Parameters:
value1 - the first value
value2 - the second value

array

public static <T> ConstArray<T> array(T value1,
                                      T value2,
                                      T value3)
Construct a ConstArray with three elements.

Parameters:
value1 - the first value
value2 - the second value
value3 - the third value

array

public static <T> ConstArray<T> array(T value1,
                                      T value2,
                                      T value3,
                                      T value4)
Construct a ConstArray with four elements.

Parameters:
value1 - the first value
value2 - the second value
value3 - the third value
value4 - the fourth value

equals

public boolean equals(Object other)
Test for equality with another object.

Overrides:
equals in class Object
Parameters:
other - the reference object with which to compare.
Returns:
true if the other object is a ConstArray with the same contents as this array (determined by calling equals() on array elements)
See Also:
Object.hashCode(), HashMap

hashCode

public int hashCode()
Computes a digest of the array for hashing. If all of the elements of the array implement Selfless in the overlay type system, the hash will be the same as that computed by Arrays.hashCode(Object[]) for a Java array with the same elements. The precise behavior when some elements are not Selfless is unspecified, and may change in future releases. It is, however, guaranteed to be deterministic for a given library version.

Specified by:
hashCode in interface Selfless
Overrides:
hashCode in class Object
Returns:
a hash code based on the contents of this array
See Also:
Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)

toString

public String toString()
Return a string representation of the array. Equivalent to the result of Arrays.toString() except that some elements may be replaced with the string "<unprintable>". The set of elements for which this is the case is unspecified, and may change in future releases.

Overrides:
toString in class Object
Returns:
a string representation of this array

iterator

public org.joe_e.array.ArrayIterator<E> iterator()
Return a new iterator over the array

Specified by:
iterator in interface Iterable<E>
Returns:
an Iterator.

get

public E get(int i)
Gets the element at a specified position.

Parameters:
i - position of the element to return
Throws:
ArrayIndexOutOfBoundsException - i is out of bounds

length

public int length()
Return the length of the array


toArray

public <T> T[] toArray(T[] prototype)
Return a mutable copy of the array

Parameters:
prototype - prototype of the array to copy into
Returns:
an array containing the contents of this ConstArray of the same type as prototype
Throws:
ArrayStoreException - if an element cannot be stored in the array

with

public ConstArray<E> with(E newE)
Return a new ConstArray that contains the same elements as this one but with a new element added to the end

Parameters:
newE - the element to add
Returns:
the new array

without

public ConstArray<E> without(int i)
Return a new ConstArray that contains the same elements as this one excluding the element at a specified index

Parameters:
i - the index of the element to exclude
Returns:
the new array

builder

public static <E> ConstArray.Builder<E> builder()
Get a ConstArray.Builder. This is equivalent to the constructor.

Returns:
a new builder instance, with the default internal array length

builder

public static <E> ConstArray.Builder<E> builder(int estimate)
Get a ConstArray.Builder. This is equivalent to the constructor.

Parameters:
estimate - estimated array length
Returns:
a new builder instance