org.joda.time.ReadableDuration Java Examples

The following examples show how to use org.joda.time.ReadableDuration. 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: DateUtils.java    From joda-time-android with Apache License 2.0 6 votes vote down vote up
/**
 * Return given duration in a human-friendly format. For example, "4
 * minutes" or "1 second". Returns only largest meaningful unit of time,
 * from seconds up to hours.
 *
 * The longest duration it supports is hours.
 *
 * This method assumes that there are 60 minutes in an hour,
 * 60 seconds in a minute and 1000 milliseconds in a second.
 * All currently supplied chronologies use this definition.
 */
public static CharSequence formatDuration(Context context, ReadableDuration readableDuration) {
    Resources res = context.getResources();
    Duration duration = readableDuration.toDuration();

    final int hours = (int) duration.getStandardHours();
    if (hours != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_hours, hours, hours);
    }

    final int minutes = (int) duration.getStandardMinutes();
    if (minutes != 0) {
        return res.getQuantityString(R.plurals.joda_time_android_duration_minutes, minutes, minutes);
    }

    final int seconds = (int) duration.getStandardSeconds();
    return res.getQuantityString(R.plurals.joda_time_android_duration_seconds, seconds, seconds);
}
 
Example #2
Source File: WatchTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testTerminationConditionsAfterTimeSinceNewOutput() {
  Instant now = Instant.now();
  Watch.Growth.AfterTimeSinceNewOutput<Object> c = afterTimeSinceNewOutput(standardSeconds(5));
  KV<Instant, ReadableDuration> state = c.forNewInput(now, null);
  assertFalse(c.canStopPolling(now, state));
  assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state));
  assertFalse(c.canStopPolling(now.plus(standardSeconds(6)), state));

  state = c.onSeenNewOutput(now.plus(standardSeconds(3)), state);
  assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state));
  assertFalse(c.canStopPolling(now.plus(standardSeconds(6)), state));
  assertTrue(c.canStopPolling(now.plus(standardSeconds(9)), state));

  state = c.onSeenNewOutput(now.plus(standardSeconds(5)), state);
  assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state));
  assertFalse(c.canStopPolling(now.plus(standardSeconds(6)), state));
  assertFalse(c.canStopPolling(now.plus(standardSeconds(9)), state));
  assertTrue(c.canStopPolling(now.plus(standardSeconds(11)), state));
}
 
Example #3
Source File: TestConverterManager.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testGetDurationConverter() {
    DurationConverter c = ConverterManager.getInstance().getDurationConverter(new Long(0L));
    assertEquals(Long.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter(new Duration(123L));
    assertEquals(ReadableDuration.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter(new Interval(0L, 1000L));
    assertEquals(ReadableInterval.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter("");
    assertEquals(String.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter(null);
    assertEquals(null, c.getSupportedType());
    
    try {
        ConverterManager.getInstance().getDurationConverter(Boolean.TRUE);
        fail();
    } catch (IllegalArgumentException ex) {}
}
 
Example #4
Source File: TestConverterManager.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testGetPeriodConverter() {
    PeriodConverter c = ConverterManager.getInstance().getPeriodConverter(new Period(1, 2, 3, 4, 5, 6, 7, 8));
    assertEquals(ReadablePeriod.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(new Duration(123L));
    assertEquals(ReadableDuration.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(new Interval(0L, 1000L));
    assertEquals(ReadableInterval.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter("");
    assertEquals(String.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(null);
    assertEquals(null, c.getSupportedType());
    
    try {
        ConverterManager.getInstance().getPeriodConverter(Boolean.TRUE);
        fail();
    } catch (IllegalArgumentException ex) {}
}
 
Example #5
Source File: TestConverterManager.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testGetPeriodConverter() {
    PeriodConverter c = ConverterManager.getInstance().getPeriodConverter(new Period(1, 2, 3, 4, 5, 6, 7, 8));
    assertEquals(ReadablePeriod.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(new Duration(123L));
    assertEquals(ReadableDuration.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(new Interval(0L, 1000L));
    assertEquals(ReadableInterval.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter("");
    assertEquals(String.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getPeriodConverter(null);
    assertEquals(null, c.getSupportedType());
    
    try {
        ConverterManager.getInstance().getPeriodConverter(Boolean.TRUE);
        fail();
    } catch (IllegalArgumentException ex) {}
}
 
Example #6
Source File: TestConverterManager.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testGetDurationConverter() {
    DurationConverter c = ConverterManager.getInstance().getDurationConverter(new Long(0L));
    assertEquals(Long.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter(new Duration(123L));
    assertEquals(ReadableDuration.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter(new Interval(0L, 1000L));
    assertEquals(ReadableInterval.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter("");
    assertEquals(String.class, c.getSupportedType());
    
    c = ConverterManager.getInstance().getDurationConverter(null);
    assertEquals(null, c.getSupportedType());
    
    try {
        ConverterManager.getInstance().getDurationConverter(Boolean.TRUE);
        fail();
    } catch (IllegalArgumentException ex) {}
}
 
Example #7
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a period from the given duration and end point.
 *
 * @param duration  the duration of the interval, null means zero-length
 * @param endInstant  the interval end, null means now
 * @param type  which set of fields this period supports, null means standard
 */
protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) {
    super();
    type = checkPeriodType(type);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    iType = type;
    iValues = chrono.get(this, startMillis, endMillis);
}
 
Example #8
Source File: Watch.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public String toString(KV<Instant, ReadableDuration> state) {
  return "AfterTimeSinceNewOutput{"
      + "timeOfLastNewOutput="
      + state.getKey()
      + ", maxTimeSinceNewOutput="
      + state.getValue()
      + '}';
}
 
Example #9
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares this object with the specified object for equality based
 * on the millisecond length. All ReadableDuration instances are accepted.
 *
 * @param duration  a readable duration to check against
 * @return true if the length of the duration is equal
 */
public boolean equals(Object duration) {
    if (this == duration) {
        return true;
    }
    if (duration instanceof ReadableDuration == false) {
        return false;
    }
    ReadableDuration other = (ReadableDuration) duration;
    return (getMillis() == other.getMillis());
}
 
Example #10
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is the length of this duration longer than the duration passed in.
 *
 * @param duration  another duration to compare to, null means zero milliseconds
 * @return true if this duration is equal to than the duration passed in
 */
public boolean isLongerThan(ReadableDuration duration) {
    if (duration == null) {
        duration = Duration.ZERO;
    }
    return compareTo(duration) > 0;
}
 
Example #11
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is the length of this duration equal to the duration passed in.
 *
 * @param duration  another duration to compare to, null means zero milliseconds
 * @return true if this duration is equal to than the duration passed in
 */
public boolean isEqual(ReadableDuration duration) {
    if (duration == null) {
        duration = Duration.ZERO;
    }
    return compareTo(duration) == 0;
}
 
Example #12
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares this duration with the specified duration based on length.
 *
 * @param other  a duration to check against
 * @return negative value if this is less, 0 if equal, or positive value if greater
 * @throws NullPointerException if the object is null
 * @throws ClassCastException if the given object is not supported
 */
public int compareTo(ReadableDuration other) {
    long thisMillis = this.getMillis();
    long otherMillis = other.getMillis();
    
    // cannot do (thisMillis - otherMillis) as it can overflow
    if (thisMillis < otherMillis) {
        return -1;
    }
    if (thisMillis > otherMillis) {
        return 1;
    }
    return 0;
}
 
Example #13
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares this object with the specified object for equality based
 * on the millisecond length. All ReadableDuration instances are accepted.
 *
 * @param duration  a readable duration to check against
 * @return true if the length of the duration is equal
 */
public boolean equals(Object duration) {
    if (this == duration) {
        return true;
    }
    if (duration instanceof ReadableDuration == false) {
        return false;
    }
    ReadableDuration other = (ReadableDuration) duration;
    return (getMillis() == other.getMillis());
}
 
Example #14
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is the length of this duration equal to the duration passed in.
 *
 * @param duration  another duration to compare to, null means zero milliseconds
 * @return true if this duration is equal to than the duration passed in
 */
public boolean isEqual(ReadableDuration duration) {
    if (duration == null) {
        duration = Duration.ZERO;
    }
    return compareTo(duration) == 0;
}
 
Example #15
Source File: Watch.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public String toString(KV<Instant, ReadableDuration> state) {
  return "AfterTotalOf{"
      + "timeStarted="
      + state.getKey()
      + ", maxTimeSinceInput="
      + state.getValue()
      + '}';
}
 
Example #16
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares this duration with the specified duration based on length.
 *
 * @param other  a duration to check against
 * @return negative value if this is less, 0 if equal, or positive value if greater
 * @throws NullPointerException if the object is null
 * @throws ClassCastException if the given object is not supported
 */
public int compareTo(ReadableDuration other) {
    long thisMillis = this.getMillis();
    long otherMillis = other.getMillis();
    
    // cannot do (thisMillis - otherMillis) as it can overflow
    if (thisMillis < otherMillis) {
        return -1;
    }
    if (thisMillis > otherMillis) {
        return 1;
    }
    return 0;
}
 
Example #17
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is the length of this duration shorter than the duration passed in.
 *
 * @param duration  another duration to compare to, null means zero milliseconds
 * @return true if this duration is equal to than the duration passed in
 */
public boolean isShorterThan(ReadableDuration duration) {
    if (duration == null) {
        duration = Duration.ZERO;
    }
    return compareTo(duration) < 0;
}
 
Example #18
Source File: AbstractDuration.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Is the length of this duration longer than the duration passed in.
 *
 * @param duration  another duration to compare to, null means zero milliseconds
 * @return true if this duration is equal to than the duration passed in
 */
public boolean isLongerThan(ReadableDuration duration) {
    if (duration == null) {
        duration = Duration.ZERO;
    }
    return compareTo(duration) > 0;
}
 
Example #19
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a period from the given start point and duration.
 *
 * @param startInstant  the interval start, null means now
 * @param duration  the duration of the interval, null means zero-length
 * @param type  which set of fields this period supports, null means standard
 */
protected BasePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) {
    super();
    type = checkPeriodType(type);
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    long endMillis = FieldUtils.safeAdd(startMillis, durationMillis);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    iType = type;
    iValues = chrono.get(this, startMillis, endMillis);
}
 
Example #20
Source File: BaseInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an interval from a millisecond duration and an end instant.
 * 
 * @param duration  the duration of this interval, null means zero length
 * @param end  end of this interval, null means now
 * @throws IllegalArgumentException if the end is before the start
 * @throws ArithmeticException if the start instant exceeds the capacity of a long
 */
protected BaseInterval(ReadableDuration duration, ReadableInstant end) {
    super();
    iChronology = DateTimeUtils.getInstantChronology(end);
    iEndMillis = DateTimeUtils.getInstantMillis(end);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    iStartMillis = FieldUtils.safeAdd(iEndMillis, -durationMillis);
    checkInterval(iStartMillis, iEndMillis);
}
 
Example #21
Source File: BaseInterval.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs an interval from a start instant and a duration.
 * 
 * @param start  start of this interval, null means now
 * @param duration  the duration of this interval, null means zero length
 * @throws IllegalArgumentException if the end is before the start
 * @throws ArithmeticException if the end instant exceeds the capacity of a long
 */
protected BaseInterval(ReadableInstant start, ReadableDuration duration) {
    super();
    iChronology = DateTimeUtils.getInstantChronology(start);
    iStartMillis = DateTimeUtils.getInstantMillis(start);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    iEndMillis = FieldUtils.safeAdd(iStartMillis, durationMillis);
    checkInterval(iStartMillis, iEndMillis);
}
 
Example #22
Source File: BasePeriod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a period from the given duration and end point.
 *
 * @param duration  the duration of the interval, null means zero-length
 * @param endInstant  the interval end, null means now
 * @param type  which set of fields this period supports, null means standard
 */
protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) {
    super();
    type = checkPeriodType(type);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    iType = type;
    iValues = chrono.get(this, startMillis, endMillis);
}
 
Example #23
Source File: ReadableDurationRelay.java    From jfixture with MIT License 5 votes vote down vote up
@Override
public Object create(Object request, SpecimenContext context) {
    if (!request.equals(ReadableDuration.class))
        return new NoSpecimen();

    return context.resolve(Duration.class);
}
 
Example #24
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Creates a period from the given duration and end point.
 *
 * @param duration  the duration of the interval, null means zero-length
 * @param endInstant  the interval end, null means now
 * @param type  which set of fields this period supports, null means standard
 */
protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) {
    super();
    type = checkPeriodType(type);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    iType = type;
    iValues = chrono.get(this, startMillis, endMillis);
}
 
Example #25
Source File: DurationCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(ReadableDuration value, OutputStream outStream)
    throws CoderException, IOException {
  if (value == null) {
    throw new CoderException("cannot encode a null ReadableDuration");
  }
  LONG_CODER.encode(toLong(value), outStream);
}
 
Example #26
Source File: Time_22_BasePeriod_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Creates a period from the given start point and duration.
 *
 * @param startInstant  the interval start, null means now
 * @param duration  the duration of the interval, null means zero-length
 * @param type  which set of fields this period supports, null means standard
 */
protected BasePeriod(ReadableInstant startInstant, ReadableDuration duration, PeriodType type) {
    super();
    type = checkPeriodType(type);
    long startMillis = DateTimeUtils.getInstantMillis(startInstant);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    long endMillis = FieldUtils.safeAdd(startMillis, durationMillis);
    Chronology chrono = DateTimeUtils.getInstantChronology(startInstant);
    iType = type;
    iValues = chrono.get(this, startMillis, endMillis);
}
 
Example #27
Source File: Time_22_BasePeriod_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Creates a period from the given duration and end point.
 *
 * @param duration  the duration of the interval, null means zero-length
 * @param endInstant  the interval end, null means now
 * @param type  which set of fields this period supports, null means standard
 */
protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) {
    super();
    type = checkPeriodType(type);
    long durationMillis = DateTimeUtils.getDurationMillis(duration);
    long endMillis = DateTimeUtils.getInstantMillis(endInstant);
    long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis);
    Chronology chrono = DateTimeUtils.getInstantChronology(endInstant);
    iType = type;
    iValues = chrono.get(this, startMillis, endMillis);
}
 
Example #28
Source File: WatchTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationConditionsAfterTotalOf() {
  Instant now = Instant.now();
  Watch.Growth.AfterTotalOf<Object> c = Growth.afterTotalOf(standardSeconds(5));
  KV<Instant, ReadableDuration> state = c.forNewInput(now, null);
  assertFalse(c.canStopPolling(now, state));
  assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state));
  assertTrue(c.canStopPolling(now.plus(standardSeconds(6)), state));
}
 
Example #29
Source File: WatchTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationConditionsEitherOf() {
  Instant now = Instant.now();
  Watch.Growth.AfterTotalOf<Object> a = Growth.afterTotalOf(standardSeconds(5));
  Watch.Growth.AfterTotalOf<Object> b = Growth.afterTotalOf(standardSeconds(10));

  Watch.Growth.BinaryCombined<
          Object, KV<Instant, ReadableDuration>, KV<Instant, ReadableDuration>>
      c = eitherOf(a, b);
  KV<KV<Instant, ReadableDuration>, KV<Instant, ReadableDuration>> state =
      c.forNewInput(now, null);
  assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state));
  assertTrue(c.canStopPolling(now.plus(standardSeconds(7)), state));
  assertTrue(c.canStopPolling(now.plus(standardSeconds(12)), state));
}
 
Example #30
Source File: TimeUtil.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Converts a {@link ReadableDuration} into a Dataflow API duration string. */
public static String toCloudDuration(ReadableDuration duration) {
  // Note that since Joda objects use millisecond resolution, we always
  // produce either no fractional seconds or fractional seconds with
  // millisecond resolution.
  long millis = duration.getMillis();
  long seconds = millis / 1000;
  millis = millis % 1000;
  if (millis == 0) {
    return String.format("%ds", seconds);
  } else {
    return String.format("%d.%03ds", seconds, millis);
  }
}