edu.rice.cs.plt.recur
Class RecurUtil

java.lang.Object
  extended by edu.rice.cs.plt.recur.RecurUtil

public final class RecurUtil
extends Object

TODO: Is the extra overhead required to check for infinite loops enough to justify non-checking alternatives to safeToString, safeEquals, and safeHashCode?


Nested Class Summary
static class RecurUtil.ArrayStringMode
          Defines the representation to be used in array string-generating methods
 
Method Summary
static boolean arrayEquals(Object[] a1, Object[] a2)
          Test the equality of the given arrays.
static boolean arrayEquals(Object a1, Object a2)
          Test the equality of the given arrays.
static int arrayHashCode(boolean[] array)
          Generate a hash code for the given array.
static int arrayHashCode(byte[] array)
          Generate a hash code for the given array.
static int arrayHashCode(char[] array)
          Generate a hash code for the given array.
static int arrayHashCode(double[] array)
          Generate a hash code for the given array.
static int arrayHashCode(float[] array)
          Generate a hash code for the given array.
static int arrayHashCode(int[] array)
          Generate a hash code for the given array.
static int arrayHashCode(long[] array)
          Generate a hash code for the given array.
static int arrayHashCode(Object array)
          Generate a hash code for the given array.
static int arrayHashCode(Object[] array)
          Generate a hash code for the given array.
static int arrayHashCode(short[] array)
          Generate a hash code for the given array.
