public static String toString(TimeUnit unit) { return names[unit.ordinal()]; } }
/** * Returns the numerical multiplier to convert a value from {@code originalTimeUnit} to * {@code newTimeUnit} (i.e. for {@code TimeUnit.DAYS -> TimeUnit.MINUTES} would return * 24 * 60 = 1440). If the original and new units are the same, returns 1. If the new unit * is larger than the original (i.e. the result would be less than 1), throws an * {@link IllegalArgumentException}. * * @param originalTimeUnit the source time unit * @param newTimeUnit the destination time unit * @return the numerical multiplier between the units */ protected static long calculateMultiplier(TimeUnit originalTimeUnit, TimeUnit newTimeUnit) { if (originalTimeUnit == newTimeUnit) { return 1; } else if (originalTimeUnit.ordinal() < newTimeUnit.ordinal()) { throw new IllegalArgumentException("The original time unit '" + originalTimeUnit + "' must be larger than the new time unit '" + newTimeUnit + "'"); } else { int originalOrd = originalTimeUnit.ordinal(); int newOrd = newTimeUnit.ordinal(); List<Long> unitMultipliers = TIME_UNIT_MULTIPLIERS.subList(newOrd, originalOrd); return unitMultipliers.stream().reduce(1L, (a, b) -> (long) a * b); } }
default String elapsedDisplayString0(long startTime, long endTime) { long elapsedNanos = elapsedNanos0(startTime, endTime); StringBuilder sb = new StringBuilder(elapsedNanos < 0 ? "-" : ""); elapsedNanos = Math.abs(elapsedNanos); for (TimeUnit unit : TIMEUNITS_DESCENDING) { long converted = unit.convert(elapsedNanos, NANOSECONDS); if (converted > 0) { sb.append(converted).append(TIMEUNIT_DISPLAY_VALUES[unit.ordinal()]); elapsedNanos -= NANOSECONDS.convert(converted, unit); } } return sb.toString(); }
/** * Returns the next smallest {@link TimeUnit} (i.e. {@code TimeUnit.DAYS -> TimeUnit.HOURS}). * If the parameter is {@code null} or {@code TimeUnit.NANOSECONDS}, an * {@link IllegalArgumentException} is thrown because there is no valid smaller TimeUnit. * * @param originalUnit the TimeUnit * @return the next smaller TimeUnit */ protected static TimeUnit getSmallerTimeUnit(TimeUnit originalUnit) { if (originalUnit == null || TimeUnit.NANOSECONDS == originalUnit) { throw new IllegalArgumentException("Cannot determine a smaller time unit than '" + originalUnit + "'"); } else { return TimeUnit.values()[originalUnit.ordinal() - 1]; } }
break; current = TimeUnit.values()[current.ordinal() - 1];
@Test public void indexKeyForShouldReturnPdxInstanceEnumWhenObjectIsInstanceOfEnum() throws TypeMismatchException { Object keyEnum = TimeUnit.SECONDS; Object keyEnumResult = TypeUtils.indexKeyFor(keyEnum); assertThat(keyEnumResult).isNotNull(); assertThat(keyEnumResult).isInstanceOf(PdxInstanceEnum.class); assertThat(((PdxInstanceEnum) keyEnumResult).getName()).isEqualTo(TimeUnit.SECONDS.name()); assertThat(((PdxInstanceEnum) keyEnumResult).getOrdinal()) .isEqualTo(TimeUnit.SECONDS.ordinal()); assertThat(((PdxInstanceEnum) keyEnumResult).getClassName()) .isEqualTo(TimeUnit.SECONDS.getDeclaringClass().getName()); }
public final Duration apply(Duration lhs, Duration rhs) { if (lhs == null || rhs == null) { throw new IllegalArgumentException("Duration cannot be null"); } Duration specialDuration = handleSpecialCases(lhs, rhs); if (specialDuration != null) { return specialDuration; } final Duration newDuration; if (lhs.getTimeUnit().ordinal() > rhs.getTimeUnit().ordinal()) { long lhsConverted = rhs.getTimeUnit().convert(lhs.getValue(), lhs.getTimeUnit()); newDuration = new Duration(apply(lhsConverted, rhs.getValue()), rhs.getTimeUnit()); } else if (lhs.getTimeUnit().ordinal() < rhs.getTimeUnit().ordinal()) { long rhsConverted = lhs.getTimeUnit().convert(rhs.getValue(), rhs.getTimeUnit()); newDuration = new Duration(apply(lhs.getValue(), rhsConverted), lhs.getTimeUnit()); } else { // Same time unit newDuration = new Duration(apply(lhs.getValue(), rhs.getValue()), lhs.getTimeUnit()); } return newDuration; }
public Message getWithoutAck(int batchSize, Long timeout, TimeUnit unit) throws CanalClientException { waitClientRunning(); if (!running) { return null; } try { int size = (batchSize <= 0) ? 1000 : batchSize; long time = (timeout == null || timeout < 0) ? -1 : timeout; // -1代表不做timeout控制 if (unit == null) { unit = TimeUnit.MILLISECONDS; } writeWithHeader(Packet.newBuilder() .setType(PacketType.GET) .setBody(Get.newBuilder() .setAutoAck(false) .setDestination(clientIdentity.getDestination()) .setClientId(String.valueOf(clientIdentity.getClientId())) .setFetchSize(size) .setTimeout(time) .setUnit(unit.ordinal()) .build() .toByteString()) .build() .toByteArray()); return receiveMessages(); } catch (IOException e) { throw new CanalClientException(e); } }
public final Duration apply(Duration lhs, Duration rhs) { if (lhs == null || rhs == null) { throw new IllegalArgumentException("Duration cannot be null"); } else if (lhs == SAME_AS_POLL_INTERVAL || rhs == SAME_AS_POLL_INTERVAL) { throw new IllegalStateException("Cannot perform operation on this kind of duration (SAME_AS_POLL_INTERVAL). Please don't use this Duration since it's deprecated"); } Duration specialDuration = handleSpecialCases(lhs, rhs); if (specialDuration != null) { return specialDuration; } final Duration newDuration; if (lhs.getTimeUnit().ordinal() > rhs.getTimeUnit().ordinal()) { long lhsConverted = rhs.getTimeUnit().convert(lhs.getValue(), lhs.getTimeUnit()); newDuration = new Duration(apply(lhsConverted, rhs.getValue()), rhs.getTimeUnit()); } else if (lhs.getTimeUnit().ordinal() < rhs.getTimeUnit().ordinal()) { long rhsConverted = lhs.getTimeUnit().convert(rhs.getValue(), rhs.getTimeUnit()); newDuration = new Duration(apply(lhs.getValue(), rhsConverted), lhs.getTimeUnit()); } else { // Same time unit newDuration = new Duration(apply(lhs.getValue(), rhs.getValue()), lhs.getTimeUnit()); } return newDuration; }
public final Duration apply(Duration lhs, Duration rhs) { if (lhs == null || rhs == null) { throw new IllegalArgumentException("Duration cannot be null"); } Duration specialDuration = handleSpecialCases(lhs, rhs); if (specialDuration != null) { return specialDuration; } final Duration newDuration; if (lhs.getTimeUnit().ordinal() > rhs.getTimeUnit().ordinal()) { long lhsConverted = rhs.getTimeUnit().convert(lhs.getValue(), lhs.getTimeUnit()); newDuration = new Duration(apply(lhsConverted, rhs.getValue()), rhs.getTimeUnit()); } else if (lhs.getTimeUnit().ordinal() < rhs.getTimeUnit().ordinal()) { long rhsConverted = lhs.getTimeUnit().convert(rhs.getValue(), rhs.getTimeUnit()); newDuration = new Duration(apply(lhs.getValue(), rhsConverted), lhs.getTimeUnit()); } else { // Same time unit newDuration = new Duration(apply(lhs.getValue(), rhs.getValue()), lhs.getTimeUnit()); } return newDuration; }
public static String toString(TimeUnit unit) { return names[unit.ordinal()]; } }
@Override public void formatTo(Formatter formatter, int flags, int width, int precision) { long nanos = duration.toNanos(); TimeUnit unit = chooseTimeUnit(nanos); double value = (double) nanos / NANOSECONDS.convert(1, unit); String format = computeFormat(flags, width, precision, TIME_UNIT[unit.ordinal()]); formatter.format(format, value); }
private TimeMeasure(@Nonnegative long value, TimeUnit unit) { checkNotNull(unit, "TimeUnit must be provided"); checkArgument(unit.ordinal() >= 2, "Unsupported precision %s", unit); checkArgument(value >= 0, "Unsupported negative values"); this.unit = unit; this.value = value; }
default String elapsedDisplayString0(long startTime, long endTime) { long elapsedNanos = elapsedNanos0(startTime, endTime); StringBuilder sb = new StringBuilder(elapsedNanos < 0 ? "-" : ""); elapsedNanos = Math.abs(elapsedNanos); for (TimeUnit unit : TIMEUNITS_DESCENDING) { long converted = unit.convert(elapsedNanos, NANOSECONDS); if (converted > 0) { sb.append(converted).append(TIMEUNIT_DISPLAY_VALUES[unit.ordinal()]); elapsedNanos -= NANOSECONDS.convert(converted, unit); } } return sb.toString(); }
default String elapsedDisplayString0(long startTime, long endTime) { long elapsedNanos = elapsedNanos0(startTime, endTime); StringBuilder sb = new StringBuilder(elapsedNanos < 0 ? "-" : ""); elapsedNanos = Math.abs(elapsedNanos); for (TimeUnit unit : TIMEUNITS_DESCENDING) { long converted = unit.convert(elapsedNanos, NANOSECONDS); if (converted > 0) { sb.append(converted).append(TIMEUNIT_DISPLAY_VALUES[unit.ordinal()]); elapsedNanos -= NANOSECONDS.convert(converted, unit); } } return sb.toString(); }
private void writeDuration(ObjectDataOutput out, Duration duration) throws IOException { if (duration != null) { out.writeLong(duration.getDurationAmount()); out.writeInt(duration.getTimeUnit().ordinal()); } }
private void writeDuration(ObjectDataOutput out, Duration duration) throws IOException { if (duration != null) { out.writeLong(duration.getDurationAmount()); out.writeInt(duration.getTimeUnit().ordinal()); } }