Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 223   Methods: 26
NCLOC: 124   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ObjectUtil.java 3.3% 5% 3.8% 4.2%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.object;
 36   
 37    import java.util.Comparator;
 38   
 39    import edu.rice.cs.plt.iter.IterUtil;
 40   
 41    /** Utility methods that may be useful in implementing an object of any type. */
 42    public final class ObjectUtil {
 43   
 44  0 private ObjectUtil() {}
 45   
 46   
 47  0 public static boolean equal(Object o1, Object o2) {
 48  0 return (o1 == null) ? (o2 == null) : o1.equals(o2);
 49    }
 50   
 51   
 52    /** Prime number used for hashing (approximately 2^32 divided by the golden ratio). */
 53    private static final int KNUTH_CONST = -1640531535; // 2654435761 (unsigned)
 54   
 55    /** Optimized implementation of {@link #hash(int[])} with 0 args. */
 56  0 public static int hash() { return 1; }
 57   
 58    /** Optimized implementation of {@link #hash(int[])} with 1 arg. */
 59  0 public static int hash(int a) { return KNUTH_CONST ^ a; }
 60   
 61    /** Optimized implementation of {@link #hash(int[])}) with 2 args. */
 62  0 public static int hash(int a, int b) { return ((KNUTH_CONST ^ a) * KNUTH_CONST) ^ b; }
 63   
 64    /** Optimized implementation of {@link #hash(int[])} with 3 args. */
 65  0 public static int hash(int a, int b, int c) {
 66  0 return ((((KNUTH_CONST ^ a) * KNUTH_CONST) ^ b) * KNUTH_CONST) ^ c;
 67    }
 68   
 69    /** Optimized implementation of {@link #hash(int[])} with 4 args. */
 70  0 public static int hash(int a, int b, int c, int d) {
 71  0 return ((((((KNUTH_CONST ^ a) * KNUTH_CONST) ^ b) * KNUTH_CONST) ^ c) * KNUTH_CONST) ^ d;
 72    }
 73   
 74    /**
 75    * Produce a hash value based on the given keys according to an algorithm attributed to
 76    * Knuth (The Art of Programming, Vol. 3, Sorting and Searching) (TODO: verify this source).
 77    * The key idea is to combine 32-bit hash keys (where a typical class has multiple hash keys)
 78    * using exclusive OR after multiplying the existing accumulated result by a large prime number.
 79    */
 80  0 public static int hash(int... keys) {
 81  0 int len = keys.length;
 82  0 int result = 1;
 83  0 for (int i = 0; i < len; i++) { result = (result * KNUTH_CONST) ^ keys[i]; }
 84  0 return result;
 85    }
 86   
 87    /** Optimized implementation of {@link #hash(Object[])} with 1 arg. */
 88  0 public static int hash(Object a) { return hash((a == null) ? 0 : a.hashCode()); }
 89   
 90    /** Optimized implementation of {@link #hash(Object[])}) with 2 args. */
 91  0 public static int hash(Object a, Object b) {
 92  0 return hash((a == null) ? 0 : a.hashCode(), (b == null) ? 0 : b.hashCode());
 93    }
 94   
 95    /** Optimized implementation of {@link #hash(Object[])} with 3 args. */
 96  0 public static int hash(Object a, Object b, Object c) {
 97  0 return hash((a == null) ? 0 : a.hashCode(), (b == null) ? 0 : b.hashCode(),
 98  0 (c == null) ? 0 : c.hashCode());
 99    }
 100   
 101    /** Optimized implementation of {@link #hash(Object[])} with 4 args. */
 102  0 public static int hash(Object a, Object b, Object c, Object d) {
 103  0 return hash((a == null) ? 0 : a.hashCode(), (b == null) ? 0 : b.hashCode(),
 104  0 (c == null) ? 0 : c.hashCode(), (d == null) ? 0 : d.hashCode());
 105    }
 106   
 107    /** Produce a hash code for an object in which {@code #equals()} depends on the given values. */
 108  0 public static int hash(Object... objs) {
 109  0 int len = objs.length;
 110  0 int[] keys = new int[len];
 111  0 for (int i = 0; i < len; i++) {
 112  0 Object obj = objs[i];
 113  0 keys[i] = (obj == null) ? 0 : obj.hashCode();
 114    }
 115  0 return hash(keys);
 116    }
 117   
 118    /** Produce a hash code for an object in which {@code equals()} depends on the given values. */
 119  0 public static int hash(Iterable<?> iter) {
 120  0 int result = 1;
 121  0 for (Object obj : iter) {
 122  0 int key = (obj == null) ? 0 : obj.hashCode();
 123  0 result = (result * KNUTH_CONST) ^ key;
 124    }
 125  0 return result;
 126    }
 127   
 128  0 public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
 129    int compare(T1 x1, T1 y1, T2 x2, T2 y2) {
 130  0 int result = x1.compareTo(y1);
 131  0 if (result == 0) { result = x2.compareTo(y2); }
 132  0 return result;
 133    }
 134   
 135  36 public static <T1, T2> int compare(Comparator<? super T1> comp1, T1 x1, T1 y1,
 136    Comparator<? super T2> comp2, T2 x2, T2 y2) {
 137  36 int result = comp1.compare(x1, y1);
 138  20 if (result == 0) { result = comp2.compare(x2, y2); }
 139  36 return result;
 140    }
 141   
 142  0 public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>,
 143    T3 extends Comparable<? super T3>>
 144    int compare(T1 x1, T1 y1, T2 x2, T2 y2, T3 x3, T3 y3) {
 145  0 int result = x1.compareTo(y1);
 146  0 if (result == 0) { result = x2.compareTo(y2); }
 147  0 if (result == 0) { result = x3.compareTo(y3); }
 148  0 return result;
 149    }
 150   
 151  0 public static <T1, T2, T3> int compare(Comparator<? super T1> comp1, T1 x1, T1 y1,
 152    Comparator<? super T2> comp2, T2 x2, T2 y2,
 153    Comparator<? super T3> comp3, T3 x3, T3 y3) {
 154  0 int result = comp1.compare(x1, y1);
 155  0 if (result == 0) { result = comp2.compare(x2, y2); }
 156  0 if (result == 0) { result = comp3.compare(x3, y3); }
 157  0 return result;
 158    }
 159   
 160  0 public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>,
 161    T3 extends Comparable<? super T3>, T4 extends Comparable<? super T4>>
 162    int compare(T1 x1, T1 y1, T2 x2, T2 y2, T3 x3, T3 y3, T4 x4, T4 y4) {
 163  0 int result = x1.compareTo(y1);
 164  0 if (result == 0) { result = x2.compareTo(y2); }
 165  0 if (result == 0) { result = x3.compareTo(y3); }
 166  0 if (result == 0) { result = x4.compareTo(y4); }
 167  0 return result;
 168    }
 169   
 170  0 public static <T1, T2, T3, T4> int compare(Comparator<? super T1> comp1, T1 x1, T1 y1,
 171    Comparator<? super T2> comp2, T2 x2, T2 y2,
 172    Comparator<? super T3> comp3, T3 x3, T3 y3,
 173    Comparator<? super T4> comp4, T4 x4, T4 y4) {
 174  0 int result = comp1.compare(x1, y1);
 175  0 if (result == 0) { result = comp2.compare(x2, y2); }
 176  0 if (result == 0) { result = comp3.compare(x3, y3); }
 177  0 if (result == 0) { result = comp4.compare(x4, y4); }
 178  0 return result;
 179    }
 180   
 181   
 182    /**
 183    * Get the height of the object when treated as a composite. If {@code obj} implements
 184    * {@code Composite}, invokes {@link Composite#compositeHeight}; otherwise, returns {@code 0}.
 185    */
 186  0 public static int compositeHeight(Object obj) {
 187  0 if (obj instanceof Composite) { return ((Composite) obj).compositeHeight(); }
 188  0 else { return 0; }
 189    }
 190   
 191    /** Get the maximum composite height of a set of objects. */
 192  0 public static int compositeHeight(Object... objs) { return compositeHeight(IterUtil.asIterable(objs)); }
 193   
 194    /** Get the maximum composite height of a set of objects. */
 195  0 public static int compositeHeight(Iterable<?> objs) {
 196  0 int result = 0;
 197  0 for (Object obj : objs) {
 198  0 int height = compositeHeight(obj);
 199  0 if (result < height) { result = height; }
 200    }
 201  0 return result;
 202    }
 203   
 204    /**
 205    * Get the composite size of the object. If {@code obj} implements {@code Composite},
 206    * invokes {@link Composite#compositeSize}; otherwise, returns {@code 1}.
 207    */
 208  0 public static int compositeSize(Object obj) {
 209  0 if (obj instanceof Composite) { return ((Composite) obj).compositeSize(); }
 210  0 else { return 1; }
 211    }
 212   
 213    /** Get the total composite size of a set of objects. */
 214  0 public static int compositeSize(Object... objs) { return compositeSize(IterUtil.asIterable(objs)); }
 215   
 216    /** Get the total composite size of a set of objects. */
 217  0 public static int compositeSize(Iterable<?> objs) {
 218  0 int result = 0;
 219  0 for (Object obj : objs) { result += compositeSize(obj); }
 220  0 return result;
 221    }
 222   
 223    }