Committed by
Ray Milkey
Refactor: Reduce depth of indent
Change-Id: I8ee247c790585a260d28438479d6992e431e6e90
Showing
1 changed file
with
32 additions
and
43 deletions
... | @@ -243,15 +243,13 @@ public class ConsistentLinkResourceStore extends | ... | @@ -243,15 +243,13 @@ public class ConsistentLinkResourceStore extends |
243 | double freeBw = cap.bandwidth().toDouble(); | 243 | double freeBw = cap.bandwidth().toDouble(); |
244 | 244 | ||
245 | // enumerate current allocations, subtracting resources | 245 | // enumerate current allocations, subtracting resources |
246 | - for (LinkResourceAllocations alloc : allocations) { | 246 | + double allocatedBw = ImmutableList.copyOf(allocations).stream() |
247 | - Set<ResourceAllocation> types = alloc.getResourceAllocation(link); | 247 | + .flatMap(x -> x.getResourceAllocation(link).stream()) |
248 | - for (ResourceAllocation a : types) { | 248 | + .filter(x -> x instanceof BandwidthResourceAllocation) |
249 | - if (a instanceof BandwidthResourceAllocation) { | 249 | + .map(x -> (BandwidthResourceAllocation) x) |
250 | - BandwidthResourceAllocation bwA = (BandwidthResourceAllocation) a; | 250 | + .mapToDouble(x -> x.bandwidth().toDouble()) |
251 | - freeBw -= bwA.bandwidth().toDouble(); | 251 | + .sum(); |
252 | - } | 252 | + freeBw -= allocatedBw; |
253 | - } | ||
254 | - } | ||
255 | 253 | ||
256 | free.put(type, Sets.newHashSet( | 254 | free.put(type, Sets.newHashSet( |
257 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))))); | 255 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))))); |
... | @@ -262,22 +260,18 @@ public class ConsistentLinkResourceStore extends | ... | @@ -262,22 +260,18 @@ public class ConsistentLinkResourceStore extends |
262 | // nothing left | 260 | // nothing left |
263 | break; | 261 | break; |
264 | } | 262 | } |
265 | - Set<LambdaResourceAllocation> freeL = new HashSet<>(); | 263 | + Set<LambdaResourceAllocation> freeL = lmd.stream() |
266 | - for (ResourceAllocation r : lmd) { | 264 | + .filter(x -> x instanceof LambdaResourceAllocation) |
267 | - if (r instanceof LambdaResourceAllocation) { | 265 | + .map(x -> (LambdaResourceAllocation) x) |
268 | - freeL.add((LambdaResourceAllocation) r); | 266 | + .collect(Collectors.toSet()); |
269 | - } | ||
270 | - } | ||
271 | 267 | ||
272 | // enumerate current allocations, removing resources | 268 | // enumerate current allocations, removing resources |
273 | - for (LinkResourceAllocations alloc : allocations) { | 269 | + List<LambdaResourceAllocation> allocatedLambda = ImmutableList.copyOf(allocations).stream() |
274 | - Set<ResourceAllocation> types = alloc.getResourceAllocation(link); | 270 | + .flatMap(x -> x.getResourceAllocation(link).stream()) |
275 | - for (ResourceAllocation a : types) { | 271 | + .filter(x -> x instanceof LambdaResourceAllocation) |
276 | - if (a instanceof LambdaResourceAllocation) { | 272 | + .map(x -> (LambdaResourceAllocation) x) |
277 | - freeL.remove(a); | 273 | + .collect(Collectors.toList()); |
278 | - } | 274 | + freeL.removeAll(allocatedLambda); |
279 | - } | ||
280 | - } | ||
281 | 275 | ||
282 | free.put(type, freeL); | 276 | free.put(type, freeL); |
283 | break; | 277 | break; |
... | @@ -287,22 +281,18 @@ public class ConsistentLinkResourceStore extends | ... | @@ -287,22 +281,18 @@ public class ConsistentLinkResourceStore extends |
287 | // nothing left | 281 | // nothing left |
288 | break; | 282 | break; |
289 | } | 283 | } |
290 | - Set<MplsLabelResourceAllocation> freeLabel = new HashSet<>(); | 284 | + Set<MplsLabelResourceAllocation> freeLabel = mpls.stream() |
291 | - for (ResourceAllocation r : mpls) { | 285 | + .filter(x -> x instanceof MplsLabelResourceAllocation) |
292 | - if (r instanceof MplsLabelResourceAllocation) { | 286 | + .map(x -> (MplsLabelResourceAllocation) x) |
293 | - freeLabel.add((MplsLabelResourceAllocation) r); | 287 | + .collect(Collectors.toSet()); |
294 | - } | ||
295 | - } | ||
296 | 288 | ||
297 | // enumerate current allocations, removing resources | 289 | // enumerate current allocations, removing resources |
298 | - for (LinkResourceAllocations alloc : allocations) { | 290 | + List<MplsLabelResourceAllocation> allocatedLabel = ImmutableList.copyOf(allocations).stream() |
299 | - Set<ResourceAllocation> types = alloc.getResourceAllocation(link); | 291 | + .flatMap(x -> x.getResourceAllocation(link).stream()) |
300 | - for (ResourceAllocation a : types) { | 292 | + .filter(x -> x instanceof MplsLabelResourceAllocation) |
301 | - if (a instanceof MplsLabelResourceAllocation) { | 293 | + .map(x -> (MplsLabelResourceAllocation) x) |
302 | - freeLabel.remove(a); | 294 | + .collect(Collectors.toList()); |
303 | - } | 295 | + freeLabel.removeAll(allocatedLabel); |
304 | - } | ||
305 | - } | ||
306 | 296 | ||
307 | free.put(type, freeLabel); | 297 | free.put(type, freeLabel); |
308 | break; | 298 | break; |
... | @@ -496,19 +486,18 @@ public class ConsistentLinkResourceStore extends | ... | @@ -496,19 +486,18 @@ public class ConsistentLinkResourceStore extends |
496 | checkNotNull(link); | 486 | checkNotNull(link); |
497 | final LinkKey key = LinkKey.linkKey(link); | 487 | final LinkKey key = LinkKey.linkKey(link); |
498 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); | 488 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); |
499 | - List<LinkResourceAllocations> res = null; | ||
500 | 489 | ||
501 | - res = linkAllocs.get(key); | 490 | + List<LinkResourceAllocations> res = linkAllocs.get(key); |
502 | - if (res == null) { | 491 | + if (res != null) { |
503 | - res = linkAllocs.putIfAbsent(key, new ArrayList<>()); | 492 | + return res; |
493 | + } | ||
504 | 494 | ||
495 | + res = linkAllocs.putIfAbsent(key, new ArrayList<>()); | ||
505 | if (res == null) { | 496 | if (res == null) { |
506 | return Collections.emptyList(); | 497 | return Collections.emptyList(); |
507 | } else { | 498 | } else { |
508 | return res; | 499 | return res; |
509 | } | 500 | } |
510 | } | 501 | } |
511 | - return res; | ||
512 | - } | ||
513 | 502 | ||
514 | } | 503 | } | ... | ... |
-
Please register or login to post a comment