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

Method merge

base/src/main/java/net/time4j/Duration.java:3197–3291  ·  view source on GitHub ↗
(
    	Duration<U> duration,
    	TimeSpan<? extends U> timespan
    )

Source from the content-addressed store, hash-verified

3195 }
3196
3197 private static <U extends IsoUnit> Duration<U> merge(
3198 Duration<U> duration,
3199 TimeSpan<? extends U> timespan
3200 ) {
3201
3202 if (duration.isEmpty()) {
3203 if (isEmpty(timespan)) {
3204 return duration;
3205 } else if (timespan instanceof Duration) {
3206 return cast(timespan);
3207 }
3208 }
3209
3210 Map<U, Long> map = new HashMap<>();
3211
3212 for (int i = 0, n = duration.count(); i < n; i++) {
3213 Item<U> item = duration.getTotalLength().get(i);
3214 map.put(
3215 item.getUnit(),
3216 Long.valueOf(
3217 MathUtils.safeMultiply(
3218 item.getAmount(),
3219 (duration.isNegative() ? -1 : 1)
3220 )
3221 )
3222 );
3223 }
3224
3225 boolean tsign = timespan.isNegative();
3226
3227 for (int i = 0, n = timespan.getTotalLength().size(); i < n; i++) {
3228 TimeSpan.Item<? extends U> e = timespan.getTotalLength().get(i);
3229 U unit = e.getUnit();
3230 long amount = e.getAmount();
3231
3232 // Millis und Micros ersetzen
3233 Item<U> item = replaceFraction(amount, unit);
3234
3235 if (item != null) {
3236 amount = item.getAmount();
3237 unit = item.getUnit();
3238 }
3239
3240 // Items aktualisieren
3241 if (map.containsKey(unit)) {
3242 map.put(
3243 unit,
3244 Long.valueOf(
3245 MathUtils.safeAdd(
3246 map.get(unit).longValue(),
3247 MathUtils.safeMultiply(amount, (tsign ? -1 : 1))
3248 )
3249 )
3250 );
3251 } else {
3252 map.put(
3253 unit,
3254 MathUtils.safeMultiply(amount, (tsign ? -1 : 1))

Callers 2

plusMethod · 0.95
unionMethod · 0.95

Calls 15

isEmptyMethod · 0.95
castMethod · 0.95
safeMultiplyMethod · 0.95
replaceFractionMethod · 0.95
safeAddMethod · 0.95
safeNegateMethod · 0.95
createMethod · 0.95
countMethod · 0.80
containsKeyMethod · 0.80
isEmptyMethod · 0.65
getMethod · 0.65
getTotalLengthMethod · 0.65

Tested by

no test coverage detected