List of usage examples for org.joda.time Duration isEqual
public boolean isEqual(ReadableDuration duration)
From source file:ch.oakmountain.tpa.solver.SolutionCandidate.java
License:Apache License
/** * Verifies that summed distances and direct distance are the same, assuming a path never takes more than whole week, . * * @param path/* www . j a va2s . co m*/ */ public static void sanityCheckPath(List<TrainPathSlot> path, SimpleTrainPathApplication simpleTrainPathApplication) { if (path.size() == 0) { throw new IllegalStateException( "Path should have size > 0 for request " + simpleTrainPathApplication.getName()); } else if (!path.get(0).getFrom().equals(simpleTrainPathApplication.getFrom()) || !path.get(path.size() - 1).getTo().equals(simpleTrainPathApplication.getTo())) { throw new IllegalStateException( "Path does not start or end at requested locations in " + simpleTrainPathApplication.getName()); } Duration summedDistance = new Duration(0); for (TrainPathSlot trainPathSlot : path) { summedDistance = summedDistance .plus(trainPathSlot.getEndTime().distanceAfter(trainPathSlot.getStartTime())); } for (int i = 1; i < path.size(); i++) { TrainPathSlot previous = path.get(i - 1); TrainPathSlot current = path.get(i); summedDistance = summedDistance.plus(current.getStartTime().distanceAfter(previous.getEndTime())); if (!previous.getTo().equals(current.getFrom())) { throw new IllegalStateException("Path is not consistent" + simpleTrainPathApplication.getName()); } } Duration directDistance = path.get(path.size() - 1).getEndTime().distanceAfter(path.get(0).getStartTime()); if (!directDistance.isEqual(summedDistance)) { throw new IllegalStateException("Direct and summed distance should be the same in path for request" + simpleTrainPathApplication.getName()); } }
From source file:com.buabook.exacttarget.fuelsdk.AutoRefreshETClient.java
License:GNU General Public License
private void initialiseRefreshTracking(Duration refreshTokenLife) throws IllegalArgumentException { if (refreshTokenLife == null || refreshTokenLife.isEqual(Duration.ZERO)) throw new IllegalArgumentException("Invalid refresh token life duration"); this.refreshTokenLife = refreshTokenLife; this.lastRefreshTime = DateTime.now(); log.info("ExactTarget client with auto-refresh of refresh token created [ Refresh Duration: " + refreshTokenLife + " ]"); }
From source file:google.registry.model.CacheUtils.java
License:Open Source License
/** * Memoize a supplier, with a short expiration specified in the environment config. * * <p>Use this for things that might change while code is running. (For example, the various * lists downloaded from the TMCH get updated in datastore and the caches need to be refreshed.) *//*w w w. java2s. c om*/ public static <T> Supplier<T> memoizeWithShortExpiration(Supplier<T> original) { Duration expiration = getSingletonCacheRefreshDuration(); return expiration.isEqual(ZERO) ? original : memoizeWithExpiration(original, expiration.getMillis(), MILLISECONDS); }
From source file:google.registry.util.CacheUtils.java
License:Open Source License
/** Memoize a supplier, with a given expiration. */ private static <T> Supplier<T> memoizeForDuration(Supplier<T> original, Duration expiration) { return expiration.isEqual(ZERO) ? original // memoizeWithExpiration won't accept 0 as a refresh duration. : memoizeWithExpiration(original, expiration.getMillis(), MILLISECONDS); }
From source file:io.druid.indexing.coordinator.scaling.SimpleResourceManagementStrategy.java
License:Open Source License
private boolean hasTaskPendingBeyondThreshold(Collection<RemoteTaskRunnerWorkItem> pendingTasks) { long now = System.currentTimeMillis(); for (TaskRunnerWorkItem pendingTask : pendingTasks) { final Duration durationSinceInsertion = new Duration(pendingTask.getQueueInsertionTime().getMillis(), now);//w ww . java2s. c o m final Duration timeoutDuration = config.getPendingTaskTimeout().toStandardDuration(); if (durationSinceInsertion.isEqual(timeoutDuration) || durationSinceInsertion.isLongerThan(timeoutDuration)) { return true; } } return false; }
From source file:io.druid.indexing.overlord.autoscaling.SimpleResourceManagementStrategy.java
License:Apache License
private boolean hasTaskPendingBeyondThreshold(Collection<RemoteTaskRunnerWorkItem> pendingTasks) { synchronized (lock) { long now = System.currentTimeMillis(); for (TaskRunnerWorkItem pendingTask : pendingTasks) { final Duration durationSinceInsertion = new Duration( pendingTask.getQueueInsertionTime().getMillis(), now); final Duration timeoutDuration = config.getPendingTaskTimeout().toStandardDuration(); if (durationSinceInsertion.isEqual(timeoutDuration) || durationSinceInsertion.isLongerThan(timeoutDuration)) { return true; }//from www .ja va 2 s . co m } return false; } }
From source file:io.druid.indexing.overlord.autoscaling.SimpleWorkerResourceManagementStrategy.java
License:Apache License
private boolean hasTaskPendingBeyondThreshold(Collection<? extends TaskRunnerWorkItem> pendingTasks) { synchronized (lock) { long now = System.currentTimeMillis(); for (TaskRunnerWorkItem pendingTask : pendingTasks) { final Duration durationSinceInsertion = new Duration( pendingTask.getQueueInsertionTime().getMillis(), now); final Duration timeoutDuration = config.getPendingTaskTimeout().toStandardDuration(); if (durationSinceInsertion.isEqual(timeoutDuration) || durationSinceInsertion.isLongerThan(timeoutDuration)) { return true; }//from w w w . j av a2 s .c o m } return false; } }
From source file:kr.debop4j.timeperiod.calendars.DateAdd.java
License:Apache License
/** * ? offset ? ?? ./*from ww w . ja v a2 s. c o m*/ * * @param start ? * @param offset * @param seekDir ? * @param seekBoundary ? * @return ? ?, */ protected Pair<DateTime, Duration> calculateEnd(DateTime start, Duration offset, SeekDirection seekDir, SeekBoundaryMode seekBoundary) { if (isTraceEnable) log.trace( "? ? ?? ... start=[{}], offset=[{}], seekDir=[{}], seekBoundary=[{}]", start, offset, seekDir, seekBoundary); shouldBe(offset.compareTo(Duration.ZERO) >= 0, "offset? 0 ??? . offset=[%d]", offset.getMillis()); Duration remaining = offset; DateTime end; // search periods ITimePeriodCollection searchPeriods = new TimePeriodCollection(this.includePeriods); if (searchPeriods.size() == 0) searchPeriods.add(TimeRange.Anytime); // available periods ITimePeriodCollection availablePeriods = new TimePeriodCollection(); if (excludePeriods.size() == 0) { availablePeriods.addAll(searchPeriods); } else { if (isTraceEnable) log.trace(" ? ."); TimeGapCalculator<TimeRange> gapCalculator = new TimeGapCalculator<>(); for (ITimePeriod p : searchPeriods) { if (excludePeriods.hasOverlapPeriods(p)) { if (isTraceEnable) log.trace(" ? ? ? "); for (ITimePeriod gap : gapCalculator.getGaps(excludePeriods, p)) availablePeriods.add(gap); } else { availablePeriods.add(p); } } } if (availablePeriods.size() == 0) { if (isTraceEnable) log.trace(" period ."); return Pair.create(null, remaining); } if (isTraceEnable) log.trace(" ? ? ? ..."); TimePeriodCombiner periodCombiner = new TimePeriodCombiner<TimeRange>(); availablePeriods = periodCombiner.combinePeriods(availablePeriods); if (isTraceEnable) log.trace(" ? ."); Pair<ITimePeriod, DateTime> result = (seekDir == SeekDirection.Forward) ? findNextPeriod(start, availablePeriods) : findPrevPeriod(start, availablePeriods); ITimePeriod startPeriod = result.getV1(); DateTime seekMoment = result.getV2(); // ? . if (startPeriod == null) { if (isTraceEnable) log.trace(" ? ."); return Pair.create(null, remaining); } // offset ? 0 ??, ? ? seekMoment . if (offset.isEqual(Duration.ZERO)) { if (isTraceEnable) log.trace("offset ? 0?, ? ? seekMoment ."); return Pair.create(seekMoment, remaining); } if (seekDir == SeekDirection.Forward) { for (int i = availablePeriods.indexOf(startPeriod); i < availablePeriods.size(); i++) { ITimePeriod gap = availablePeriods.get(i); Duration gapRemaining = new Duration(seekMoment, gap.getEnd()); if (isTraceEnable) log.trace("Seek forward. gap=[{}], gapRemaining=[{}], remaining=[{}], seekMoment=[{}]", gap, gapRemaining, remaining, seekMoment); boolean isTargetPeriod = (seekBoundary == SeekBoundaryMode.Fill) ? gapRemaining.compareTo(remaining) >= 0 : gapRemaining.compareTo(remaining) > 0; if (isTargetPeriod) { end = seekMoment.plus(remaining); remaining = null; return Pair.create(end, remaining); } remaining = remaining.minus(gapRemaining); if (i == availablePeriods.size() - 1) return Pair.create(null, remaining); seekMoment = availablePeriods.get(i + 1).getStart(); // next period } } else { for (int i = availablePeriods.indexOf(startPeriod); i >= 0; i--) { ITimePeriod gap = availablePeriods.get(i); Duration gapRemaining = new Duration(gap.getStart(), seekMoment); if (isTraceEnable) log.trace("Seek backward. gap=[{}], gapRemaining=[{}], remaining=[{}], seekMoment=[{}]", gap, gapRemaining, remaining, seekMoment); boolean isTargetPeriod = (seekBoundary == SeekBoundaryMode.Fill) ? gapRemaining.compareTo(remaining) >= 0 : gapRemaining.compareTo(remaining) > 0; if (isTargetPeriod) { end = seekMoment.minus(remaining); remaining = null; return Pair.create(end, remaining); } remaining = remaining.minus(gapRemaining); if (i == 0) return Pair.create(null, remaining); seekMoment = availablePeriods.get(i - 1).getEnd(); } } if (isTraceEnable) log.trace(" ?? ."); return Pair.create(null, remaining); }
From source file:kr.debop4j.timeperiod.TimeBlock.java
License:Apache License
@Override public void durationFromStart(Duration newDuration) { assertMutable();/* w w w . ja va2 s . co m*/ assertValidDuration(newDuration); if (newDuration.isEqual(TimeSpec.MaxDuration)) { this.duration = newDuration; this.end = TimeSpec.MaxPeriodTime; } else { if (end != null) assert this.end.plus(newDuration) .compareTo(TimeSpec.MaxPeriodTime) <= 0 : "duration ? ?."; this.duration = newDuration; this.end = this.start.plus(this.duration); } }
From source file:org.samcrow.ridgesurvey.TimerFragment.java
License:Open Source License
private void startTimer() { if (!mRunning) { showTime(Duration.ZERO);/*from w w w .j av a2 s. c o m*/ mStartStopButton.setImageResource(ICON_STOP); mRunning = true; mTimer = new Timer(); mNotificationManager.cancelAll(); mCurrentDuration = Duration.ZERO; mTimer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { final Duration newDuration = mCurrentDuration.plus(Duration.standardSeconds(1)); mCurrentDuration = newDuration; getActivity().runOnUiThread(new Runnable() { @Override public void run() { showTime(newDuration); } }); if (newDuration.isEqual(HALF_PERIOD)) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { notifyHalfPeriod(); } }); } if (newDuration.isEqual(COUNT_UP_PERIOD) || newDuration.isLongerThan(COUNT_UP_PERIOD)) { mTimer.cancel(); getActivity().runOnUiThread(new Runnable() { @Override public void run() { notifyStopped(); stopTimer(); } }); } } }, 1000, 1000); } }