org.joda.time.base.BaseInterval Java Examples

The following examples show how to use org.joda.time.base.BaseInterval. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseIntervalRelay.java    From jfixture with MIT License 5 votes vote down vote up
@Override
public Object create(Object request, SpecimenContext context) {

    if (!(request instanceof SpecimenType)) {
        return new NoSpecimen();
    }

    SpecimenType type = (SpecimenType) request;
    if (!BaseInterval.class.isAssignableFrom(type.getRawType())) {
        return new NoSpecimen();
    }

    DateTime dateA = (DateTime) context.resolve(DateTime.class);
    DateTime dateB = (DateTime) context.resolve(DateTime.class);

    try {
        Constructor constructor = type.getRawType().getDeclaredConstructor(ReadableInstant.class, ReadableInstant.class);

        Object specimen;
        if (dateA.isBefore(dateB))
            specimen = constructor.newInstance(dateA, dateB);
        else
            specimen = constructor.newInstance(dateB, dateA);

        return specimen;

    } catch (Exception e) {
        e.printStackTrace();
        return new NoSpecimen();
    }
}
 
Example #2
Source File: OfflineSegmentIntervalChecker.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the number of missing segments based on the given existing segment intervals and the expected frequency
 * of the intervals.
 * <p>We count the interval as missing if there are at least two intervals between the start of the previous interval
 * and current interval. For long intervals (span over multiple intervals), count its start time as the start time of
 * the last interval it covers.
 *
 * @param segmentIntervals List of existing segment intervals
 * @param frequency Expected interval frequency
 * @return Number of missing segments
 */
@VisibleForTesting
static int computeNumMissingSegments(List<Interval> segmentIntervals, Duration frequency) {
  int numSegments = segmentIntervals.size();

  // If there are less than two segments, none can be missing
  if (numSegments < 2) {
    return 0;
  }

  // Sort the intervals by ascending starting time
  segmentIntervals.sort(Comparator.comparingLong(BaseInterval::getStartMillis));

  int numMissingSegments = 0;
  long frequencyMs = frequency.getMillis();
  long lastStartTimeMs = -1L;
  for (Interval segmentInterval : segmentIntervals) {
    long startTimeMs = segmentInterval.getStartMillis();
    if (lastStartTimeMs != -1L && startTimeMs - lastStartTimeMs > frequencyMs) {
      // If there are at least two intervals between the start of the previous interval and current interval, then
      // count the interval(s) as missing
      numMissingSegments += (startTimeMs - lastStartTimeMs - frequencyMs) / frequencyMs;
    }
    // Handle long intervals
    long endTimeMs = segmentInterval.getEndMillis();
    while (startTimeMs + frequencyMs <= endTimeMs) {
      startTimeMs += frequencyMs;
    }
    lastStartTimeMs = Math.max(lastStartTimeMs, startTimeMs);
  }
  return numMissingSegments;
}