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

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

public class InsideSpan extends TimeSpanBase {
    private static final long serialVersionUID = 1L;
    private final int ordinal;
    private final Period period;
    private final TimeSpan span;
    private final Period cache;
    private final Period cache2;

    public InsideSpan(int ordinal, Period period, TimeSpan span) {
        super(reprOrdinal(ordinal) + " " +
                        checkNotNull(period, "period").toString(false) + " of $1",
                checkNotNull(span, "span"));

        this.ordinal = ordinal;
        this.period = period;
        this.span = span;
        this.cache = ordinal > 1 ? period.multiply(ordinal - 1) : null;
        this.cache2 = period.multiply(ordinal);
    }

    @Override
    public InsideSpan forceZone(ZoneId zone) {
        return new InsideSpan(ordinal, forceZone(period, zone), forceZone(span, zone));
    }

    private static String reprOrdinal(int ordinal) {
        checkArgument(ordinal > 0, "ordinal must be > 0: %d", ordinal);
        return ordinal != 1 ? th(ordinal) : "first";
    }

    public static InsideSpan monthOfYear(int ordinal, ZoneId tz) {
        return new InsideSpan(ordinal,
                new SimplePeriod(1, PeriodUnit.MONTH, tz),
                new PeriodSpan(new SimplePeriod(1, PeriodUnit.YEAR, tz)));
    }

    public static InsideSpan dayOfWeek(int ordinal, ZoneId tz) {
        return dayOfSpan(ordinal, tz,
                new PeriodSpan(new SimplePeriod(1, PeriodUnit.WEEK, tz)));
    }

    public static InsideSpan dayOfSpan(int ordinal, ZoneId tz, TimeSpan span) {
        return new InsideSpan(ordinal,
                new SimplePeriod(1, PeriodUnit.DAY, tz),
                span);
    }

    @Override
    public long start(long reference) {
        reference = span.start(reference);
        if (cache != null)
            reference = cache.add(reference);
        return reference;
    }

    @Override
    public long end(long reference) {
        reference = span.start(reference);
        reference = cache2.add(reference);
        return reference;
    }

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

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

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

    private static String th(int n) {
        int mod10 = n % 10;
        int mod100 = n % 100;
        if (mod10 == 1 && mod100 != 11)
            return n + "st";
        if (mod10 == 2 && mod100 != 12)
            return n + "nd";
        if (mod10 == 3 && mod100 != 13)
            return n + "rd";
        return n + "th";
    }

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

        InsideSpan that = (InsideSpan) o;

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy