Package Torello.Java.Additional
Class MultiType
- java.lang.Object
-
- Torello.Java.Additional.MultiType
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public abstract class MultiType extends java.lang.Object implements java.io.Serializable, java.lang.Cloneable
This is a parent class of the 'Multiple Return Type' classes. (Ret0 ... Ret8) This class provides some basic functionality to its descendants, namely:toString(),hashCode()andequals(Object).- See Also:
- Serialized Form
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/MultiType.java
- Open New Browser-Tab: Torello/Java/Additional/MultiType.java
File Size: 11,559 Bytes Line Count: 277 '\n' Characters Found
-
-
Field Summary
Fields Modifier and Type Field protected static longserialVersionUID
-
Method Summary
Convenience Getters for SubClasses Modifier and Type Method abstract Objectget(int i)<T> Tget(int i, Class<T> c)<T> TGET(int i)Convert Fields to Object Array Modifier and Type Method Object[]asArray()Retrieve the Number of Fields in this Class Modifier and Type Method abstract intn()Methods: interface java.lang.Cloneable Modifier and Type Method abstract Objectclone()Methods: class java.lang.Object Modifier and Type Method booleanequals(Object other)inthashCode()StringtoString()
-
-
-
Field Detail
-
serialVersionUID
protected static final long serialVersionUID
This fulfils the SerialVersion UID requirement for all classes that implement Java'sinterface java.io.Serializable. Using theSerializableImplementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.- See Also:
- Constant Field Values
- Code:
- Exact Field Declaration Expression:
protected static final long serialVersionUID = 1;
-
-
Method Detail
-
n
-
asArray
public java.lang.Object[] asArray()
Retrieve the contents of the instance-descendant class, as an array.- Returns:
- Returns the
'this'instance' fields'a', 'b', 'c'etc... as anObject[]array. - Code:
- Exact Method Body:
return asArrayMain();
-
get
public abstract java.lang.Object get(int i)
This will return an instance ofObjectwhich represents theithinstance-field from whateverRetNorTupleNimplementation this method has been invoked.
If'this'instance were aRet5, and'3'were passed to parameter'i', then the value inRet5.cwould be returned.
If a call to'get'is made fromTuple2, and'3'were passed to'i', then anIndexOutOfBoundsExceptionwould throw.- Parameters:
i- This specifies which field of the instance is being requested.Important: Unlike a Java array, when a'1'is passed to this parameter, it is requesting the first field in this instance. Passing a value of'0'shall cause anIndexOutOfBoundsExceptionthrow.- Returns:
- This returns the
ithfield of this class. - Throws:
java.lang.IndexOutOfBoundsException- if'i'is zero or negative, or greater than the value of'N'for whicheverRetNorTupleNclass is being used.
If'this'is an instance ofTuple5, then a value of 6 or greater would force this exception to throw.- See Also:
get(int, Class),GET(int)
-
get
public final <T> T get(int i, java.lang.Class<T> c)
This provides a quick way to cast the field to the requested class. This may be quicker typing than using an actual cast, because it will not generate a compiler warning about unchecked casts. If the cast fails, it will throw the usualClassCastException.- Parameters:
i- This specifies which field of the implementingRetNis being requested.Important: Unlike a Java array, when a'1'is passed to this parameter, it is requesting the first field in theRetN. Passing a value of'0'shall case anIndexOutOfBoundsExceptionexception.- Returns:
- This returns the
ithfield of this class. - Throws:
java.lang.IndexOutOfBoundsException- if'i'is zero or negative, or greater than the value of'N'for whicheverRetNclass is being used. If'this'is an instance ofRet5, then a value of 6 or greater will cause this exception to throw.java.lang.ClassCastException- If the field cannot be cast to the class provided.- See Also:
get(int),GET(int)- Code:
- Exact Method Body:
return c.cast(get(i));
-
GET
public final <T> T GET(int i)
This is more magic with Java's Type-Inferencing being applied to a Generic-Cast. Note that this method works a lot like (but not identical to) the later Java'var'syntax. Here, Java's Type-Inference Mechanism (inside the compile-time, not run-time, logic) will cast the result of this method to whatever type is on the left hand-side of the assignment.Type-Inferencing: Remember that the Java-Compiler will output a Compile-Time Error if it appears that it cannot infer type-parameter'T'.Run-Time Casting: This method will throw a Run-Time'ClassCastException', if the cast fails.- Type Parameters:
T- This Type-Parameter is inferred by whatever assignment is taking place, or however the result of this method is being applied, programatically. More can be read about Java's Compile-Time Type-Inferencing Mechanism on the Oracle Website.- Parameters:
i- This specifies which field of the implementingRetNis being requested.Important: Unlike a Java array, when a'1'is passed to this parameter, it is requesting the first field in theRetN. Passing a value of'0'shall case anIndexOutOfBoundsExceptionexception.- Returns:
- This returns the
ithfield of this class. - Throws:
java.lang.IndexOutOfBoundsException- if'i'is zero or negative, or greater than the value of'N'for whicheverRetNclass is being used. If'this'is an instance ofRet5, then a value of 6 or greater will cause this exception to throw.java.lang.ClassCastException- If theithfield of this class cannot be cast to the type that is inferred for Type-Parameter'T'.- Code:
- Exact Method Body:
return (T) get(i);
-
equals
public boolean equals(java.lang.Object other)
Compares'this'with another Java Object for equality.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- Any Java Object.- Returns:
- If
'this'instance is equal to the'other'instance. - Code:
- Exact Method Body:
if (this == other) return true; if (! (other instanceof MultiType)) return false; MultiType o = (MultiType) other; if (o.n() != this.n()) return false; Object[] theseFields = asArray(); Object[] thoseFields = o.asArray(); // Keep this in mind - Three lines ago, the value of n() was already checked! // if (theseFields.length != thoseFields.length) throw new UnreachableError(); for (int i=0; i < theseFields.length; i++) if (theseFields[i] == thoseFields[i]) continue; else if ( ((theseFields[i] != null) && (thoseFields[i] != null)) && (theseFields[i].getClass() == thoseFields[i].getClass()) && theseFields[i].equals(thoseFields[i]) ) continue; else return false; return true;
-
hashCode
public int hashCode()
Builds a hash-code to fulfill Java'sjava.lang.Objectrequirement. This variant of a hash function simply computes a hashcode for the first two non-null fields of this instance, and returns their sum.
If there aren't at least two non-null fields in this instance, then the hashcode for however many have been computed (READ: either 0, or 1) is returned.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash-code that may be used for sets and maps like
java.util.Hashtableandjava.util.HashSet. - Code:
- Exact Method Body:
// This is the value returned. int hashCode = 0; int count = 0; for (Object field : asArray()) if (field != null) { hashCode += field.hashCode(); // Once two Hash Code's have been computed return the 'SUM' of them. if (++count == 2) return hashCode; } return hashCode;
-
toString
public java.lang.String toString()
Converts this instance of the implementingRetNto aString.- Overrides:
toStringin classjava.lang.Object- Returns:
- This instance-object as a
String. - Code:
- Exact Method Body:
return MultiTypeToString.run(this.asArray(), this.n());
-
clone
public abstract java.lang.Object clone()
Clones the contents of'this'instance of.- Overrides:
clonein classjava.lang.Object- Returns:
- A clone copy of
'this'object.
-
-