X-Git-Url: http://jaekl.net/gitweb/?a=blobdiff_plain;f=prod%2Fnet%2Fjaekl%2Fcfb%2Futil%2FUtil.java;h=99ef6e846f78b9b082cfbbc7d4e8db7f94e9fae6;hb=a4a577abc3f9b2b1147caafd1cb39fa8c2622cd4;hp=c0e7b1ca4b592ee9c2decc8e168fe3994ba3ec00;hpb=08a530ef53cc4756f5e632b69c78830872ebd9f4;p=cfb.git diff --git a/prod/net/jaekl/cfb/util/Util.java b/prod/net/jaekl/cfb/util/Util.java index c0e7b1c..99ef6e8 100644 --- a/prod/net/jaekl/cfb/util/Util.java +++ b/prod/net/jaekl/cfb/util/Util.java @@ -1,13 +1,79 @@ package net.jaekl.cfb.util; +// Copyright (C) 2015 Christian Jaekl + import java.io.PrintWriter; import java.io.StringWriter; +import java.util.Iterator; +import java.util.List; public class Util { - public static String stringify(Throwable thr) { + // Returns true iff. a and b contain equal items in the same order + public static boolean listsAreEqual(List a, List b) + { + if ((null == a) || (null == b)) { + return (a == b); + } + + if (0 == a.size()) { + return (0 == b.size()); + } + + if (a.size() != b.size()) { + return false; + } + + Iterator iterA = a.iterator(); + Iterator iterB = b.iterator(); + + while (iterA.hasNext()) { + Object elemA = iterA.next(); + Object elemB = iterB.next(); + + if (! objsAreEqual(elemA, elemB)) { + return false; + } + } + + return true; + } + + // Test for equality, while taking care to avoid + // dereferencing a null pointer. + // Note that two null pointers are considered equal. + public static boolean objsAreEqual(Object a, Object b) + { + if ((null == a) || (null == b)) { + return (a == b); + } + + if ((a instanceof Number) && (b instanceof Number)) { + Number aNum = (Number)a; + Number bNum = (Number)b; + + return ( (aNum.longValue() == bNum.longValue()) + || (aNum.doubleValue() == bNum.doubleValue()) ); + } + + return a.equals(b); + } + + // Return 1 if obj is null, or obj.hashCode() otherwise + public static int objHashCode(Object obj) + { + if (null == obj) { + return 1; + } + return obj.hashCode(); + } + + // Convert a Throwable to the string representation + // that is generated by printStackTrace(). + public static String stringify(Throwable thr) + { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); thr.printStackTrace(pw); return sw.toString(); - } + } }