/*
 * Copyright 2016 E-System LLC
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.es.lib.common;

import com.es.lib.common.builder.DateBuilder;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.TimeZone;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Zuzoev Dmitry - zuzoev.d@ext-system.com
 * @since 10.04.15
 */
public class DateRange {

    private Date dbegin;
    private Date dend;

    private DateRange(Date dbegin, Date dend) {
        this.dbegin = dbegin;
        this.dend = dend;
    }

    public static Collection<SItem> getAll(TimeZone timeZone) {
        return Stream.of(DateRange.Interval.values())
            .map(v -> v.getItem(timeZone))
            .collect(Collectors.toList());
    }

    public static DateRange getDefaultRange(TimeZone timeZone) {
        return Interval.TODAY.getRange(timeZone);
    }

    public Date getDbegin() {
        return dbegin;
    }

    public void setDbegin(Date dbegin) {
        this.dbegin = dbegin;
    }

    public Date getDend() {
        return dend;
    }

    public void setDend(Date dend) {
        this.dend = dend;
    }

    public String getIntervalString() {
        return DateUtil.format(getDbegin(), DateUtil.CALENDAR_DATE_PATTERN) + "|" + DateUtil.format(getDend(), DateUtil.CALENDAR_DATE_PATTERN);
    }

    @Override
    public String toString() {
        return "DateRange[dbegin=" + dbegin + ", dend=" + dend + ']';
    }

    public enum Interval {
        TODAY {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().build(),
                    DateBuilder.create(timeZone).clearTime().addDayOfMonth(lastNextDay ? 1 : 0).build()
                );
            }
        },
        YESTERDAY {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().addDayOfMonth(-1).build(),
                    DateBuilder.create(timeZone).clearTime().addDayOfMonth(lastNextDay ? 0 : -1).build()
                );
            }
        },
        LAST_7_DAYS {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().addDayOfMonth(-7).build(),
                    DateBuilder.create(timeZone).clearTime().addDayOfMonth(lastNextDay ? 1 : 0).build()
                );
            }
        },
        CURRENT_WEEK {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().setDayOfWeek(Calendar.MONDAY).build(),
                    DateBuilder.create(timeZone).clearTime().setDayOfWeek(Calendar.SUNDAY).addDayOfMonth(lastNextDay ? 1 : 0).build()
                );
            }
        },
        LAST_WEEK {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().addWeekOfMonth(-1).setDayOfWeek(Calendar.MONDAY).build(),
                    DateBuilder.create(timeZone).clearTime().addWeekOfMonth(-1).setDayOfWeek(Calendar.SUNDAY).addDayOfMonth(lastNextDay ? 1 : 0).build()
                );
            }
        },
        CURRENT_MONTH {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).build(),
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).addMonth(1).addDayOfMonth(lastNextDay ? 0 : -1).build()
                );
            }
        },
        LAST_MONTH {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().addMonth(-1).setDayOfMonth(1).build(),
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).addDayOfMonth(lastNextDay ? 0 : -1).build()
                );
            }
        },
        CURRENT_TRIAD {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).build(),
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).addMonth(3).addDayOfMonth(lastNextDay ? 0 : -1).build()
                );
            }
        },
        LAST_TRIAD {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).addMonth(-3).build(),
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).addDayOfMonth(lastNextDay ? 0 : -1).build()
                );
            }
        },
        CURRENT_YEAR {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).setMonth(0).build(),
                    DateBuilder.create(timeZone).clearTime().addDayOfMonth(lastNextDay ? 1 : 0).build()
                );
            }
        },
        LAST_YEAR {
            @Override
            public DateRange getRange(TimeZone timeZone, boolean lastNextDay) {
                return new DateRange(
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).setMonth(0).addYear(-1).build(),
                    DateBuilder.create(timeZone).clearTime().setDayOfMonth(1).setMonth(0).addDayOfMonth(lastNextDay ? 0 : -1).build()
                );
            }
        };

        public DateRange getRange(TimeZone timeZone) {
            return getRange(timeZone, true);
        }

        public abstract DateRange getRange(TimeZone timeZone, boolean lastNextDay);

        public SItem getItem(TimeZone timeZone) {
            return getItem(timeZone, true);
        }

        public SItem getItem(TimeZone timeZone, boolean lastNextDay) {
            return new SItem(this.getRange(timeZone, lastNextDay).getIntervalString(), this.toString());
        }

    }
}

@@ -1183,32 +1183,53 @@
 imeZone timeZone
+, boolean lastNextDay
 ) %7B%0A        retu
@@ -1310,16 +1310,29 @@
 timeZone
+, lastNextDay
 ))%0A     
@@ -1398,51 +1398,302 @@
 tic 
-DateRange getDefaultRange(TimeZone timeZone
+Collection%3CSItem%3E getAll(TimeZone timeZone) %7B%0A        return getAll(timeZone, true);%0A    %7D%0A%0A    public static DateRange getDefaultRange(TimeZone timeZone) %7B%0A        return getDefaultRange(timeZone, true);%0A    %7D%0A%0A    public static DateRange getDefaultRange(TimeZone timeZone, boolean lastNextDay
 ) %7B%0A
@@ -1739,16 +1739,29 @@
 timeZone
+, lastNextDay
 );%0A    %7D

Add lastNextDay to DateRange intervals