public static DateRange getDateRange()

in datafu-hourglass/src/main/java/datafu/hourglass/jobs/DateRangePlanner.java [52:182]


  public static DateRange getDateRange(Date beginDateOverride, 
                                       Date endDateOverride,
                                       Collection<Date> available,
                                       Integer daysAgo,
                                       Integer numDays,
                                       boolean failOnMissing)
  {
    Date beginDate = null;
    Date endDate = null;
    
    Calendar cal = Calendar.getInstance(PathUtils.timeZone);
            
    // determine the range of available input data
    Date beginAvailable = null;
    Date endAvailable = null;    
    if (available != null && available.size() > 0)
    {
      ArrayList<Date> sortedAvailable = new ArrayList<Date>(available);
      Collections.sort(sortedAvailable);
      beginAvailable = sortedAvailable.get(0);
      endAvailable = sortedAvailable.get(sortedAvailable.size()-1);
    }
    else
    {
      throw new IllegalArgumentException("No data available");
    }
    
    if (endDateOverride != null && beginDateOverride != null)
    {
      beginDate = beginDateOverride;
      endDate = endDateOverride;
      
      _log.info(String.format("Specified begin date is %s",
                              PathUtils.datedPathFormat.format(beginDate)));
      _log.info(String.format("Specified end date is %s",
                              PathUtils.datedPathFormat.format(endDate)));
      
      if (daysAgo != null)
      {
        throw new IllegalArgumentException("Cannot specify days ago when begin and end date set");
      }
       
      if (numDays != null)
      {
        throw new IllegalArgumentException("Cannot specify num days when begin and end date set");
      }
    }
    else if (beginDateOverride != null)
    {
      beginDate = beginDateOverride;
      
      _log.info(String.format("Specified begin date is %s",
                              PathUtils.datedPathFormat.format(beginDate)));
      
      if (numDays != null)
      { 
        cal.setTime(beginDate);
        cal.add(Calendar.DAY_OF_MONTH, numDays-1);
        endDate = cal.getTime();
        _log.info(String.format("Num days is %d, giving end date of %s",
                                numDays, PathUtils.datedPathFormat.format(endDate)));
      }
    }
    else if (endDateOverride != null)
    {
      endDate = endDateOverride;
      
      _log.info(String.format("Specified end date is %s",
                              PathUtils.datedPathFormat.format(endDate)));
      
      if (numDays != null)
      {
        cal.setTime(endDate);
        cal.add(Calendar.DAY_OF_MONTH, -(numDays-1));
        beginDate = cal.getTime();
        _log.info(String.format("Num days is %d, giving begin date of %s",
                                numDays, PathUtils.datedPathFormat.format(beginDate)));
      }
    }
    
    if (endDate == null)
    {
      endDate = endAvailable;
            
      _log.info(String.format("No end date specified, using date for latest available input: %s",
                              PathUtils.datedPathFormat.format(endDate)));
      
      if (daysAgo != null)
      {
        cal.setTime(endDate);
        cal.add(Calendar.DAY_OF_MONTH, -daysAgo);
        endDate = cal.getTime();
        _log.info(String.format("However days ago is %d, giving end date of %s",
                                daysAgo, PathUtils.datedPathFormat.format(endDate)));
      }
    }
    
    if (endAvailable.compareTo(endDate) < 0 && failOnMissing)
    {
      throw new IllegalArgumentException(String.format("Latest available date %s is less than desired end date %s",
                                                       PathUtils.datedPathFormat.format(endAvailable),
                                                       PathUtils.datedPathFormat.format(endDate)));
    }
    
    if (beginDate == null)
    {
      beginDate = beginAvailable;
      
      if (numDays != null)
      {
        cal.setTime(endDate);
        cal.add(Calendar.DAY_OF_MONTH, -(numDays-1));
        beginDate = cal.getTime();
        _log.info(String.format("Num days is %d, giving begin date of %s",
                                numDays, PathUtils.datedPathFormat.format(beginDate)));
      }
    }
    
    if (beginAvailable.compareTo(beginDate) > 0 && failOnMissing)
    {
      throw new IllegalArgumentException(String.format("Desired begin date is %s but the next available date is %s",                                                       
                                                       PathUtils.datedPathFormat.format(beginDate),
                                                       PathUtils.datedPathFormat.format(beginAvailable)));
    }
    
    _log.info(String.format("Determined date range of inputs to consume is [%s,%s]",
                           PathUtils.datedPathFormat.format(beginDate),
                           PathUtils.datedPathFormat.format(endDate)));
    
    return new DateRange(beginDate, endDate);
  }