static String arrayToString(boolean[] array)
          Invoke arrayToString(boolean[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(boolean[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(byte[] array)
          Invoke arrayToString(byte[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(byte[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(char[] array)
          Invoke arrayToString(char[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(char[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(double[] array)
          Invoke arrayToString(double[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(double[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(float[] array)
          Invoke arrayToString(float[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(float[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(int[] array)
          Invoke arrayToString(int[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(int[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(long[] array)
          Invoke arrayToString(long[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(long[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(Object array)
          Invoke arrayToString(Object, ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(Object[] array)
          Invoke arrayToString(Object[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(Object[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(Object array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static String arrayToString(short[] array)
          Invoke arrayToString(short[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED
static String arrayToString(short[] array, RecurUtil.ArrayStringMode stringMode)
          Generate a string representation of the given array.
static boolean safeEquals(Object obj1, Object obj2)
          Invokes safeEquals(Object, Object, Predicate2) using true for infiniteEquals (note that false is not a reasonable value for infiniteEquals, since an infinite structure must be equal to itself)
static
<T1,T2> boolean
safeEquals(T1 obj1, T2 obj2, Predicate2<? super T1,? super T2> infiniteEquals)
          Evaluate obj1.equals(obj2) under the protection of an infinite-recursion check.
static int safeHashCode(Object obj)
          Invokes safeHashCode(Object, Lambda) using an arbitrary default value as the infiniteHashCode.
static int safeHashCode(Object obj, int infiniteHashCode)
          Invokes safeHashCode(Object, Lambda) using the given value as the infiniteHashCode.
static
<T> int
safeHashCode(T obj, Lambda<? super T,Integer> infiniteHashCode)
          Evaluate obj.hashCode() under the protection of an infinite-recursion check.
static String safeToString(Object obj)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ...
static String safeToString(Object obj, int depth)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ...
static String safeToString(Object obj, int depth, RecurUtil.ArrayStringMode arrayMode)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ...
static String safeToString(Object obj, RecurUtil.ArrayStringMode arrayMode)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ...
static String safeToString(Object obj, String infiniteString)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString, a default depth of 1, and RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.
static String safeToString(Object obj, String infiniteString, int depth)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString, and RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.
static String safeToString(Object obj, String infiniteString, int depth, RecurUtil.ArrayStringMode arrayMode)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString.
static String safeToString(Object obj, String infiniteString, RecurUtil.ArrayStringMode arrayMode)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString, and a default depth of 1.
static
<T> String
safeToString(T obj, Lambda<? super T,String> infiniteString)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) with a default depth of 1, and using RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.
static
<T> String
safeToString(T obj, Lambda<? super T,String> infiniteString, int depth)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.
static
<T> String
safeToString(T obj, Lambda<? super T,String> infiniteString, int depth, RecurUtil.ArrayStringMode arrayMode)
          Evaluate obj.toString() under the protection of an infinite-recursion check.
static
<T> String
safeToString(T obj, Lambda<? super T,String> infiniteString, RecurUtil.ArrayStringMode arrayMode)
          Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default depth of 1.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

safeToString

public static String safeToString(Object obj)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ... }", a default depth of 1, and RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.


safeToString

public static String safeToString(Object obj,
                                  String infiniteString)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString, a default depth of 1, and RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.


safeToString

public static <T> String safeToString(T obj,
                                      Lambda<? super T,String> infiniteString)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) with a default depth of 1, and using RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.


safeToString

public static String safeToString(Object obj,
                                  RecurUtil.ArrayStringMode arrayMode)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ... }", and a default depth of 1.


safeToString

public static String safeToString(Object obj,
                                  String infiniteString,
                                  RecurUtil.ArrayStringMode arrayMode)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString, and a default depth of 1.


safeToString

public static <T> String safeToString(T obj,
                                      Lambda<? super T,String> infiniteString,
                                      RecurUtil.ArrayStringMode arrayMode)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default depth of 1.


safeToString

public static String safeToString(Object obj,
                                  int depth)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ... }", and RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.


safeToString

public static String safeToString(Object obj,
                                  String infiniteString,
                                  int depth)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString, and RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.


safeToString

public static <T> String safeToString(T obj,
                                      Lambda<? super T,String> infiniteString,
                                      int depth)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using RecurUtil.ArrayStringMode.DEEP_BRACED as a default array string mode.


safeToString

public static String safeToString(Object obj,
                                  int depth,
                                  RecurUtil.ArrayStringMode arrayMode)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using a default infiniteString like "ClassName..." or "{ ... }".


safeToString

public static String safeToString(Object obj,
                                  String infiniteString,
                                  int depth,
                                  RecurUtil.ArrayStringMode arrayMode)
Invokes safeToString(Object, Lambda, int, ArrayStringMode) using the given string as the infiniteString.


safeToString

public static <T> String safeToString(T obj,
                                      Lambda<? super T,String> infiniteString,
                                      int depth,
                                      RecurUtil.ArrayStringMode arrayMode)

Evaluate obj.toString() under the protection of an infinite-recursion check. If a string for obj is already in the process of being computed by this method depth times (that is, this is the depth+1st nested invocation), infiniteString will be applied. Otherwise, the result is obj.toString().

To simplify client code, this method also handles null values (returning "null") and arrays (calling arrayToString(Object, ArrayStringMode)).

Parameters:
obj - An object (may be null or an array)
infiniteString - A lambda to generate a string for obj if its toString method has already been invoked depth times
depth - The number of times to allow a toString invocation before using infiniteString instead
arrayMode - The RecurUtil.ArrayStringMode to use if obj is an array
Returns:
A string representation of obj

arrayToString

public static String arrayToString(Object array)
Invoke arrayToString(Object, ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(Object array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list of each of the elements (calling safeToString(Object, ArrayStringMode) where applicable, using the mode produced by RecurUtil.ArrayStringMode.nestedMode()).

Throws:
IllegalArgumentException - If array is not an array

arrayToString

public static String arrayToString(Object[] array)
Invoke arrayToString(Object[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(Object[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise call safeToString(Object, ArrayStringMode) on each of the elements, using the mode produced by RecurUtil.ArrayStringMode.nestedMode(). If stringMode is RecurUtil.ArrayStringMode.SHALLOW_BRACKETED, the result will match that of Arrays.toString(Object[]); if stringMode is RecurUtil.ArrayStringMode.DEEP_BRACKETED, the result will match that of Arrays.deepToString(java.lang.Object[]).


arrayToString

public static String arrayToString(boolean[] array)
Invoke arrayToString(boolean[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(boolean[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


arrayToString

public static String arrayToString(char[] array)
Invoke arrayToString(char[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(char[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


arrayToString

public static String arrayToString(byte[] array)
Invoke arrayToString(byte[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(byte[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


arrayToString

public static String arrayToString(short[] array)
Invoke arrayToString(short[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(short[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


arrayToString

public static String arrayToString(int[] array)
Invoke arrayToString(int[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(int[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


arrayToString

public static String arrayToString(long[] array)
Invoke arrayToString(long[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(long[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


arrayToString

public static String arrayToString(float[] array)
Invoke arrayToString(float[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(float[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


arrayToString

public static String arrayToString(double[] array)
Invoke arrayToString(double[], ArrayStringMode) with a default string mode RecurUtil.ArrayStringMode.DEEP_BRACED


arrayToString

public static String arrayToString(double[] array,
                                   RecurUtil.ArrayStringMode stringMode)
Generate a string representation of the given array. If the stringMode is RecurUtil.ArrayStringMode.CLASS_NAME or RecurUtil.ArrayStringMode.TYPE_AND_SIZE, generate a simple name, as specified; otherwise, generate a list containing each element.


safeEquals

public static boolean safeEquals(Object obj1,
                                 Object obj2)
Invokes safeEquals(Object, Object, Predicate2) using true for infiniteEquals (note that false is not a reasonable value for infiniteEquals, since an infinite structure must be equal to itself)


safeEquals

public static <T1,T2> boolean safeEquals(T1 obj1,
                                         T2 obj2,
                                         Predicate2<? super T1,? super T2> infiniteEquals)

Evaluate obj1.equals(obj2) under the protection of an infinite-recursion check. If the equality of obj1 and obj2 is already in the process of being computed by this method, infiniteEquals will be applied. Otherwise, the result is obj1.equals(obj2).

To simplify client code, this method also handles null values (equal iff both values are null) and arrays (calling arrayEquals(Object, Object).

Parameters:
obj1 - An object (may be null or an array)
obj2 - An object (may be null or an array)
infiniteEquals - A predicate to determine equality of obj1.equals(obj2) has already been invoked
Returns:
true iff the two objects are equal

arrayEquals

public static boolean arrayEquals(Object a1,
                                  Object a2)
Test the equality of the given arrays. The result is calculated by comparing the lengths, types (in the primitive cases), and corresponding elements of the arguments, recurring on any nested objects (including arrays) using safeEquals(Object, Object). Unlike Arrays.deepEquals(java.lang.Object[], java.lang.Object[]), this method is able to handle an array nested within itself.

Throws:
IllegalArgumentException - If array1 or array2 is not an array

arrayEquals

public static boolean arrayEquals(Object[] a1,
                                  Object[] a2)
Test the equality of the given arrays. The result is calculated by comparing the lengths and corresponding elements of the arguments, recurring on nested objects (including arrays) using safeEquals(Object, Object). Note that arrays of different runtime types may still be equal if their elements are equal. Unlike Arrays.deepEquals(java.lang.Object[], java.lang.Object[]), this method is able to handle an array nested within itself.


safeHashCode

public static int safeHashCode(Object obj)
Invokes safeHashCode(Object, Lambda) using an arbitrary default value as the infiniteHashCode.


safeHashCode

public static int safeHashCode(Object obj,
                               int infiniteHashCode)
Invokes safeHashCode(Object, Lambda) using the given value as the infiniteHashCode.


safeHashCode

public static <T> int safeHashCode(T obj,
                                   Lambda<? super T,Integer> infiniteHashCode)

Evaluate obj.hashCode() under the protection of an infinite-recursion check. If the hash code for obj is already in the process of being computed by this method, infiniteHashCode will be applied. Otherwise, the result is obj.hashCode().

To simplify client code, this method also handles null values (returning 0) and arrays (calling arrayHashCode(Object)).

Parameters:
obj - An object (may be null or an array)
infiniteHashCode - A lambda to generate a hash code for obj if its hashCode method has already been invoked
Returns:
A hash code for of obj

arrayHashCode

public static int arrayHashCode(Object array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), recurring on any nested objects (including arrays) using safeHashCode(Object). Unlike Arrays.deepHashCode(java.lang.Object[]), this method is able to handle an array nested within itself.

Throws:
IllegalArgumentException - If array is not an array

arrayHashCode

public static int arrayHashCode(Object[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), recurring on each element using safeHashCode(Object). Unlike Arrays.deepHashCode(java.lang.Object[]), this method is able to handle an array nested within itself.


arrayHashCode

public static int arrayHashCode(boolean[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Boolean#hashCode method on each element. (Note that Arrays.hashCode(boolean[]) implements the same method; this is defined here for compatibility with earlier APIs.)


arrayHashCode

public static int arrayHashCode(char[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Character#hashCode method on each element. (Note that Arrays.hashCode(char[]) implements the same method; this is defined here for compatibility with earlier APIs.)


arrayHashCode

public static int arrayHashCode(byte[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Byte#hashCode method on each element. (Note that Arrays.hashCode(byte[]) implements the same method; this is defined here for compatibility with earlier APIs.)


arrayHashCode

public static int arrayHashCode(short[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Short#hashCode method on each element. (Note that Arrays.hashCode(short[]) implements the same method; this is defined here for compatibility with earlier APIs.)


arrayHashCode

public static int arrayHashCode(int[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Integer#hashCode method on each element. (Note that Arrays.hashCode(int[]) implements the same method; this is defined here for compatibility with earlier APIs.)


arrayHashCode

public static int arrayHashCode(long[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Long#hashCode method on each element. (Note that Arrays.hashCode(long[]) implements the same method; this is defined here for compatibility with earlier APIs.)


arrayHashCode

public static int arrayHashCode(float[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Float#hashCode method on each element. (Note that Arrays.hashCode(float[]) implements the same method; this is defined here for compatibility with earlier APIs.)


arrayHashCode

public static int arrayHashCode(double[] array)
Generate a hash code for the given array. The result is calculated as specified by List.hashCode(), invoking the Double#hashCode method on each element. (Note that Arrays.hashCode(double[]) implements the same method; this is defined here for compatibility with earlier APIs.)