Use ImmutableLists in AbstractPrependAsPath 91/102191/3
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 20 Aug 2022 21:25:21 +0000 (23:25 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 20 Aug 2022 22:29:03 +0000 (00:29 +0200)
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 <robert.varga@pantheon.tech>
bgp/openconfig-rp-statement/src/main/java/org/opendaylight/protocol/bgp/openconfig/routing/policy/statement/actions/AbstractPrependAsPath.java

index 368b46e3635d5c862a9317e034a846f79e590ab9..f6e11fe5a1a362ef318da552a9c291ecabfd1d7e 100644 (file)
@@ -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<Segments> prependAS(final List<Segments> oldSegments, final AsNumber as) {
@@ -44,22 +41,23 @@ abstract class AbstractPrependAsPath {
         final Segments firstSegment = it.next();
         final List<AsNumber> firstAsSequence = firstSegment.getAsSequence();
 
-        final List<Segments> newSegments;
+        final ImmutableList.Builder<Segments> newSegments;
         if (firstAsSequence != null && firstAsSequence.size() < Values.UNSIGNED_BYTE_MAX_VALUE) {
-            final ArrayList<AsNumber> 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.<Segments>builderWithExpectedSize(oldSegments.size())
+                .add(new SegmentsBuilder()
+                    .setAsSequence(ImmutableList.<AsNumber>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.<Segments>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) {