(
long amount,
U unit
)
| 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) { |
no test coverage detected