package com.lanyuanxiaoyao.service.executor.task.helper;

import java.util.concurrent.TimeUnit;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;

/**
 * @author lanyuanxiaoyao
 */
public class TimeRangeHelper {
    public static final class TimeRange {
        private final long start;
        private final long end;
        private final long gap;

        public TimeRange(long start, long end, long gap) {
            this.start = start;
            this.end = end;
            this.gap = gap;
        }

        public long getStart() {
            return start;
        }

        public long getEnd() {
            return end;
        }

        public long getGap() {
            return gap;
        }
    }

    public static ImmutableList<TimeRange> range(long start, long end, long gap) {
        if (gap < TimeUnit.MINUTES.toMillis(5)) {
            return Lists.immutable.empty();
        }
        MutableList<TimeRange> ranges = Lists.mutable.empty();
        while (start <= end) {
            ranges.add(new TimeRange(start, Math.min(end, start + gap), gap));
            start += gap;
        }
        return ranges.toImmutable();
    }
}
