All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.intelie.pipes.time.WindowOfSpan Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes.time;

import java.time.ZoneId;
import java.util.Objects;

import static net.intelie.pipes.util.Preconditions.checkNotNull;

public class WindowOfSpan extends TimeSpanBase {
    private static final long serialVersionUID = 1L;

    private final TimeSpan span;

    private final Object cacheLock = new Object[0];
    private long cacheArg = -1, cacheRes;

    public WindowOfSpan(TimeSpan span) {
        super("window of $1", checkNotNull(span, "span"));
        this.span = span;
    }

    @Override
    public WindowOfSpan forceZone(ZoneId zone) {
        return new WindowOfSpan(span.forceZone(zone));
    }

    @Override
    public long start(long reference) {
        return span.start(findReference(reference));
    }

    @Override
    public long end(long reference) {
        return span.end(findReference(reference));
    }

    @Override
    public boolean isFixed() {
        return span.isFixed();
    }

    @Override
    public boolean isPoint() {
        return span.isPoint();
    }

    @Override
    public boolean includesPresent() {
        return !span.isFixed();
    }

    public long findReference(long timestamp) {
        /*
        performs a binary search to find the lowest reference timestamp for
        which span.end(reference) >= timestamp.
        this means: the first time the span includes the argument timestamp
        */

        synchronized (cacheLock) {
            if (cacheArg == timestamp)
                return cacheRes;
        }

        long dist = 60 * 60 * 1000;
        long max = (Long.MAX_VALUE - timestamp) / 2;

        while (dist < max && (span.end(timestamp + dist) < timestamp || span.end(timestamp - dist) >= timestamp))
            dist *= 2;

        long first = timestamp - dist, count = dist * 2;
        while (count > 0) {
            long it = first;
            long step = count / 2;
            it += step;

            if (span.end(it) < timestamp) {
                first = ++it;
                count -= step + 1;
            } else count = step;
        }
        synchronized (cacheLock) {
            cacheArg = timestamp;
            cacheRes = first;
        }
        return first;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        WindowOfSpan that = (WindowOfSpan) o;

        return Objects.equals(this.span, that.span);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.span);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy