Example usage for org.joda.time Interval isBefore

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

Introduction

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

Prototype

public boolean isBefore(long millisInstant) 

Source Link

Document

Is this time interval before the specified millisecond instant.

Usage

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

License:Apache License

/**
 * Return the intersection of all subintervals in two interval lists.
 *
 * @param that  A simplified list of intervals
 *
 * @return A new simplified interval list whose intervals are all subintervals of this and that.
 *///from   w  ww.j a  v  a 2 s  .c  o m
public SimplifiedIntervalList intersect(SimplifiedIntervalList that) {
    Iterator<Interval> theseIntervals = this.iterator();
    Iterator<Interval> thoseIntervals = that.iterator();
    Interval thisCurrent = getNextIfAvailable.apply(theseIntervals);
    Interval thatCurrent = getNextIfAvailable.apply(thoseIntervals);
    List<Interval> collected = new ArrayList<>();

    while (thisCurrent != null && thatCurrent != null) {
        if (thisCurrent.overlaps(thatCurrent)) {
            collected.add(thisCurrent.overlap(thatCurrent));
        }
        if (thisCurrent.isBefore(thatCurrent.getEnd())) {
            thisCurrent = getNextIfAvailable.apply(theseIntervals);
        } else {
            thatCurrent = getNextIfAvailable.apply(thoseIntervals);
        }
    }
    return new SimplifiedIntervalList(collected);
}

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
 *///ww w. j a v a2s.co  m
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:io.druid.server.ClientInfoResource.java

License:Apache License

@GET
@Path("/{dataSourceName}")
@Produces(MediaType.APPLICATION_JSON)/*from  www .  j  ava  2s  .com*/
public Map<String, Object> getDatasource(@PathParam("dataSourceName") String dataSourceName,
        @QueryParam("interval") String interval, @QueryParam("full") String full) {
    if (full == null) {
        return ImmutableMap.<String, Object>of(KEY_DIMENSIONS,
                getDatasourceDimensions(dataSourceName, interval), KEY_METRICS,
                getDatasourceMetrics(dataSourceName, interval));
    }

    Interval theInterval;
    if (interval == null || interval.isEmpty()) {
        DateTime now = getCurrentTime();
        theInterval = new Interval(segmentMetadataQueryConfig.getDefaultHistory(), now);
    } else {
        theInterval = new Interval(interval);
    }

    TimelineLookup<String, ServerSelector> timeline = timelineServerView
            .getTimeline(new TableDataSource(dataSourceName));
    Iterable<TimelineObjectHolder<String, ServerSelector>> serversLookup = timeline != null
            ? timeline.lookup(theInterval)
            : null;
    if (serversLookup == null || Iterables.isEmpty(serversLookup)) {
        return Collections.EMPTY_MAP;
    }
    Map<Interval, Object> servedIntervals = new TreeMap<>(new Comparator<Interval>() {
        @Override
        public int compare(Interval o1, Interval o2) {
            if (o1.equals(o2) || o1.overlaps(o2)) {
                return 0;
            } else {
                return o1.isBefore(o2) ? -1 : 1;
            }
        }
    });

    for (TimelineObjectHolder<String, ServerSelector> holder : serversLookup) {
        final Set<Object> dimensions = Sets.newHashSet();
        final Set<Object> metrics = Sets.newHashSet();
        final PartitionHolder<ServerSelector> partitionHolder = holder.getObject();
        if (partitionHolder.isComplete()) {
            for (ServerSelector server : partitionHolder.payloads()) {
                final DataSegment segment = server.getSegment();
                dimensions.addAll(segment.getDimensions());
                metrics.addAll(segment.getMetrics());
            }
        }

        servedIntervals.put(holder.getInterval(),
                ImmutableMap.of(KEY_DIMENSIONS, dimensions, KEY_METRICS, metrics));
    }

    //collapse intervals if they abut and have same set of columns
    Map<String, Object> result = Maps.newLinkedHashMap();
    Interval curr = null;
    Map<String, Set<String>> cols = null;
    for (Map.Entry<Interval, Object> e : servedIntervals.entrySet()) {
        Interval ival = e.getKey();
        if (curr != null && curr.abuts(ival) && cols.equals(e.getValue())) {
            curr = curr.withEnd(ival.getEnd());
        } else {
            if (curr != null) {
                result.put(curr.toString(), cols);
            }
            curr = ival;
            cols = (Map<String, Set<String>>) e.getValue();
        }
    }
    //add the last one in
    if (curr != null) {
        result.put(curr.toString(), cols);
    }
    return result;
}

From source file:op.allowance.PnlAllowance.java

License:Open Source License

private void updateCarrySums(Resident resident, LocalDate pit, BigDecimal amount) {
    String prevKey = getKey(resident, SYSCalendar.eom(pit.minusMonths(1)));
    if (!carrySums.containsKey(prevKey)) {
        carrySums.put(prevKey, AllowanceTools.getSUM(resident, SYSCalendar.eom(pit).minusMonths(1)));
    }/*from  ww w.j a va 2s. c o  m*/

    // update carrysums
    for (LocalDate month = SYSCalendar.eom(pit); month
            .compareTo(SYSCalendar.eoy(new LocalDate())) <= 0; month = SYSCalendar.eom(month.plusMonths(1))) {
        OPDE.debug(month.toString("yyyy-MM-dd"));
        final String key = getKey(resident, month);

        if (!carrySums.containsKey(key)) {
            prevKey = getKey(resident, SYSCalendar.eom(month.minusMonths(1)));
            carrySums.put(key, carrySums.get(prevKey).add(amount));
        } else {
            carrySums.put(key, carrySums.get(key).add(amount));
        }

        contentmap.remove(key);
    }

    // fix minmax interval
    Interval myMinMax = new Interval(minmax.get(resident).getFirst().toDateTimeAtStartOfDay(),
            SYSCalendar.eod(minmax.get(resident).getSecond()));
    if (myMinMax.isBefore(pit.toDateTimeAtCurrentTime())) {
        minmax.put(resident, new Pair(minmax.get(resident).getFirst(), SYSCalendar.eom(pit)));
    } else if (myMinMax.isAfter(pit.toDateTimeAtCurrentTime())) {
        minmax.put(resident, new Pair(SYSCalendar.bom(pit), minmax.get(resident).getSecond()));
    }

}

From source file:org.apache.druid.server.ClientInfoResource.java

License:Apache License

@GET
@Path("/{dataSourceName}")
@Produces(MediaType.APPLICATION_JSON)/* w  ww .  ja  va  2  s.  c  om*/
@ResourceFilters(DatasourceResourceFilter.class)
public Map<String, Object> getDatasource(@PathParam("dataSourceName") String dataSourceName,
        @QueryParam("interval") String interval, @QueryParam("full") String full) {
    if (full == null) {
        return ImmutableMap.of(KEY_DIMENSIONS, getDataSourceDimensions(dataSourceName, interval), KEY_METRICS,
                getDataSourceMetrics(dataSourceName, interval));
    }

    Interval theInterval;
    if (interval == null || interval.isEmpty()) {
        DateTime now = getCurrentTime();
        theInterval = new Interval(segmentMetadataQueryConfig.getDefaultHistory(), now);
    } else {
        theInterval = Intervals.of(interval);
    }

    TimelineLookup<String, ServerSelector> timeline = timelineServerView
            .getTimeline(new TableDataSource(dataSourceName));
    Iterable<TimelineObjectHolder<String, ServerSelector>> serversLookup = timeline != null
            ? timeline.lookup(theInterval)
            : null;
    if (serversLookup == null || Iterables.isEmpty(serversLookup)) {
        return Collections.EMPTY_MAP;
    }
    Map<Interval, Object> servedIntervals = new TreeMap<>(new Comparator<Interval>() {
        @Override
        public int compare(Interval o1, Interval o2) {
            if (o1.equals(o2) || o1.overlaps(o2)) {
                return 0;
            } else {
                return o1.isBefore(o2) ? -1 : 1;
            }
        }
    });

    for (TimelineObjectHolder<String, ServerSelector> holder : serversLookup) {
        final Set<Object> dimensions = new HashSet<>();
        final Set<Object> metrics = new HashSet<>();
        final PartitionHolder<ServerSelector> partitionHolder = holder.getObject();
        if (partitionHolder.isComplete()) {
            for (ServerSelector server : partitionHolder.payloads()) {
                final DataSegment segment = server.getSegment();
                dimensions.addAll(segment.getDimensions());
                metrics.addAll(segment.getMetrics());
            }
        }

        servedIntervals.put(holder.getInterval(),
                ImmutableMap.of(KEY_DIMENSIONS, dimensions, KEY_METRICS, metrics));
    }

    //collapse intervals if they abut and have same set of columns
    Map<String, Object> result = Maps.newLinkedHashMap();
    Interval curr = null;
    Map<String, Set<String>> cols = null;
    for (Map.Entry<Interval, Object> e : servedIntervals.entrySet()) {
        Interval ival = e.getKey();
        if (curr != null && curr.abuts(ival) && cols.equals(e.getValue())) {
            curr = curr.withEnd(ival.getEnd());
        } else {
            if (curr != null) {
                result.put(curr.toString(), cols);
            }
            curr = ival;
            cols = (Map<String, Set<String>>) e.getValue();
        }
    }
    //add the last one in
    if (curr != null) {
        result.put(curr.toString(), cols);
    }
    return result;
}

From source file:org.kalypso.ui.rrm.internal.timeseries.view.actions.MergeTimeseriesOperation.java

License:Open Source License

private void validateTimeseries(final DateRange baseRange, final Period baseTimestep,
        final DateRange importRange, final Period importTimestep) throws CoreException {
    /* The timesteps must be equal. */
    final Minutes baseMinutes = baseTimestep.toStandardMinutes();
    final Minutes importMinutes = importTimestep.toStandardMinutes();
    if (baseMinutes.getMinutes() != importMinutes.getMinutes())
        throw new CoreException(new Status(IStatus.ERROR, KalypsoUIRRMPlugin.getID(),
                String.format(Messages.getString("MergeTimeseriesOperation.0"), baseTimestep.toString(), //$NON-NLS-1$
                        importTimestep.toString())));

    /* Create the intervals. */
    final Interval baseInterval = new Interval(new DateTime(baseRange.getFrom()),
            new DateTime(baseRange.getTo()));
    final Interval importInterval = new Interval(new DateTime(importRange.getFrom()),
            new DateTime(importRange.getTo()));

    /* Is the base range before the import range? */
    /* Only a gap with one timestep is allowed. */
    if (baseInterval.isBefore(importInterval)) {
        final DateTime baseEnd = baseInterval.getEnd();
        final DateTime importStart = importInterval.getStart();

        final Period gap = new Period(baseEnd, importStart);
        final Minutes gapMinutes = gap.toStandardMinutes();
        if (gapMinutes.getMinutes() > 0 && baseMinutes.getMinutes() != gapMinutes.getMinutes())
            throw new CoreException(new Status(IStatus.ERROR, KalypsoUIRRMPlugin.getID(),
                    String.format(Messages.getString("MergeTimeseriesOperation.1"), baseMinutes.toString(), //$NON-NLS-1$
                            gapMinutes.toString())));
    }//w w w. ja  va 2  s .c om

    /* Is the base range after the import range? */
    /* Only a gap with one timestep is allowed. */
    if (baseInterval.isAfter(importInterval)) {
        final DateTime importEnd = importInterval.getEnd();
        final DateTime baseStart = baseInterval.getStart();

        final Period gap = new Period(importEnd, baseStart);
        final Minutes gapMinutes = gap.toStandardMinutes();
        if (gapMinutes.getMinutes() > 0 && baseMinutes.getMinutes() != gapMinutes.getMinutes())
            throw new CoreException(new Status(IStatus.ERROR, KalypsoUIRRMPlugin.getID(),
                    String.format(Messages.getString("MergeTimeseriesOperation.1"), baseMinutes.toString(), //$NON-NLS-1$
                            gapMinutes.toString())));
    }

    /* Here the intervals touch or overlap. */
}