过滤数据时间

时间:2017-12-21 21:55:17

标签: java arrays datetime arraylist collections

此代码块过滤(丢弃)通过测试的最新(索引0)参考时间。过滤器的结果允许用户显示下一个最新的参考时间。我试图弄清楚如何过滤掉(丢弃)输出中的最新和下一个最新参考时间。

// This routine filters out any DataTimes from a sequence that have a
// reference time later than "clock". If the flag "prevRun" is true then
// it also filters out any DataTimes with the latest reference time
// that passes the "clock" test. This allows the caller to see the
// previous run of some model. Returns the latest reference time remaining
// in the sequence. Caller allocates and own "times".
//
// -- implementation
// ---------------------------------------------------------
// Correct return of latest reference time dependent on default construction
// of AbsTime being a time_t of zero.
// "prevRun" feature is implemented by making a recursive call with "clock"
// set to the latest reference time in the sequence minus one.
// ---------------------------------------------------------------------------

private static Date filterByClock(List<DataTime> times, Date clock,
        boolean prevRun) {
    int next, check;
    long latest = 0L;
    long curRef;
    if (times == null || times.size() == 0 || clock == null) {
        return clock;
    }

    for (next = check = 0; check < times.size(); check++) {
        curRef = (times).get(check).getMatchRef();
        if (curRef > clock.getTime()) {
            continue;
        }
        if (latest < curRef) {
            latest = curRef;
        }
        if (next < check) {
            times.set(next, times.get(check));
        }
        next++;
    }

    if (next < check) {
        for (int i = next; i < check; i++) {
            times.remove(next);
        }
    }

    if (prevRun) {
        return filterByClock(times, new Date(latest - 1), false);           
    } else {
        return new Date(latest);
    }
}

0 个答案:

没有答案