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

net.intelie.pipes.time.ShiftByPercentSpan 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.checkArgument;
import static net.intelie.pipes.util.Preconditions.checkNotNull;

public class ShiftByPercentSpan extends TimeSpanBase {
    private static final long serialVersionUID = 1L;
    private final TimeSpan span;
    private final Where where;
    private final int value;

    public ShiftByPercentSpan(TimeSpan span, Where where, int value) {
        super("$1 shifted " + checkNotNull(where, "where").toString().toLowerCase(Locale.ROOT) + " by " + value + "%",
                checkNotNull(span, "span"));
        checkArgument(value >= 0, "value must be non-negative");
        this.span = span;
        this.where = where;
        this.value = value;
    }

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

    @Override
    public long start(long reference) {
        long start = span.start(reference);
        long end = span.end(reference);
        start += (where == Where.LEFT ? -1 : 1) * (value / 100.0) * Math.max(end - start, 0);
        return start;
    }

    @Override
    public long end(long reference) {
        long start = span.start(reference);
        long end = span.end(reference);
        end += (where == Where.LEFT ? -1 : 1) * (value / 100.0) * Math.max(end - start, 0);
        return end;
    }

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

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

    @Override
    public boolean includesPresent() {
        return false;
    }

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

        ShiftByPercentSpan that = (ShiftByPercentSpan) o;

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy