public IntervalList build(DataSetHandler ctx, ColumnGroup columnGroup) {
   IntervalListLabel intervalList = new IntervalListLabel(columnGroup);
   String columnId = columnGroup.getSourceId();
   List values = ctx.getDataSet().getColumnById(columnId).getValues();
   List<Integer> rows = ctx.getRows();
   return intervalList.indexValues(values, rows);
 }
  public DateIntervalType calculateIntervalSize(
      Date minDate, Date maxDate, ColumnGroup columnGroup) {

    DateIntervalType intervalType = DateIntervalType.getByName(columnGroup.getIntervalSize());
    if (intervalType == null) {
      intervalType = YEAR;
    }

    if (minDate == null || maxDate == null) {
      return intervalType;
    }

    long millis = (maxDate.getTime() - minDate.getTime());
    if (millis <= 0) {
      return intervalType;
    }

    // Calculate the interval type used according to the constraints set.
    int maxIntervals = columnGroup.getMaxIntervals();
    if (maxIntervals < 1) maxIntervals = 15;
    for (DateIntervalType type : values()) {
      long nintervals = millis / getDurationInMillis(type);
      if (nintervals < maxIntervals) {
        intervalType = type;
        break;
      }
    }

    // Ensure the interval mode obtained is always greater or equals than the preferred interval
    // size.
    DateIntervalType intervalSize = null;
    if (!StringUtils.isBlank(columnGroup.getIntervalSize())) {
      intervalSize = getByName(columnGroup.getIntervalSize());
    }
    if (intervalSize != null && compare(intervalType, intervalSize) == -1) {
      intervalType = intervalSize;
    }
    return intervalType;
  }
 protected Calendar firstIntervalDate(
     DateIntervalType intervalType, Date minDate, ColumnGroup columnGroup) {
   Calendar c = GregorianCalendar.getInstance();
   c.setLenient(false);
   c.setTime(minDate);
   if (YEAR.equals(intervalType)) {
     c.set(Calendar.MONTH, 0);
     c.set(Calendar.DAY_OF_MONTH, 1);
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (QUARTER.equals(intervalType)) {
     int currentMonth = c.get(Calendar.MONTH);
     int firstMonthYear = columnGroup.getFirstMonthOfYear().getIndex();
     int rest = Quarter.getPositionInQuarter(firstMonthYear, currentMonth + 1);
     c.add(Calendar.MONTH, rest * -1);
     c.set(Calendar.DAY_OF_MONTH, 1);
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (MONTH.equals(intervalType)) {
     c.set(Calendar.DAY_OF_MONTH, 1);
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (DAY.equals(intervalType) || DAY_OF_WEEK.equals(intervalType) || WEEK.equals(intervalType)) {
     c.set(Calendar.HOUR, 0);
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (HOUR.equals(intervalType)) {
     c.set(Calendar.MINUTE, 0);
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (MINUTE.equals(intervalType)) {
     c.set(Calendar.SECOND, 0);
     c.set(Calendar.MILLISECOND, 0);
   }
   if (SECOND.equals(intervalType)) {
     c.set(Calendar.MILLISECOND, 0);
   }
   return c;
 }
  public IntervalList build(DataColumn dataColumn) {
    ColumnGroup columnGroup = dataColumn.getColumnGroup();
    Date minDate = (Date) dataColumn.getMinValue();
    Date maxDate = (Date) dataColumn.getMaxValue();

    IntervalDateRangeList results = new IntervalDateRangeList(columnGroup);
    if (minDate == null || maxDate == null) {
      return results;
    }
    DateIntervalType intervalType = DateIntervalType.getByName(dataColumn.getIntervalType());
    if (intervalType == null) {
      intervalType = DateIntervalType.YEAR;
    }
    Calendar c = firstIntervalDate(intervalType, minDate, columnGroup);
    int counter = 0;
    while (c.getTime().compareTo(maxDate) <= 0) {
      Date intervalMinDate = c.getTime();

      // Create the next interval
      nextIntervalDate(c, intervalType, 1);
      Date intervalMaxDate = c.getTime();
      IntervalDateRange interval =
          new IntervalDateRange(counter++, intervalType, intervalMinDate, intervalMaxDate);
      results.add(interval);
    }

    // Reverse intervals if requested
    boolean asc = columnGroup.isAscendingOrder();
    if (!asc) Collections.reverse(results);

    // Return the results
    results.setIntervalType(intervalType.toString());
    results.setMinValue(minDate);
    results.setMaxValue(maxDate);
    return results;
  }
  public IntervalList build(DataSetHandler handler, ColumnGroup columnGroup) {
    IntervalDateRangeList results = new IntervalDateRangeList(columnGroup);
    DataSet dataSet = handler.getDataSet();
    List values = dataSet.getColumnById(columnGroup.getSourceId()).getValues();
    if (values.isEmpty()) {
      return results;
    }

    // Sort the column dates.
    DataSetSort sortOp = new DataSetSort();
    sortOp.addSortColumn(new ColumnSort(columnGroup.getSourceId(), SortOrder.ASCENDING));
    DataSetHandler sortResults = handler.sort(sortOp);
    List<Integer> sortedRows = sortResults.getRows();
    if (sortedRows == null || sortedRows.isEmpty()) {
      return results;
    }

    // Get the lower & upper limits (discard nulls).
    SortedList sortedValues = new SortedList(values, sortedRows);
    Date minDate = null;
    Date maxDate = null;
    for (int i = 0; minDate == null && i < sortedValues.size(); i++) {
      minDate = (Date) sortedValues.get(i);
    }
    for (int i = sortedValues.size() - 1; maxDate == null && i >= 0; i--) {
      maxDate = (Date) sortedValues.get(i);
    }

    // If min/max are equals then return a single interval.
    DateIntervalType intervalType = calculateIntervalSize(minDate, maxDate, columnGroup);
    if (minDate == null || minDate.compareTo(maxDate) == 0) {

      IntervalDateRange interval = new IntervalDateRange(0, intervalType, minDate, maxDate);
      for (int row = 0; row < sortedValues.size(); row++) interval.getRows().add(row);

      results.add(interval);
      results.setIntervalType(columnGroup.getIntervalSize());
      results.setMinValue(minDate);
      results.setMaxValue(maxDate);
      return results;
    }

    // Create the intervals according to the min/max dates.
    Calendar c = firstIntervalDate(intervalType, minDate, columnGroup);
    int index = 0;
    int counter = 0;
    while (c.getTime().compareTo(maxDate) <= 0) {
      Date intervalMinDate = c.getTime();

      // Create the next interval
      nextIntervalDate(c, intervalType, 1);
      Date intervalMaxDate = c.getTime();
      IntervalDateRange interval =
          new IntervalDateRange(counter++, intervalType, intervalMinDate, intervalMaxDate);
      results.add(interval);

      // Add the target rows
      boolean stop = false;
      while (!stop) {
        if (index >= sortedValues.size()) {
          stop = true;
        } else {
          Date dateValue = (Date) sortedValues.get(index);
          Integer row = sortedRows.get(index);
          if (dateValue == null) {
            index++;
          } else if (dateValue.before(intervalMaxDate)) {
            interval.getRows().add(row);
            index++;
          } else {
            stop = true;
          }
        }
      }
    }

    // Reverse intervals if requested
    boolean asc = columnGroup.isAscendingOrder();
    if (!asc) Collections.reverse(results);

    // Return the results
    results.setIntervalType(intervalType.toString());
    results.setMinValue(minDate);
    results.setMaxValue(maxDate);
    return results;
  }