edu.neu.ccs.quick
Class StringObjectPair

java.lang.Object
  extended byedu.neu.ccs.quick.StringObjectPair

public final class StringObjectPair
extends Object

StringObjectPair constructs an immutable pair object from a non-null String and a non-null Object.

The methods equals and hashCode are designed so that two StringObjectPair objects whose contents are equal will be considered equal and will return the same hash code.

Since:
2.3
Version:
2.3

Field Summary
private  Object object
          The Object item of the pair.
private  String string
          The String item of the pair.
 
Constructor Summary
StringObjectPair(Object[] data)
          Constructor that constructs a pair using a non-null array of Object such that the array has 2 items, item 0 is a non-null String and item 1 is a non-null Object.
StringObjectPair(String string, Object object)
          Constructor that constructs a pair using a non-null String and a non-null Object.
 
Method Summary
 boolean equals(Object entity)
          If the given entity is an instance of StringObjectPair and if the corresponding items in this pair and the given entity are equal, then returns true; otherwise returns false.
 Object getObject()
          Returns the object in the pair.
static Object[] getObjects(StringObjectPair[] pairs)
          Returns the array of objects from this array of pairs.
 String getString()
          Returns the string in the pair.
static String[] getStrings(StringObjectPair[] pairs)
          Returns the array of strings from this array of pairs.
 int hashCode()
          Returns a hash code value for this pair that is computed as the exclusive OR of the hash code values for the two items in the pair.
static StringObjectPair makeStringObjectPair(Object[] data)
          Factory method that returns a StringObjectPair if the data array is non-null, has exactly two non-null items, and the first item is a String; otherwise returns null.
static StringObjectPair makeStringObjectPair(Object a, Object b)
          Factory method that returns a StringObjectPair if the two objects are non-null and the first is a String; otherwise returns null.
 Object[] toArray()
          Returns the contents of the pair as an array of Object of length 2.
static StringObjectPair[] toArray(Object[][] array)
          Returns the contents of the given Object[][] array as a StringObjectPair[] array.
static Object[][] toArray(StringObjectPair[] pairs)
          Returns the contents of the given StringObjectPair[] array as an Object[][] array.
 String toString()
          Returns the String constructed as: (getString(), getObject().toString()).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

string

private String string
The String item of the pair.


object

private Object object
The Object item of the pair.

Constructor Detail

StringObjectPair

public StringObjectPair(String string,
                        Object object)

Constructor that constructs a pair using a non-null String and a non-null Object.

Parameters:
string - the string for the pair
object - the object for the pair
Throws:
NullPointerException - if either data item is null

StringObjectPair

public StringObjectPair(Object[] data)

Constructor that constructs a pair using a non-null array of Object such that the array has 2 items, item 0 is a non-null String and item 1 is a non-null Object.

Parameters:
data - the data array
Throws:
NullPointerException - if data is null or if an array item in data is null
IllegalArgumentException - if data does not have length 2 or if data[0] is not of type String
Method Detail

getString

public String getString()
Returns the string in the pair.

Returns:
object string in the pair

getObject

public Object getObject()
Returns the object in the pair.

Returns:
object in the pair

equals

public boolean equals(Object entity)

If the given entity is an instance of StringObjectPair and if the corresponding items in this pair and the given entity are equal, then returns true; otherwise returns false.

Parameters:
entity - the entity to compare to this pair
Returns:
true if the given entity is a string-object pair with equal items to those in this string-object pair

hashCode

public int hashCode()

Returns a hash code value for this pair that is computed as the exclusive OR of the hash code values for the two items in the pair.

Returns:
the hash code

toString

public String toString()

Returns the String constructed as: (getString(), getObject().toString()).

Returns:
the string formed conceptually as (string, object)

toArray

public Object[] toArray()

Returns the contents of the pair as an array of Object of length 2.

Returns:
the pair as an array

makeStringObjectPair

public static StringObjectPair makeStringObjectPair(Object a,
                                                    Object b)

Factory method that returns a StringObjectPair if the two objects are non-null and the first is a String; otherwise returns null.

Does not throw any exceptions.

Parameters:
a - the string for the pair
b - the object for the pair

makeStringObjectPair

public static StringObjectPair makeStringObjectPair(Object[] data)

Factory method that returns a StringObjectPair if the data array is non-null, has exactly two non-null items, and the first item is a String; otherwise returns null.

Does not throw any exceptions.

Parameters:
data - the data with which to form the pair if possible
See Also:
StringObjectPair(Object[])

toArray

public static Object[][] toArray(StringObjectPair[] pairs)

Returns the contents of the given StringObjectPair[] array as an Object[][] array.

Parameters:
pairs - the 1-dimensional pair array
Returns:
the 2-dimensional object array

toArray

public static StringObjectPair[] toArray(Object[][] array)

Returns the contents of the given Object[][] array as a StringObjectPair[] array.

Uses makeStringObjectPair(Object[]) to make the individual items in the returned StringObjectPair[] array. In this way, items in the original array that cannot make a suitable pair will map to null.

Parameters:
array - the 2-dimensional object array
Returns:
the 1-dimensional pair array

getStrings

public static String[] getStrings(StringObjectPair[] pairs)
Returns the array of strings from this array of pairs.

Parameters:
pairs - the 1-dimensional pair array
Returns:
the array of strings

getObjects

public static Object[] getObjects(StringObjectPair[] pairs)
Returns the array of objects from this array of pairs.

Parameters:
pairs - the 1-dimensional pair array
Returns:
the array of objects