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

com.hfg.datetime.DateRange Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.datetime;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.hfg.math.Range;
import com.hfg.util.CompareUtil;
import com.hfg.util.collection.CollectionUtil;

//------------------------------------------------------------------------------
/**
 Range of Dates.
 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class DateRange implements Cloneable, Comparable
{
   private Range mMillisRange;

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public DateRange()
   {
      mMillisRange = new Range<>();
   }

   //--------------------------------------------------------------------------
   public DateRange(Date inStartDate, Date inEndDate)
   {
      mMillisRange = new Range<>(inStartDate != null ? inStartDate.getTime() : null, inEndDate != null ? inEndDate.getTime() : null);
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   public static DateRange yearToDate()
   {
      Calendar cal = new GregorianCalendar();
      cal.set(Calendar.YEAR, DateUtil.getCurrentYear());
      cal.set(Calendar.MONTH, 0);
      cal.set(Calendar.DAY_OF_MONTH, 1);
      cal.set(Calendar.HOUR_OF_DAY, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
      cal.set(Calendar.MILLISECOND, 0);

      return new DateRange(cal.getTime(), new Date());
   }

   //--------------------------------------------------------------------------
   public static DateRange monthToDate()
   {
      Calendar cal = new GregorianCalendar();
      cal.set(Calendar.YEAR, DateUtil.getCurrentYear());
      // Don't change the current month
      cal.set(Calendar.DAY_OF_MONTH, 1);
      cal.set(Calendar.HOUR_OF_DAY, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
      cal.set(Calendar.MILLISECOND, 0);

      return new DateRange(cal.getTime(), new Date());
   }

   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return String.format("[%s, %s]",
                           getStartDate() != null ? getStartDate().toString() : null,
                           getEndDate() != null ? getEndDate().toString() : null);
   }

   //--------------------------------------------------------------------------
   public DateRange clone()
   {
      DateRange clone;
      try
      {
         clone = (DateRange) super.clone();

         clone.mMillisRange = mMillisRange.clone();
      }
      catch (CloneNotSupportedException e)
      {
         throw new RuntimeException(e);
      }

      return clone;
   }

   //--------------------------------------------------------------------------
   @Override
   public int hashCode()
   {
      int hashCode = (getStartDate() != null ? getStartDate().hashCode() : 0);
      hashCode += 31 * (getEndDate() != null ? getEndDate().hashCode() : 0);

      return hashCode;
   }

   //--------------------------------------------------------------------------
   @Override
   public boolean equals(Object inObj2)
   {
      boolean result = false;

      if (inObj2 instanceof DateRange)
      {
         result = (0 == compareTo((DateRange)inObj2));
      }

      return result;
   }

   //--------------------------------------------------------------------------
   @Override
   public int compareTo(DateRange inObj2)
   {
      int result = 0;

      if (null == inObj2)
      {
         result = 1;
      }
      else
      {
         // Some custom work here to ensure that null starts sort first and
         // null ends sort last
         if (getStartDate() != null)
         {
            if (inObj2.getStartDate() != null)
            {
               result = CompareUtil.compare(getStartDate(), inObj2.getStartDate());
            }
         }
         else if (inObj2.getStartDate() != null)
         {
            result = -1;
         }

         if (0 == result)
         {
            if (getEndDate() != null)
            {
               if (inObj2.getEndDate() != null)
               {
                  result = CompareUtil.compare(getEndDate(), inObj2.getEndDate());
               }
            }
            else if (inObj2.getEndDate() != null)
            {
               result = 1;
            }
         }
      }

      return result;
   }

   //--------------------------------------------------------------------------
   public DateRange setStartDate(Date inValue)
   {
      mMillisRange.setStart(inValue.getTime());
      return this;
   }

   //--------------------------------------------------------------------------
   public Date getStartDate()
   {
      Date date = null;
      Long startMillis = mMillisRange.getStart();
      if (startMillis != null)
      {
         date = new Date(startMillis);
      }

      return date;
   }

   //--------------------------------------------------------------------------
   public DateRange setEndDate(Date inValue)
   {
      mMillisRange.setEnd(inValue.getTime());
      return this;
   }

   //--------------------------------------------------------------------------
   public Date getEndDate()
   {
      Date date = null;
      Long endMillis = mMillisRange.getEnd();
      if (endMillis != null)
      {
         date = new Date(endMillis);
      }

      return date;
   }

   //--------------------------------------------------------------------------
   public boolean contains(Date inValue)
   {
      return inValue != null
             && mMillisRange.contains(inValue.getTime());
   }

   //--------------------------------------------------------------------------
   public boolean contains(DateRange inRange2)
   {
      return (contains(inRange2.getStartDate())
              && contains(inRange2.getEndDate()));
   }

   //--------------------------------------------------------------------------
   public List subtract(DateRange inRange2)
   {
      List dateRangeResult = null;

      if (inRange2 != null)
      {
         List> result = mMillisRange.subtract(inRange2.mMillisRange);
         if (CollectionUtil.hasValues(result))
         {
            dateRangeResult = new ArrayList<>(result.size());

            for (Range range : result)
            {
               dateRangeResult.add(new DateRange(range.getStart() != null ? new Date(range.getStart()) : null,
                                                 range.getEnd() != null ? new Date(range.getEnd()) : null));
            }
         }
      }
      else
      {
         dateRangeResult = new ArrayList<>(1);
         dateRangeResult.add(clone());
      }

      return dateRangeResult;
   }

   //--------------------------------------------------------------------------
   public DateRange intersection(DateRange inRange2)
   {
      DateRange dateRangeResult = null;

      if (inRange2 != null)
      {
         Range result = mMillisRange.intersection(inRange2.mMillisRange);
         if (result != null)
         {
            dateRangeResult = new DateRange(result.getStart() != null ? new Date(result.getStart()) : null,
                                            result.getEnd() != null ? new Date(result.getEnd()) : null);
         }
      }

      return dateRangeResult;
   }

   //--------------------------------------------------------------------------
   public boolean intersects(DateRange inRange2)
   {
      boolean result = false;

      if (inRange2 != null)
      {
         result = mMillisRange.intersects(inRange2.mMillisRange);
      }

      return result;
   }


   //---------------------------------------------------------------------------
   /**
    Collapses a Collection of specified DateRanges.
    @param inOrigList a Collection of DateRanges to be combined.
    @return a List of DateRange that represents the union of the specified DateRange.
    */
   public static List union(Collection inOrigList)
   {
      List resultRanges = null;

      if (CollectionUtil.hasValues(inOrigList))
      {
         // Convert to a List of Range
         List> longRanges = new ArrayList<>(inOrigList.size());
         for (DateRange range : inOrigList)
         {
            longRanges.add(range.mMillisRange);
         }

         List> union = Range.union(longRanges);

         resultRanges = new ArrayList<>(union.size());
         for (Range range : union)
         {
            resultRanges.add(new DateRange(range.getStart() != null ? new Date(range.getStart()) : null,
                                           range.getEnd() != null ? new Date(range.getEnd()) : null));
         }
      }

      return resultRanges;
   }

   //---------------------------------------------------------------------------
   /**
    Returns a single DateRange that encompasses the collection of input ranges and ignores internal gaps.
    @param inOrigList a collection of input ranges
    @return a single DateRange that encompasses the input ranges and ignores internal gaps
    */
   public static DateRange superUnion(Collection inOrigList)
   {
      DateRange dateRangeResult = null;

      if (CollectionUtil.hasValues(inOrigList))
      {
         // Convert to a List of Range
         List> longRanges = new ArrayList<>(inOrigList.size());
         for (DateRange range : inOrigList)
         {
            longRanges.add(range.mMillisRange);
         }

         Range superUnion = Range.superUnion(longRanges);
         dateRangeResult = new DateRange(superUnion.getStart() != null ? new Date(superUnion.getStart()) : null,
                                         superUnion.getEnd() != null ? new Date(superUnion.getEnd()) : null);
      }

      return dateRangeResult;
   }

   //---------------------------------------------------------------------------
   /**
    Returns a single DateRange that encompasses both input ranges and ignores internal gaps.
    @param inRange2 the second DateRange to union with the current range
    @return a single DateRange that encompasses both input ranges and ignores internal gaps
    */
   public DateRange superUnion(DateRange inRange2)
   {
      DateRange dateRangeResult;

      if (inRange2 != null)
      {
         Range superUnion = mMillisRange.superUnion(inRange2.mMillisRange);
         dateRangeResult = new DateRange(superUnion.getStart() != null ? new Date(superUnion.getStart()) : null,
                                         superUnion.getEnd() != null ? new Date(superUnion.getEnd()) : null);
      }
      else
      {
         dateRangeResult = clone();
      }

      return dateRangeResult;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy