private long total(String name, long now, TimeUnit unit) { switch (status) { case PAUSED: case RUN: long d = 0L; if (name == null) { d = global.total(now); } else { long[] entries = getSplitsByName(name, now); for (long entry : entries) { d += entry; } } return unit.convert(d, UNIT); default: throw new IllegalArgumentException("unknow status: " + status); } }
private long[] getSplitsByName(String name, long now) { List<Long> list = times.get(name); TimeEntry inSplitting = null; for (TimeEntry entry : splitStack) { if (entry.name.equals(name)) { inSplitting = entry; } } long[] ret = new long[(list == null ? 0 : list.size()) + (inSplitting == null ? 0 : 1)]; if (list != null) { int i = 0; for (long l : list) { ret[i] = l; i++; } } if (inSplitting != null) { ret[ret.length - 1] = inSplitting.total(now); } return ret; }
public void unsplit() { long now = now(); TimeEntry timeEntry = splitStack.peek(); if (timeEntry != null) { splitStack.pop(); } else { return; } switch (status) { case RUN: timeEntry.pause(now); case PAUSED: List<Long> list = times.get(timeEntry.name); if (list == null) { list = new ArrayList<Long>(); times.put(timeEntry.name, list); } list.add(timeEntry.total(now)); break; default: throw new IllegalArgumentException("unknow status: " + status); } }