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

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

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

import net.intelie.pipes.util.DSTHelper;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Locale;
import java.util.Objects;

public class HourSpan extends TimeSpanBase {
    private static final long serialVersionUID = 1L;
    private final int hour;
    private final int minute;
    private final int second;
    private final ZoneId zone;
    private final SimplePeriod unit;

    public HourSpan(int hour, int minute, int second, PeriodUnit span, ZoneId zone) {
        super(pad(hour, 2) + (PeriodUnit.MINUTE.compareTo(span) < 0 ? "h" : "") +
                (PeriodUnit.MINUTE.compareTo(span) >= 0 ? ":" + pad(minute, 2) : "") +
                (PeriodUnit.SECOND.compareTo(span) >= 0 ? ":" + pad(second, 2) : ""));
        this.hour = hour;
        this.minute = minute;
        this.second = second;
        this.zone = zone;
        this.unit = new SimplePeriod(1, span, zone);
    }

    @Override
    public HourSpan forceZone(ZoneId zone) {
        return new HourSpan(hour, minute, second, unit.unit(), zone);
    }

    private static String pad(int v, int length) {
        return String.format((Locale) null, "%0" + length + "d", v);
    }

    @Override
    public long start(long reference) {
        LocalDateTime local = ZonedDateTime.ofInstant(Instant.ofEpochMilli(reference), zone)
                .toLocalDateTime()
                .withHour(hour)
                .withMinute(minute)
                .withSecond(second)
                .withNano(0);
        local = DSTHelper.nextValid(local, zone);
        return ZonedDateTime.of(local, zone).toInstant().toEpochMilli();
    }

    @Override
    public long end(long reference) {
        return unit.add(start(reference));
    }

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

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

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

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

        HourSpan that = (HourSpan) o;

        return Objects.equals(this.hour, that.hour) &&
                Objects.equals(this.minute, that.minute) &&
                Objects.equals(this.second, that.second) &&
                Objects.equals(this.unit, that.unit) &&
                Objects.equals(this.zone, that.zone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(hour, minute, second, unit, zone);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy