/** * @param start Start time * @return Index sample with time stamp at-or-before start time, or -1. */ private int findSampleLessOrEqual(final Instant start) { // Would like to use PlotSampleSearch, but that operates on array // of PlotSample[] int low = 0; int high = samples.size() - 1; int cmp = 0; int mid = -1; while (low <= high) { mid = (low + high) / 2; // Compare 'mid' sample to goal final Instant time = samples.get(mid).getPosition(); final int compare = time.compareTo(start); if (compare > 0) { // 'mid' too big, search lower half cmp = 1; high = mid - 1; } else if (compare < 0) { // 'mid' too small, search upper half cmp = -1; low = mid + 1; } else { cmp = 0; return mid; // found exact time } } // Didn't find exact match. if (cmp < 0) // 'mid' sample is smaller than x, so it's OK return mid; // cmp > 0, 'mid' sample is greater than x. // If there is a sample before, use that if (mid > 0) return mid - 1; return -1; }
/** Returns the maximum between the two specified {@link Timestamp}. */ public static Instant max(Instant t1, Instant t2) { if (t1 == null || t2 == null) return null; if (t1.compareTo(t2) >= 0) return t1; else return t2; }