From 4a9191b114ba0e95b4131d55ac7ccabbe918cffe Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 20 Aug 2022 23:25:21 +0200 Subject: [PATCH] Use ImmutableLists in AbstractPrependAsPath ImmutableList.Builder allow for fluent building and does not allow the result to be modified. Use it instead of an an ArrayList. Change-Id: I3ca9b4c0b206ac9521efbe62c21f8df7f65cac06 Signed-off-by: Robert Varga --- .../actions/AbstractPrependAsPath.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/bgp/openconfig-rp-statement/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/actions/AbstractPrependAsPath.java b/bgp/openconfig-rp-statement/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/actions/AbstractPrependAsPath.java index 368b46e363..f6e11fe5a1 100644 --- a/bgp/openconfig-rp-statement/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/actions/AbstractPrependAsPath.java +++ b/bgp/openconfig-rp-statement/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/actions/AbstractPrependAsPath.java @@ -8,7 +8,6 @@ package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions; import com.google.common.collect.ImmutableList; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.opendaylight.protocol.util.Values; @@ -23,10 +22,8 @@ abstract class AbstractPrependAsPath { static final Attributes prependAS(final Attributes attributes, final AsNumber as) { return new AttributesBuilder(attributes) - .setAsPath(new AsPathBuilder() - .setSegments(prependAS(attributes.getAsPath().getSegments(), as)) - .build()) - .build(); + .setAsPath(new AsPathBuilder().setSegments(prependAS(attributes.getAsPath().getSegments(), as)).build()) + .build(); } private static List prependAS(final List oldSegments, final AsNumber as) { @@ -44,22 +41,23 @@ abstract class AbstractPrependAsPath { final Segments firstSegment = it.next(); final List firstAsSequence = firstSegment.getAsSequence(); - final List newSegments; + final ImmutableList.Builder newSegments; if (firstAsSequence != null && firstAsSequence.size() < Values.UNSIGNED_BYTE_MAX_VALUE) { - final ArrayList newAsSequence = new ArrayList<>(firstAsSequence.size() + 1); - newAsSequence.add(as); - newAsSequence.addAll(firstAsSequence); - - newSegments = new ArrayList<>(oldSegments.size()); - newSegments.add(new SegmentsBuilder().setAsSequence(newAsSequence).build()); + newSegments = ImmutableList.builderWithExpectedSize(oldSegments.size()) + .add(new SegmentsBuilder() + .setAsSequence(ImmutableList.builderWithExpectedSize(firstAsSequence.size() + 1) + .add(as) + .addAll(firstAsSequence) + .build()) + .build()); } else { - newSegments = new ArrayList<>(oldSegments.size() + 1); - newSegments.add(singleSequence(as)); - newSegments.add(firstSegment); + newSegments = ImmutableList.builderWithExpectedSize(oldSegments.size() + 1) + .add(singleSequence(as)) + .add(firstSegment); } it.forEachRemaining(newSegments::add); - return newSegments; + return newSegments.build(); } private static Segments singleSequence(final AsNumber as) { -- 2.36.6