Java 5 migration 88/68488/6
authorStephen Kitt <skitt@redhat.com>
Thu, 22 Feb 2018 10:42:18 +0000 (11:42 +0100)
committerSam Hague <shague@redhat.com>
Wed, 4 Apr 2018 15:53:24 +0000 (15:53 +0000)
* Use foreach loops instead of indexed for loops or iterating while
  loops.
* Use String::contains instead of String::indexOf.
* Use StringBuilder instead of StringBuffer, or a collector when the
  StringBuffer is only used to join a string collection.

This patch also simplifies the return value handling in
FibHelper::doesPrefixBelongToSubnet.

Change-Id: I13c63380e056cdeaa96090c16fee06316db782d9
Signed-off-by: Stephen Kitt <skitt@redhat.com>
bgpmanager/impl/src/main/java/org/opendaylight/netvirt/bgpmanager/BgpUtil.java
bgpmanager/impl/src/main/java/org/opendaylight/netvirt/bgpmanager/VtyshCli.java
elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/l2gw/ha/HwvtepHAUtil.java
fibmanager/api/src/main/java/org/opendaylight/netvirt/fibmanager/api/FibHelper.java
fibmanager/shell/src/main/java/org/opendaylight/netvirt/fibmanager/shell/ShowFibCommand.java
ipv6service/impl/src/main/java/org/opendaylight/netvirt/ipv6service/utils/Ipv6ServiceUtils.java
ipv6service/shell/src/main/java/org/opendaylight/netvirt/ipv6service/shell/ShowIpv6Command.java
neutronvpn/impl/src/main/java/org/opendaylight/netvirt/neutronvpn/NeutronBgpvpnChangeListener.java
neutronvpn/impl/src/main/java/org/opendaylight/netvirt/neutronvpn/NeutronvpnUtils.java
statistics/impl/src/main/java/org/opendaylight/netvirt/statistics/CounterRetriever.java
vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/VpnSubnetRouteHandler.java

