Example usage for org.joda.time Interval getStart

List of usage examples for org.joda.time Interval getStart

Introduction

In this page you can find the example usage for org.joda.time Interval getStart.

Prototype

public DateTime getStart() 

Source Link

Document

Gets the start of this time interval, which is inclusive, as a DateTime.

Usage

From source file:com.yahoo.bard.webservice.util.DateTimeUtils.java

License:Apache License

/**
 * Check to see if we can determine availability from the given available and needed intervals.
 *
 * @param available  Available interval/*from w  w w. jav a2s . c o m*/
 * @param needed  Needed interval
 *
 * @return True if we can determine availability, false if not
 */
private static boolean canDetermineAvailability(Interval available, Interval needed) {
    if (available != null && needed != null) {
        if (available.contains(needed) || available.getStart().isAfter(needed.getStart())) {
            return true;
        }
    }
    return false;
}

From source file:com.yahoo.bard.webservice.util.DateTimeUtils.java

License:Apache License

/**
 * Converts an interval to a specified string format.
 *
 * @param interval  interval to be formatted
 * @param formatter  date time formatter for the
 * @param separator  string to separate interval start and end
 *
 * @return formatted interval string/*  w  w  w  .ja  v  a  2s.c o  m*/
 */
public static String intervalToString(Interval interval, DateTimeFormatter formatter, String separator) {
    return interval.getStart().toString(formatter) + separator + interval.getEnd().toString(formatter);
}

From source file:com.yahoo.bard.webservice.util.DateTimeUtils.java

License:Apache License

/**
 * Slices the intervals into smaller intervals of the timeGrain duration.
 *
 * @param interval  interval to be sliced
 * @param timeGrain  size of the slice/*from   w w w  .j a va 2  s .  c  om*/
 *
 * @return list of intervals obtained by slicing the larger interval
 *
 * @throws java.lang.IllegalArgumentException if the interval is not an even multiple of the time grain
 */
public static List<Interval> sliceIntervals(Interval interval, TimeGrain timeGrain) {
    // TODO: Refactor me to use a Period
    DateTime intervalEnd = interval.getEnd();
    DateTime sliceStart = interval.getStart();
    DateTime periodStart = timeGrain.roundFloor(sliceStart);

    if (!sliceStart.equals(periodStart)) {
        LOG.info("Interval {} is not aligned to TimeGrain {} starting {}", interval, timeGrain, periodStart);
        throw new IllegalArgumentException("Interval must be aligned to the TimeGrain starting " + periodStart);
    }

    List<Interval> intervalSlices = new ArrayList<>();
    while (sliceStart.isBefore(intervalEnd)) {
        // Find the end of the next slice
        DateTime sliceEnd = DateTimeUtils.addTimeGrain(sliceStart, timeGrain);

        // Make the next slice
        Interval slicedInterval = new Interval(sliceStart, sliceEnd);

        // Make sure that our slice is fully contained within our interval
        if (!interval.contains(slicedInterval)) {
            LOG.info("Interval {} is not a multiple of TimeGrain {}", interval, timeGrain);
            throw new IllegalArgumentException("Interval must be a multiple of the TimeGrain");
        }

        // Add the slice
        intervalSlices.add(slicedInterval);

        // Move the slicer forward
        sliceStart = sliceEnd;
    }
    LOG.debug("Sliced interval {} into {} slices of {} grain", interval, intervalSlices.size(), timeGrain);

    return intervalSlices;
}

From source file:com.yahoo.bard.webservice.util.IntervalPeriodIterator.java

License:Apache License

/**
 * Constructor./* w  ww  . j  a v  a  2  s  .  c  om*/
 *
 * @param period  The period to divide the interval by
 * @param baseInterval  The raw interval which is to be divided
 */
public IntervalPeriodIterator(@NotNull ReadablePeriod period, Interval baseInterval) {
    this.period = period;
    intervalStart = baseInterval.getStart();
    intervalEnd = baseInterval.getEnd();
    position = 0;
    currentPosition = boundaryAt(0);

    // Chronology accepts null periods, we must not do so or this iterator is non-terminating
    if (period == null) {
        throw new IllegalArgumentException("Period cannot be null");
    }
}

From source file:com.yahoo.bard.webservice.util.IntervalStartComparator.java

License:Apache License

@Override
public int compare(Interval a, Interval b) {
    return a.getStart().compareTo(b.getStart());
}

From source file:com.yahoo.bard.webservice.util.IntervalUtils.java

License:Apache License

/**
 * Find a valid timegrain for the interval based on the start and end date of the interval.
 *
 * @param interval  The interval to find a timegrain for.
 * @return the valid timegrain spanned by the interval.
 *///from   www. j  a va 2s . c  o  m
public static Optional<TimeGrain> getTimeGrain(Interval interval) {
    return StandardGranularityParser.getDefaultGrainMap().values().stream()
            .filter(granularity -> granularity instanceof TimeGrain).map(granularity -> (TimeGrain) granularity)
            .filter(timeGrain -> timeGrain.aligns(interval))
            .filter(timeGrain -> interval.getStart().plus(timeGrain.getPeriod()).equals(interval.getEnd()))
            .findFirst();
}

From source file:com.yahoo.bard.webservice.util.SimplifiedIntervalList.java

License:Apache License

/**
 * Given a sorted linked list of intervals, add the following interval to the end, merging the incoming interval
 * to any tail intervals which overlap or abut with it.
 * <p>/* w w  w.j av  a 2  s.co m*/
 * In the case where added intervals are at the end of the list, this is efficient. In the case where they are not,
 * this degrades to an insertion sort.
 *
 * @param interval  The interval to be merged and added to this list
 */
private void appendWithMerge(Interval interval) {
    // Do not store empty intervals
    if (interval.toDurationMillis() == 0) {
        return;
    }

    if (isEmpty()) {
        addLast(interval);
        return;
    }
    final Interval previous = peekLast();

    // If this interval does not belong at the end, removeLast until it does
    if (interval.getStart().isBefore(previous.getStart())) {
        mergeInner(interval);
        return;
    }

    if (previous.gap(interval) != null) {
        addLast(interval);
        return;
    }
    removeLast();
    Interval newEnd = new Interval(Math.min(previous.getStartMillis(), interval.getStartMillis()),
            Math.max(previous.getEndMillis(), interval.getEndMillis()));
    addLast(newEnd);
}

From source file:com.yahoo.bard.webservice.util.SimplifiedIntervalList.java

License:Apache License

/**
 * Back elements off the list until the insertion is at the correct endpoint of the list and then merge and append
 * the original contents of the list back in.
 *
 * @param interval  The interval to be merged and added
 *//*from ww  w. ja  v  a  2 s.c  om*/
private void mergeInner(Interval interval) {
    Interval previous = peekLast();
    LinkedList<Interval> buffer = new LinkedList<>();
    while (previous != null && interval.getStart().isBefore(previous.getStart())) {
        buffer.addFirst(previous);
        removeLast();
        previous = peekLast();
    }
    appendWithMerge(interval);
    buffer.stream().forEach(this::appendWithMerge);
}

From source file:com.yahoo.bard.webservice.util.SimplifiedIntervalList.java

License:Apache License

/**
 * Return the subtracted list of all intervals in this that are not in that.
 *
 * @param that  A simplified list of intervals
 *
 * @return A new simplified interval list whose intervals are all subintervals of this and not that
 *//*from ww  w . j  av  a 2 s . c om*/
public SimplifiedIntervalList subtract(SimplifiedIntervalList that) {
    Iterator<Interval> theseIntervals = this.iterator();
    Interval thisCurrent = getNextIfAvailable.apply(theseIntervals);

    if (thisCurrent == null) {
        return new SimplifiedIntervalList();
    }

    Iterator<Interval> thoseIntervals = that.iterator();

    Interval thatCurrent = getNextIfAvailable.apply(thoseIntervals);
    List<Interval> collected = new ArrayList<>();

    while (thisCurrent != null && thatCurrent != null) {
        if (thisCurrent.isBefore(thatCurrent)) {
            // Non overlapping intervals are simply collected
            collected.add(thisCurrent);
        } else if (thisCurrent.overlaps(thatCurrent)) {
            // Take any part of the source interval that lies before an overlap
            if (thisCurrent.getStart().isBefore(thatCurrent.getStart())) {
                collected.add(new Interval(thisCurrent.getStart(), thatCurrent.getStart()));
            }
            // Truncate out any overlap from the source interval and continue
            if (!thisCurrent.getEnd().isBefore(thatCurrent.getEnd())) {
                thisCurrent = new Interval(thatCurrent.getEnd(), thisCurrent.getEnd());
            }
        }
        // Advance to the next interval to consider
        if (thisCurrent.isBefore(thatCurrent.getEnd())) {
            thisCurrent = getNextIfAvailable.apply(theseIntervals);
        } else {
            thatCurrent = getNextIfAvailable.apply(thoseIntervals);
        }
    }
    if (thatCurrent == null) {
        collected.add(thisCurrent);
        while (theseIntervals.hasNext()) {
            collected.add(theseIntervals.next());
        }
    }
    return new SimplifiedIntervalList(collected);
}

From source file:csv.CSVCreator.java

/**
 * Converts a duration object to a descriptive string of that object
 *
 * @param interval//from w ww . j  a  v a  2s  .c o m
 * @return a descriptive string of the given input
 */
private String convertDurationToString(Interval interval) {
    DateTime start = interval.getStart();
    DateTime end = interval.getEnd();

    DateTimeFormatter df = DateTimeFormat.forPattern("y-MM-dd");

    return df.print(start) + " - " + df.print(end);
}