类org.joda.time.ReadableDuration源码实例Demo

下面列出了怎么用org.joda.time.ReadableDuration的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: astor   文件: TestConverterManager.java
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) {}
}
 
源代码2 项目: astor   文件: TestConverterManager.java
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) {}
}
 
源代码3 项目: beam   文件: WatchTest.java
@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));
}
 
源代码4 项目: astor   文件: TestConverterManager.java
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) {}
}
 
源代码5 项目: astor   文件: TestConverterManager.java
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) {}
}
 
源代码6 项目: joda-time-android   文件: DateUtils.java
/**
 * 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);
}
 
源代码7 项目: beam   文件: Watch.java
@Override
public String toString(KV<Instant, ReadableDuration> state) {
  return "AfterTotalOf{"
      + "timeStarted="
      + state.getKey()
      + ", maxTimeSinceInput="
      + state.getValue()
      + '}';
}
 
源代码8 项目: beam   文件: Watch.java
@Override
public String toString(KV<Instant, ReadableDuration> state) {
  return "AfterTimeSinceNewOutput{"
      + "timeOfLastNewOutput="
      + state.getKey()
      + ", maxTimeSinceNewOutput="
      + state.getValue()
      + '}';
}
 
源代码9 项目: beam   文件: DurationCoder.java
@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);
}
 
源代码10 项目: beam   文件: WatchTest.java
@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));
}
 
源代码11 项目: beam   文件: WatchTest.java
@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));
}
 
源代码12 项目: beam   文件: TimeUtil.java
/** 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);
  }
}
 
源代码13 项目: coming   文件: Time_22_BasePeriod_s.java
/**
 * 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);
}
 
源代码14 项目: coming   文件: Time_22_BasePeriod_t.java
/**
 * 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);
}
 
源代码15 项目: coming   文件: Time_22_BasePeriod_t.java
/**
 * 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);
}
 
源代码16 项目: jfixture   文件: ReadableDurationRelay.java
@Override
public Object create(Object request, SpecimenContext context) {
    if (!request.equals(ReadableDuration.class))
        return new NoSpecimen();

    return context.resolve(Duration.class);
}
 
源代码17 项目: astor   文件: BaseInterval.java
/**
 * 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);
}
 
源代码18 项目: astor   文件: BaseInterval.java
/**
 * 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);
}
 
源代码19 项目: astor   文件: BasePeriod.java
/**
 * 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);
}
 
源代码20 项目: astor   文件: BasePeriod.java
/**
 * 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);
}
 
源代码21 项目: astor   文件: AbstractDuration.java
/**
 * 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;
}
 
源代码22 项目: astor   文件: AbstractDuration.java
/**
 * 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;
}
 
源代码23 项目: astor   文件: AbstractDuration.java
/**
 * 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;
}
 
源代码24 项目: astor   文件: AbstractDuration.java
/**
 * 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;
}
 
源代码25 项目: astor   文件: AbstractDuration.java
/**
 * 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());
}
 
源代码26 项目: astor   文件: BasePeriod.java
/**
 * 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);
}
 
源代码27 项目: astor   文件: AbstractDuration.java
/**
 * 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;
}
 
源代码28 项目: astor   文件: AbstractDuration.java
/**
 * 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;
}
 
源代码29 项目: astor   文件: AbstractDuration.java
/**
 * 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;
}
 
源代码30 项目: astor   文件: AbstractDuration.java
/**
 * 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());
}
 
 类所在包
 类方法
 同包方法