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

net.intelie.pipes.time.MultiplyPeriod 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.HashMap;
import java.util.Map;
import java.util.Objects;

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

public class MultiplyPeriod implements Period {
    private static final long serialVersionUID = 1L;

    private final int amount;
    private final Period period;

    public MultiplyPeriod(int amount, Period period) {
        this.amount = amount;
        this.period = period;
    }

    @Override
    public boolean supportsFloor() {
        return amount == 1 && period.supportsFloor();
    }

    @Override
    public Map simple() {
        Map map = new HashMap<>();
        map.put("type", "multiply");
        map.put("amount", (double) amount);
        map.put("value", period.simple());
        return map;
    }

    @Override
    public String toString() {
        return toString(true);
    }

    @Override
    public String toString(boolean includeOne) {
        if (!includeOne && amount == 1)
            return period.toString(false);
        return amount + "*(" + period + ")";
    }

    @Override
    public MultiplyPeriod forceZone(ZoneId zone) {
        return new MultiplyPeriod(amount, period.forceZone(zone));
    }

    @Override
    public Period multiply(int amount) {
        return new MultiplyPeriod(this.amount * amount, period);
    }

    @Override
    public long add(long timestamp) {
        for (int i = 0; i < amount; i++)
            timestamp = period.add(timestamp);
        for (int i = 0; i < -amount; i++)
            timestamp = period.sub(timestamp);
        return timestamp;
    }

    @Override
    public long sub(long timestamp) {
        for (int i = 0; i < amount; i++)
            timestamp = period.sub(timestamp);
        for (int i = 0; i < -amount; i++)
            timestamp = period.add(timestamp);
        return timestamp;
    }

    @Override
    public long floor(long timestamp) {
        checkArgument(supportsFloor(),
                "PeriodList with multiple periods doesn't support 'floor' operation.");
        return period.floor(timestamp);
    }

    @Override
    public long estimateMillis() {
        return amount * period.estimateMillis();
    }


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

        MultiplyPeriod that = (MultiplyPeriod) o;

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy