MCPcopy Create free account
hub / github.com/MenoData/Time4J / of

Method of

base/src/main/java/net/time4j/Duration.java:393–428  ·  view source on GitHub ↗
(
        long amount,
        U unit
    )

Source from the content-addressed store, hash-verified

391 * @return new duration
392 */
393 public static <U extends IsoUnit> Duration<U> of(
394 long amount,
395 U unit
396 ) {
397
398 if (amount == 0) {
399 return ofZero();
400 }
401
402 U u = unit;
403 long value = amount;
404
405 if (amount < 0) {
406 value = MathUtils.safeNegate(amount);
407 }
408
409 if (unit instanceof ClockUnit) {
410 switch (unit.getSymbol()) {
411 case '3':
412 u = cast(ClockUnit.NANOS);
413 value = MathUtils.safeMultiply(value, MIO);
414 break;
415 case '6':
416 u = cast(ClockUnit.NANOS);
417 value = MathUtils.safeMultiply(value, 1000);
418 break;
419 default:
420 // no-op
421 }
422 }
423
424 List<Item<U>> items = new ArrayList<>(1);
425 items.add(Item.of(value, u));
426 return new Duration<>(items, (amount < 0));
427
428 }
429
430 /**
431 * <p>Konstructs a new positive duration for combined date- and time items

Calls 7

ofZeroMethod · 0.95
safeNegateMethod · 0.95
castMethod · 0.95
safeMultiplyMethod · 0.95
ofMethod · 0.95
getSymbolMethod · 0.65
addMethod · 0.45