index d3c1bf583a5027ae8396415bed531d288623074a..cad127397b5339f8e0c3156b3468e67ee2f9fe9f 100755 (executable)
@@ -101,7 +101,7 @@ public class BgpUtil implements AutoCloseable {
     public static int getAFItranslatedfromPrefix(String argPrefix) {
         int retValue = af_afi.AFI_IP.getValue();//default afiValue is 1 (= ipv4)
         String prefixOnly;
-        if (argPrefix.indexOf("/") == -1) {
+        if (!argPrefix.contains("/")) {
             prefixOnly = argPrefix;
         } else {
             prefixOnly = argPrefix.substring(0, argPrefix.indexOf("/"));
index 27106315c7b33a20467687d2a8ff00bca2f9674b..8f468ee95a1248cc70bb959d2262543d6691d15d 100644 (file)
@@ -230,7 +230,7 @@ public class VtyshCli extends OsgiCommandSupport {
 
         String inputCmd = "show " + command;
         outToSocket.println(inputCmd);
-        StringBuffer output = new StringBuffer();
+        StringBuilder output = new StringBuilder();
         String errorMsg = "";
         while (true) {
             char[] opBuf = new char[100];
index 67f04fc508af4fc4113b86e51aa8757b7f65b76e..04165200809dc81ff3efc4666d16fa951e92c208 100644 (file)
@@ -19,6 +19,7 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -431,14 +432,7 @@ public final class HwvtepHAUtil {
 
         builder1.setKey(new ManagersKey(new Uri(MANAGER_KEY)));
         List<ManagerOtherConfigs> otherConfigses = new ArrayList<>();
-        StringBuffer stringBuffer = new StringBuffer();
-        for (NodeId nodeId : nodeIds) {
-            stringBuffer.append(nodeId.getValue());
-            stringBuffer.append(",");
-        }
-
-        String children = stringBuffer.substring(0, stringBuffer.toString().length() - 1);
-
+        String children = nodeIds.stream().map(NodeId::getValue).collect(Collectors.joining(","));
         otherConfigses.add(getOtherConfigBuilder(HA_CHILDREN, children).build());
         builder1.setManagerOtherConfigs(otherConfigses);
         List<Managers> managers = new ArrayList<>();
index 1f87f9a28096613d216ca462efc8ec8c030040f8..03cbe14e149284f928dbf6b2964a9d91e0a5224f 100644 (file)
@@ -219,9 +219,8 @@ public final class FibHelper {
      * @return true if the param subnet contained the prefixToTest false otherwise
      */
     public static boolean doesPrefixBelongToSubnet(String prefixToTest, String subnet, boolean exactMatch) {
-        boolean rep = false;
         if (prefixToTest == null || prefixToTest.length() < 7 || subnet == null || subnet.length() < 7) {
-            return rep;
+            return false;
         }
         if (isIpv4Prefix(prefixToTest) && isIpv4Prefix(subnet)
                 || isIpv6Prefix(prefixToTest) && isIpv6Prefix(subnet)) {
@@ -234,8 +233,8 @@ public final class FibHelper {
             String ipPref = getIpFromPrefix(prefixToTest);
             String ipSub = getIpFromPrefix(subnet);
             String maskSubString = subnet.substring(subnet.indexOf("/") + 1);
-            String maskPrefString = null;
-            if (prefixToTest.indexOf("/") > -1) {
+            String maskPrefString;
+            if (prefixToTest.contains("/")) {
                 maskPrefString = prefixToTest.substring(prefixToTest.indexOf("/") + 1);
             } else if (ipVersion == 4) {
                 maskPrefString = "32";
@@ -243,31 +242,25 @@ public final class FibHelper {
                 maskPrefString = "128";
             }
 
-            int maskPref = -1;
-            int maskSub = -1;
             try {
-                maskPref = Integer.parseInt(maskPrefString);
-                maskSub = Integer.parseInt(maskSubString);
+                int maskPref = Integer.parseInt(maskPrefString);
+                int maskSub = Integer.parseInt(maskSubString);
                 if (exactMatch && maskPref != maskSub) {
                  /*because the mask must be exactly the same between them, the return type is false. This behavior could
                   * be changed to ignored it in including a boolean options to force or not the same mask control*/
-                    rep = false;
-                    return rep;
+                    return false;
                 }
                 BigInteger maskSubBig = getMaskNetwork(ipVersion, maskSub);
                 byte[] byteIpSub = InetAddress.getByName(ipSub).getAddress();
                 byte[] byteIpPref = InetAddress.getByName(ipPref).getAddress();
                 BigInteger netFromIpSub = packBigInteger(byteIpSub).and(maskSubBig);
                 BigInteger netFromIpPref = packBigInteger(byteIpPref).and(maskSubBig);
-                if (netFromIpSub.compareTo(netFromIpPref) == 0) {
-                    rep = true;
-                }
+                return netFromIpSub.compareTo(netFromIpPref) == 0;
             } catch (NumberFormatException | UnknownHostException e) {
-                return rep;
+                return false;
             }
-            return rep;
         }
-        return rep;
+        return false;
     }
 
     /**This method converts a byte[] to a BigInteger value.
index 72479965bf78751c16250e11dcf14e6e536ddcf5..b30e08b17f01d11dc8ed6019646a438a9720fd0e 100644 (file)
@@ -73,7 +73,7 @@ public class ShowFibCommand extends OsgiCommandSupport {
         if (prefixOrSubnetOption != null && prefixOrSubnetOption.length() > 0) {
             prefixOrSubnet = prefixOrSubnetOption.replace("[", "");
             prefixOrSubnet = prefixOrSubnet.replace("]", "");
-            if (prefixOrSubnet.indexOf("/") < 0) {
+            if (!prefixOrSubnet.contains("/")) {
                 String maskFull = null;
                 try {
                     Inet4Address tempAdd = (Inet4Address) InetAddress.getByName(prefixOrSubnet);
index 291202da6dd7012e69a2ba577f3dd5e4aec1708a..fdcdf151eb49439cf884ad2bbe1ab18a5aa815af 100644 (file)
@@ -163,7 +163,7 @@ public class Ipv6ServiceUtils {
         if (bytes == null) {
             return "null";
         }
-        StringBuffer buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
         for (int i = 0; i < bytes.length; i++) {
             if (i > 0) {
                 buf.append(":");
@@ -302,7 +302,7 @@ public class Ipv6ServiceUtils {
            3. Use the prefix "FE80::/10" along with the above 64 bit Interface
               identifier to generate the IPv6 LLA. */
 
-        StringBuffer interfaceID = new StringBuffer();
+        StringBuilder interfaceID = new StringBuilder();
         short u8byte = (short) (octets[0] & 0xff);
         u8byte ^= 1 << 1;
         interfaceID.append(Integer.toHexString(0xFF & u8byte));
@@ -361,14 +361,13 @@ public class Ipv6ServiceUtils {
             return null;
         }
 
-        StringBuffer macAddress = new StringBuffer();
-        macAddress.append("33:33:");
-        macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[12]), 2, "0") + ":");
-        macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[13]), 2, "0") + ":");
-        macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[14]), 2, "0") + ":");
-        macAddress.append(StringUtils.leftPad(Integer.toHexString(0xFF & octets[15]), 2, "0"));
+        String macAddress = "33:33:"
+                + StringUtils.leftPad(Integer.toHexString(0xFF & octets[12]), 2, "0") + ":"
+                + StringUtils.leftPad(Integer.toHexString(0xFF & octets[13]), 2, "0") + ":"
+                + StringUtils.leftPad(Integer.toHexString(0xFF & octets[14]), 2, "0") + ":"
+                + StringUtils.leftPad(Integer.toHexString(0xFF & octets[15]), 2, "0");
 
-        return new MacAddress(macAddress.toString());
+        return new MacAddress(macAddress);
     }
 
     private static List<MatchInfo> getIcmpv6RSMatch(Long elanTag) {
index 1db6537fd5cfd7c9a5d6875730635708f9207b01..a266db189542417d39ad31d4aff1c3f00a0d91c6 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.netvirt.ipv6service.shell;
 
 import java.util.List;
+import java.util.stream.Collectors;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
@@ -33,12 +34,7 @@ public class ShowIpv6Command extends OsgiCommandSupport {
 
     private String getPortIpv6Addresses(IVirtualPort vport) {
         List<Ipv6Address> ipv6Addresses = vport.getIpv6Addresses();
-        StringBuffer str = new StringBuffer();
-        for (Ipv6Address ipaddr: ipv6Addresses) {
-            str.append(ipaddr.getValue());
-            str.append("  ");
-        }
-        return str.toString();
+        return ipv6Addresses.stream().map(Ipv6Address::getValue).collect(Collectors.joining("  "));
     }
 
     @Override
index 36aa0dd57970287ba03161305eb057dd4913b1ec..b3cedd3f61079ff59e7164472ced4eadc0e84e3d 100644 (file)
@@ -201,10 +201,8 @@ public class NeutronBgpvpnChangeListener extends AsyncDataTreeChangeListenerBase
             return;
         }
         int oldRdsCount = originalRds.size();
-        Iterator<String> originalRdsInter = originalRds.iterator();
 
-        while (originalRdsInter.hasNext()) {
-            String rd = originalRdsInter.next();
+        for (String rd : originalRds) {
             //If the existing rd is not present in the updateRds list, not allow to process the updateRDs.
             if (!updateRDs.contains(rd)) {
                 LOG.error("The existing RD {} not present in the updatedRDsList:{}", rd, updateRDs);
index ed37c0043135c24dbca1c296026a173e8bd33cde..4b9cdd77418d263a1a1ed7bcb49d17b05f9e03ed 100644 (file)
@@ -1179,7 +1179,7 @@ public class NeutronvpnUtils {
            3. Use the prefix "FE80::/10" along with the above 64 bit Interface
               identifier to generate the IPv6 LLA. */
 
-        StringBuffer interfaceID = new StringBuffer();
+        StringBuilder interfaceID = new StringBuilder();
         short u8byte = (short) (octets[0] & 0xff);
         u8byte ^= 1 << 1;
         interfaceID.append(Integer.toHexString(0xFF & u8byte));
@@ -1346,7 +1346,7 @@ public class NeutronvpnUtils {
      */
     public static IpVersionChoice getIpVersionFromString(String ipAddress) {
         IpVersionChoice ipchoice = IpVersionChoice.UNDEFINED;
-        if (ipAddress.indexOf("/") >= 0) {
+        if (ipAddress.contains("/")) {
             ipAddress = ipAddress.substring(0, ipAddress.indexOf("/"));
         }
         try {
index 8cc3b65f043e426e96d556b801bcff327f8083a4..ffb6ec4355207e1425ea7e67f4a7028dee650407 100644 (file)
@@ -105,8 +105,7 @@ public class CounterRetriever {
         try {
             CompletableFuture<Void> allOf = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[0]));
             allOf.get(nodeResultTimeout, TimeUnit.SECONDS);
-            for (int i = 0; i < futureList.size(); i++) {
-                CompletableFuture<NodeConnectorStatisticsSupplierOutput> completableCurrentResult = futureList.get(i);
+            for (CompletableFuture<NodeConnectorStatisticsSupplierOutput> completableCurrentResult : futureList) {
                 if (completableCurrentResult == null) {
                     LOG.warn("Unable to retrieve node counters");
                     counters.failedGettingNodeCounters.inc();
index b95cec2ccff4600d815c4f483e42b4bc4c6106d7..1c23ec15b9177082a9aeb539453f0ec348fc2d82 100644 (file)
@@ -14,7 +14,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -940,9 +939,7 @@ public class VpnSubnetRouteHandler {
 
         String nhTepIp = null;
         BigInteger nhDpnId = null;
-        Iterator<SubnetToDpn> subnetDpnIter = subDpnList.iterator();
-        while (subnetDpnIter.hasNext()) {
-            SubnetToDpn subnetToDpn = subnetDpnIter.next();
+        for (SubnetToDpn subnetToDpn : subDpnList) {
             if (subnetToDpn.getDpnId().equals(oldDpnId)) {
                 // Is this same is as input dpnId, then ignore it
                 continue;
@@ -960,7 +957,6 @@ public class VpnSubnetRouteHandler {
                 } catch (Exception e) {
                     LOG.warn("{} electNewDpnForSubnetRoute: Unable to find TepIp for rd {} subnetroute subnetip {}"
                             + " for dpnid {}, attempt next", LOGGING_PREFIX, rd, subnetIp, nhDpnId.toString(), e);
-                    continue;
                 }
             }
         }