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

net.intelie.pipes.time.AlignSpan 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.Locale;
import java.util.Objects;

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

public class AlignSpan extends TimeSpanBase {
    private static final long serialVersionUID = 1L;
    private final TimeSpan span;
    private final Where where;
    private final TimeSpan refSpan;

    public AlignSpan(TimeSpan span, Where where, TimeSpan refSpan) {
        super("$1 aligned " + checkNotNull(where, "where").toString().toLowerCase(Locale.ROOT) + " to $2",
                checkNotNull(span, "span"),
                checkNotNull(refSpan, "refSpan"));

        this.refSpan = refSpan;
        this.span = span;
        this.where = where;
    }

    @Override
    public AlignSpan forceZone(ZoneId zone) {
        return new AlignSpan(forceZone(span, zone), where, forceZone(refSpan, zone));
    }

    @Override
    public long start(long reference) {
        if (where == Where.LEFT) {
            return refSpan.start(reference);
        } else {
            return refSpan.end(reference) - length(reference);
        }
    }

    @Override
    public long end(long reference) {
        if (where == Where.LEFT) {
            return refSpan.start(reference) + length(reference);
        } else {
            return refSpan.end(reference);
        }
    }

    private long length(long reference) {
        return span.end(reference) - span.start(reference);
    }

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

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

    @Override
    public boolean includesPresent() {
        return refSpan.includesPresent() && (where == Where.RIGHT || where == Where.LEFT && refSpan.isPoint());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AlignSpan)) return false;

        AlignSpan that = (AlignSpan) o;

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy