StringBuffer cleanup 63/57063/4
authorStephen Kitt <skitt@redhat.com>
Mon, 15 May 2017 13:15:08 +0000 (15:15 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 17 May 2017 09:07:44 +0000 (09:07 +0000)
This patch cleans up StringBuffer use, mostly replacing StringBuffer
with StringBuilder or plain String concatenation for short sequences
(or constants).

* HexEncode: additionally, avoid using a separate return variable,
  drop the useless ":" handling in bytesToHexString(), and add the
  test class from l2switch.
* HardcodedModuleFactoriesResolver: rework the conflicting module
  handling.

Change-Id: Id76e91bba9ce40bd8ed5947c2d40e3a7baf0a949
Signed-off-by: Stephen Kitt <skitt@redhat.com>
opendaylight/commons/liblldp/src/main/java/org/opendaylight/controller/liblldp/HexEncode.java
opendaylight/commons/liblldp/src/test/java/org/opendaylight/controller/liblldp/HexEncodeTest.java [new file with mode: 0644]
opendaylight/config/config-api/src/main/java/org/opendaylight/controller/config/api/jmx/notifications/CommitJMXNotification.java
opendaylight/config/config-manager-facade-xml/src/main/java/org/opendaylight/controller/config/facade/xml/mapping/attributes/mapping/ObjectNameAttributeMappingStrategy.java
opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/factoriesresolver/HardcodedModuleFactoriesResolver.java
opendaylight/config/config-persister-directory-xml-adapter/src/main/java/org/opendaylight/controller/config/persist/storage/directory/xml/XmlDirectoryPersister.java
opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorTest.java
opendaylight/md-sal/messagebus-util/src/main/java/org/opendaylight/controller/messagebus/app/util/Util.java

index cef1745..bb18149 100644 (file)
@@ -30,12 +30,8 @@ public class HexEncode {
             return "null";
         }
 
-        String ret = "";
-        StringBuffer buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
         for (int i = 0; i < bytes.length; i++) {
-            if (i > 0) {
-                ret += ":";
-            }
             short u8byte = (short) (bytes[i] & 0xff);
             String tmp = Integer.toHexString(u8byte);
             if (tmp.length() == 1) {
@@ -43,13 +39,12 @@ public class HexEncode {
             }
             buf.append(tmp);
         }
-        ret = buf.toString();
-        return ret;
+        return buf.toString();
     }
 
     public static String longToHexString(final long val) {
         char arr[] = Long.toHexString(val).toCharArray();
-        StringBuffer buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
         // prepend the right number of leading zeros
         int i = 0;
         for (; i < (16 - arr.length); i++) {
@@ -94,8 +89,7 @@ public class HexEncode {
         if (bytes == null) {
             return "null";
         }
-        String ret = "";
-        StringBuffer buf = new StringBuffer();
+        StringBuilder buf = new StringBuilder();
         for (int i = 0; i < bytes.length; i++) {
             if (i > 0) {
                 buf.append(":");
@@ -107,7 +101,6 @@ public class HexEncode {
             }
             buf.append(tmp);
         }
-        ret = buf.toString();
-        return ret;
+        return buf.toString();
     }
 }
diff --git a/opendaylight/commons/liblldp/src/test/java/org/opendaylight/controller/liblldp/HexEncodeTest.java b/opendaylight/commons/liblldp/src/test/java/org/opendaylight/controller/liblldp/HexEncodeTest.java
new file mode 100644 (file)
index 0000000..18aa300
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.liblldp;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class HexEncodeTest {
+    @Test
+    public void testbytesToHexString() {
+        byte[] bytes1 = { (byte) 0x01, (byte) 0x02, (byte) 0x03 };
+        String str1 = HexEncode.bytesToHexString(bytes1);
+        Assert.assertTrue(str1.equals("010203"));
+
+        byte[] bytes2 = { (byte) 0x11, (byte) 0x22, (byte) 0x33 };
+        String str2 = HexEncode.bytesToHexString(bytes2);
+        Assert.assertFalse(str2.equals("010203"));
+
+    }
+
+    @Test
+    public void testLongToHexString() {
+        long value1 = 12345678L;
+        String str1 = HexEncode.longToHexString(value1);
+        Assert.assertTrue(str1.equals("00:00:00:00:00:bc:61:4e"));
+
+        long value2 = 98765432L;
+        String str2 = HexEncode.longToHexString(value2);
+        Assert.assertFalse(str2.equals("00:44:33:22:11:bc:61:4e"));
+
+    }
+
+    @Test
+    public void testBytesFromHexString() {
+        String byteStr1 = "00:11:22:33:44:55";
+        byte byteArray1[] = HexEncode.bytesFromHexString(byteStr1);
+
+        Assert.assertTrue(byteArray1[0] == (byte) 0x0);
+        Assert.assertTrue(byteArray1[1] == (byte) 0x11);
+        Assert.assertTrue(byteArray1[2] == (byte) 0x22);
+        Assert.assertTrue(byteArray1[3] == (byte) 0x33);
+        Assert.assertTrue(byteArray1[4] == (byte) 0x44);
+        Assert.assertTrue(byteArray1[5] == (byte) 0x55);
+
+        String byteStr2 = "00:11:22:33:44:55";
+        byte byteArray2[] = HexEncode.bytesFromHexString(byteStr2);
+
+        Assert.assertFalse(byteArray2[0] == (byte) 0x55);
+        Assert.assertFalse(byteArray2[1] == (byte) 0x44);
+        Assert.assertFalse(byteArray2[2] == (byte) 0x33);
+        Assert.assertFalse(byteArray2[3] == (byte) 0x22);
+        Assert.assertFalse(byteArray2[4] == (byte) 0x11);
+        Assert.assertFalse(byteArray2[5] == (byte) 0x0);
+
+    }
+}
index 1a49b88..ef53818 100644 (file)
@@ -20,9 +20,7 @@ public class CommitJMXNotification extends ConfigJMXNotification {
 
     @Override
     public String toString() {
-        final StringBuffer sb = new StringBuffer("CommitJMXNotification{");
-        sb.append('}');
-        return sb.toString();
+        return "CommitJMXNotification{}";
     }
 
     /**
index 3874c3a..b7e6932 100644 (file)
@@ -70,7 +70,7 @@ public class ObjectNameAttributeMappingStrategy extends
 
         @Override
         public String toString() {
-            final StringBuffer sb = new StringBuffer("MappedDependency{");
+            final StringBuilder sb = new StringBuilder("MappedDependency{");
             sb.append("namespace='").append(namespace).append('\'');
             sb.append(", serviceName='").append(serviceName).append('\'');
             sb.append(", refName='").append(refName).append('\'');
index b313049..969192c 100644 (file)
@@ -29,32 +29,20 @@ public class HardcodedModuleFactoriesResolver implements ModuleFactoriesResolver
     private Map<String, Map.Entry<ModuleFactory, BundleContext>> factories;
 
     public HardcodedModuleFactoriesResolver(final BundleContext bundleContext, final ModuleFactory... list) {
-        List<ModuleFactory> factoryList = Arrays.asList(list);
-        this.factories = new HashMap<>(factoryList.size());
+        this.factories = new HashMap<>(list.length);
         for (ModuleFactory moduleFactory : list) {
-            StringBuffer errors = new StringBuffer();
             String moduleName = moduleFactory.getImplementationName();
             if (moduleName == null || moduleName.isEmpty()) {
                 throw new IllegalStateException(
                         "Invalid implementation name for " + moduleFactory);
             }
-            String error = null;
             Map.Entry<ModuleFactory, BundleContext> conflicting = factories.get(moduleName);
-            if (conflicting != null) {
-                error = String
-                        .format("Module name is not unique. Found two conflicting factories with same name '%s': " +
-                                "\n\t%s\n\t%s\n", moduleName, conflicting.getKey(), moduleFactory);
-
-            }
-
-            if (error == null) {
-                factories.put(moduleName, new AbstractMap.SimpleEntry<>(moduleFactory,
-                        bundleContext));
+            if (conflicting == null) {
+                factories.put(moduleName, new AbstractMap.SimpleEntry<>(moduleFactory, bundleContext));
             } else {
-                errors.append(error);
-            }
-            if (errors.length() > 0) {
-                throw new IllegalArgumentException(errors.toString());
+                throw new IllegalArgumentException(String.format(
+                        "Module name is not unique. Found two conflicting factories with same name '%s':\n\t%s\n\t%s\n",
+                        moduleName, conflicting.getKey(), moduleFactory));
             }
         }
     }
index 10fb270..ae7954f 100644 (file)
@@ -155,9 +155,6 @@ public class XmlDirectoryPersister implements Persister {
 
     @Override
     public String toString() {
-        final StringBuffer sb = new StringBuffer("XmlDirectoryPersister{");
-        sb.append("storage=").append(storage);
-        sb.append('}');
-        return sb.toString();
+        return "XmlDirectoryPersister{storage=" + storage + "}";
     }
 }
\ No newline at end of file
index e87259b..61647c4 100644 (file)
@@ -430,7 +430,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
 
         @Override
         public String toString() {
-            final StringBuffer sb = new StringBuffer();
+            final StringBuilder sb = new StringBuilder();
             sb.append(type).append(' ');
             sb.append(name).append('(');
 
@@ -458,10 +458,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
 
         @Override
         public String toString() {
-            final StringBuffer sb = new StringBuffer();
-            sb.append(type).append(' ');
-            sb.append(name);
-            return sb.toString();
+            return type + " " + name;
         }
     }
 }
index 02247ff..399cfb4 100644 (file)
@@ -52,7 +52,7 @@ public final class Util {
      * @return
      */
     public static String wildcardToRegex(final String wildcard){
-        final StringBuffer s = new StringBuffer(wildcard.length());
+        final StringBuilder s = new StringBuilder(wildcard.length());
         s.append('^');
         for (final char c : wildcard.toCharArray()) {
             switch(c) {

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.