1 package org.unicode.cldr.util; 2 3 import java.lang.reflect.Field; 4 import java.lang.reflect.Modifier; 5 6 public final class BoilerplateUtilities { 7 toStringHelper(Object object)8 public static String toStringHelper(Object object) { 9 StringBuffer result = new StringBuffer("["); 10 Class<?> cls = object.getClass(); 11 Field[] fields = cls.getDeclaredFields(); 12 boolean gotOne = false; 13 for (int i = 0; i < fields.length; ++i) { 14 int mods = fields[i].getModifiers(); 15 if (Modifier.isStatic(mods)) continue; 16 Object value = "no-access"; 17 try { 18 value = fields[i].get(object); 19 } catch (Exception e) { 20 } 21 if (value == null) continue; 22 if (gotOne) result.append(", "); 23 result.append(fields[i].getName()).append('=').append(value); 24 gotOne = true; 25 } 26 result.append("]"); 27 return result.toString(); 28 } 29 30 @SuppressWarnings("unchecked") compareToHelper(Object a, Object b, int depth)31 public static int compareToHelper(Object a, Object b, int depth) { 32 if (a == null) { 33 return b == null ? 0 : -1; 34 } else if (b == null) { 35 return 1; 36 } 37 Class<?> aClass = a.getClass(); 38 Class<?> bClass = b.getClass(); 39 if (aClass != bClass) { 40 return aClass.getName().compareTo(bClass.getName()); 41 } 42 if (depth != 0 && a instanceof Comparable<?>) { 43 return ((Comparable<Object>) a).compareTo(b); 44 } 45 if (a instanceof Number) { 46 double aDouble = ((Number) a).doubleValue(); 47 double bDouble = ((Number) b).doubleValue(); 48 return aDouble < bDouble ? -1 : aDouble == bDouble ? 0 : -1; 49 } 50 Field[] fields = aClass.getDeclaredFields(); 51 for (int i = 0; i < fields.length; ++i) { 52 int mods = fields[i].getModifiers(); 53 if (Modifier.isStatic(mods)) continue; 54 try { 55 fields[i].get(a); 56 fields[i].get(b); 57 } catch (Exception e) { 58 } 59 int result = compareToHelper(a, b, depth + 1); 60 if (result != 0) return result; 61 } 62 return 0; 63 } 64 }