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

Method plus

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

Source from the content-addressed store, hash-verified

1300 * @see #with(long, IsoUnit) with(long, U)
1301 */
1302 public Duration<U> plus(
1303 long amount,
1304 U unit
1305 ) {
1306
1307 if (unit == null) {
1308 throw new NullPointerException("Missing chronological unit.");
1309 }
1310
1311 long originalAmount = amount;
1312 U originalUnit = unit;
1313 boolean negatedValue = false;
1314
1315 if (amount == 0) {
1316 return this;
1317 } else if (amount < 0) {
1318 amount = MathUtils.safeNegate(amount);
1319 negatedValue = true;
1320 }
1321
1322 // Millis und Micros ersetzen
1323 List<Item<U>> temp = new ArrayList<>(this.getTotalLength());
1324 Item<U> item = replaceFraction(amount, unit);
1325
1326 if (item != null) {
1327 amount = item.getAmount();
1328 unit = item.getUnit();
1329 }
1330
1331 if (this.isEmpty()) {
1332 temp.add((item == null) ? Item.of(amount, unit) : item);
1333 return new Duration<>(temp, negatedValue);
1334 }
1335
1336 // Items aktualisieren
1337 int index = this.getIndex(unit);
1338 boolean resultNegative = this.isNegative();
1339
1340 if (index < 0) { // Einheit nicht vorhanden
1341 if (this.isNegative() == negatedValue) {
1342 temp.add(Item.of(amount, unit));
1343 } else { // mixed signs possible => last try
1344 return this.plus(Duration.of(originalAmount, originalUnit));
1345 }
1346 } else {
1347 long sum =
1348 MathUtils.safeAdd(
1349 MathUtils.safeMultiply(
1350 temp.get(index).getAmount(),
1351 (this.isNegative() ? -1 : 1)
1352 ),
1353 MathUtils.safeMultiply(
1354 amount,
1355 (negatedValue ? -1 : 1)
1356 )
1357 );
1358
1359 if (sum == 0) {

Callers 7

withMethod · 0.95
fromMethod · 0.45
unionMethod · 0.45
composeMethod · 0.45
convertMethod · 0.45
betweenMethod · 0.45
compareMethod · 0.45

Calls 15

safeNegateMethod · 0.95
getTotalLengthMethod · 0.95
replaceFractionMethod · 0.95
isEmptyMethod · 0.95
ofMethod · 0.95
getIndexMethod · 0.95
isNegativeMethod · 0.95
ofMethod · 0.95
safeAddMethod · 0.95
safeMultiplyMethod · 0.95
countMethod · 0.95
mergeMethod · 0.95

Tested by

no test coverage detected