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: 17,195 Bytes Line Count: 414 '\n' Characters Found
-
-
Field Summary
Fields Modifier and Type Field protected static long
serialVersionUID
-
Method Summary
Convenience Getters for SubClasses Modifier and Type Method abstract Object
get(int i)
<T> T
get(int i, Class<T> c)
<T> T
GET(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 int
n()
Methods: interface java.lang.Cloneable Modifier and Type Method abstract Object
clone()
Methods: class java.lang.Object Modifier and Type Method boolean
equals(Object other)
int
hashCode()
String
toString()
-
-
-
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 theSerializable
Implementation 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 ofObject
which represents theith
instance-field from whateverRetN
orTupleN
implementation this method has been invoked.
If'this'
instance were aRet5
, and'3'
were passed to parameter'i'
, then the value inRet5.c
would be returned.
If a call to'get'
is made fromTuple2
, and'3'
were passed to'i'
, then anIndexOutOfBoundsException
would 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 anIndexOutOfBoundsException
throw.- Returns:
- This returns the
ith
field of this class. - Throws:
java.lang.IndexOutOfBoundsException
- if'i'
is zero or negative, or greater than the value of'N'
for whicheverRetN
orTupleN
class 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 implementingRetN
is 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 anIndexOutOfBoundsException
exception.- Returns:
- This returns the
ith
field of this class. - Throws:
java.lang.IndexOutOfBoundsException
- if'i'
is zero or negative, or greater than the value of'N'
for whicheverRetN
class 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 implementingRetN
is 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 anIndexOutOfBoundsException
exception.- Returns:
- This returns the
ith
field of this class. - Throws:
java.lang.IndexOutOfBoundsException
- if'i'
is zero or negative, or greater than the value of'N'
for whicheverRetN
class 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 theith
field 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:
equals
in 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.Object
requirement. 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:
hashCode
in classjava.lang.Object
- Returns:
- a hash-code that may be used for sets and maps like
java.util.Hashtable
andjava.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 implementingRetN
to aString
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- This instance-object as a
String
. - Code:
- Exact Method Body:
// All MultiType / Tuple implementations return their fields as an array. Object[] fields = asArray(); // whatever subclass this instance is, this is actually just the number of fields int n = n(); // Tells the output-printing mechanism when/if one of the fields is an array. boolean[] isArray = new boolean[n]; // These will hold the "Simple Name's" of each class/type in the previous array. String[] types = new String[n]; // The fields are named 'a ... e', unless N is 6, 7, or 8. In that case the fields are // named 'a1 ... h8' String[] FIELD_NAMES = (n < 6) ? fieldNames2To5 : fieldNames6To8; // This will hold the returned java.lang.String that is provided by this method call. StringBuilder sb = new StringBuilder(); // Simple Loop Variables int i=0, maxLen=0; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Loop merely retrieves the TYPE/CLASS of each field as a STRING (and if it is an array) // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** for (Object field : fields) { // If the field is non-null, retrieving the class is easy, otherwise, it isn't-possible // because of GENERIC-ERASURE if (field != null) { Class c = field.getClass(); types[i] = c.getSimpleName(); isArray[i] = c.isArray(); } else { types[i] = "<GENERIC-ERASURE>"; // Smoke 'em if you got 'em isArray[i] = false; // DUMMY-VALUE } // These String's are pretty-printed with right-space-pad. This computes the padding. if (types[i].length() > maxLen) maxLen = types[i].length(); i++; } // Formatting: the '+2' adds two space-characters to the output. These spaces occur // *AFTER* the Type/Class is printed to the output. maxLen += 2; i=0; for (Object field : fields) { // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // This print's the NAME & TYPE of the Field - For Example: "Ret6.a1: String" // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** String line = "Ret" + n + "." + FIELD_NAMES[i] + ": " + StringParse.rightSpacePad(types[i], maxLen); sb.append(line); // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // This simply prints the VALUE of the Field. For arrays, "extra-care is provided" // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** if (field != null) { String s = isArray[i] ? toArrayString(field, types[i]) : field.toString(); if (s.indexOf('\n') != -1) sb.append('\n' + StrIndent.indent(s, 4)); else if (line.length() + s.length() > 70) sb.append('\n' + StrIndent.indent(s, 4)); else sb.append(s); } else sb.append("null"); sb.append('\n'); i++; } return sb.toString();
-
clone
public abstract java.lang.Object clone()
Clones the contents of'this'
instance of.- Overrides:
clone
in classjava.lang.Object
- Returns:
- A clone copy of
'this'
object.
-
-