package timeflow.data.db; import timeflow.data.db.filter.*; import timeflow.data.time.*; import timeflow.util.*; import java.util.*; public class DBUtils { public static void dump(Act act) { List fields = act.getDB().getFields(); for (Field f : fields) { System.out.println(f.getName() + " = " + act.get(f)); } } public static Object get(Act act, String field) { return act.get(act.getDB().getField(field)); } public static List getFieldAliases(ActDB db) { ArrayList list = new ArrayList(); for (String s : db.getFieldKeys()) { if (!db.getField(s).getName().equals(s)) { list.add(s); } } return list; } public static Interval range(ActList a, Field[] fields) { if (fields == null || fields.length == 0) { return new Interval(0, 0); } Interval t = null; for (Act act : a) { for (Field f : fields) { RoughTime d = act.getTime(f); if (d != null && d.isDefined()) { if (t == null) { t = new Interval(d.getTime(), d.getTime()); } else { t.include(d.getTime()); } } } } return t != null ? t : new Interval(RoughTime.UNKNOWN, RoughTime.UNKNOWN); } public static Interval range(ActList a, String fieldName) { Field field = a.getDB().getField(fieldName); if (field == null || a.size() == 0) { return new Interval(0, 0); } Interval t = null; for (Act act : a) { RoughTime d = act.getTime(field); if (d != null && d.isDefined()) { if (t == null) { t = new Interval(d.getTime(), d.getTime()); } else { t.include(d.getTime()); } } } return t != null ? t : new Interval(RoughTime.UNKNOWN, RoughTime.UNKNOWN); } public static List categoryFields(ActDB db) { List list = new ArrayList(); list.addAll(db.getFields()); // list.addAll(db.getFields(String.class)); // list.addAll(db.getFields(String[].class)); return list; } public static int count(Iterable acts, Interval i, Field field)//String fieldName) { return count(acts, new TimeIntervalFilter(i, field)); } public static int count(Iterable acts, ActFilter filter) { int num = 0; for (Act a : acts) { if (filter.accept(a)) { num++; } } return num; } public static double[] minmax(Iterable acts, Field field) { double min = Double.NaN; double max = min; for (Act a : acts) { double x = a.getValue(field); if (Double.isNaN(min)) { min = x; max = x; } else if (!Double.isNaN(x)) { min = Math.min(x, min); max = Math.max(x, max); } } return new double[] { min, max }; } public static double[] getValues(Iterable acts, Field field) { ArrayList list = new ArrayList(); if (field.getType() == Double.class) { for (Act a : acts) { list.add(a.getValue(field)); } } else if (field.getType() == RoughTime.class) { for (Act a : acts) { RoughTime r = a.getTime(field); if (r != null) { list.add(new Double(r.getTime())); } } } int n = list.size(); double[] x = new double[n]; for (int i = 0; i < n; i++) { x[i] = list.get(i); } return x; } public static Bag countValues(Iterable acts, Field field) { Bag bag = new Bag(); if (field.getType() != String[].class) { for (Act a : acts) { bag.add(a.getString(field)); } } else // if (field.getType() == String[].class) { for (Act a : acts) { String[] s = a.getTextList(field); if (s != null) { for (int i = 0; i < s.length; i++) { bag.add(s[i]); } } } // } else // { // throw new IllegalArgumentException("Asked to count values for non-text field: " + field); } return bag; } public static void setRecSizesFromCurrent(ActDB db) { // for String fields. for (Field f : db.getFields(String.class)) { int max = 0; for (Act a : db) { String s = a.getString(f); if (s != null) { max = Math.max(s.length(), max); } } f.setRecommendedSize(max); } } public static Field ensureField(ActDB db, String name, Class type) { Field f = db.getField(name); if (f == null) { return db.addField(name, type); } else { if (f.getType() != type) { throw new IllegalArgumentException("Mismatched types: got " + type + ", expected " + f.getType()); } } return f; } }