Eliminate duplicate code in toString(). 36/5636/1
authorShigeru Yasuda <s-yasuda@da.jp.nec.com>
Fri, 14 Mar 2014 11:13:29 +0000 (20:13 +0900)
committerShigeru Yasuda <s-yasuda@da.jp.nec.com>
Fri, 14 Mar 2014 11:13:29 +0000 (20:13 +0900)
Change-Id: Ieb1093941c502406ac65ae2e10756df9b122e2e8
Signed-off-by: Shigeru Yasuda <s-yasuda@da.jp.nec.com>
manager/api/src/main/java/org/opendaylight/vtn/manager/VBridge.java
manager/api/src/main/java/org/opendaylight/vtn/manager/VBridgeConfig.java
manager/api/src/main/java/org/opendaylight/vtn/manager/VInterface.java
manager/api/src/main/java/org/opendaylight/vtn/manager/VInterfaceConfig.java
manager/api/src/main/java/org/opendaylight/vtn/manager/VTenant.java
manager/api/src/main/java/org/opendaylight/vtn/manager/VTenantConfig.java
manager/api/src/main/java/org/opendaylight/vtn/manager/VlanMap.java
manager/api/src/main/java/org/opendaylight/vtn/manager/VlanMapConfig.java

index bcca51336ba87c8254e4d61e7e7373c8300aa784..acab2bc8a194d92a842537c76e9b2c172514e194 100644 (file)
@@ -306,21 +306,18 @@ public class VBridge extends VBridgeConfig {
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder("VBridge[");
+        String prefix;
         if (name != null) {
-            builder.append("name=").append(name).append(',');
-        }
-
-        String desc = getDescription();
-        if (desc != null) {
-            builder.append("desc=").append(desc).append(',');
+            builder.append("name=").append(name);
+            prefix = ",";
+        } else {
+            prefix = "";
         }
 
-        int age = getAgeInterval();
-        if (age >= 0) {
-            builder.append("ageInterval=").append(age).append(',');
+        if (appendContents(builder, prefix)) {
+            prefix = ",";
         }
-
-        builder.append("faults=").append(faults).
+        builder.append(prefix).append("faults=").append(faults).
             append(",state=").append(state.toString()).append("]");
 
         return builder.toString();
index ecb665de4d0d9a9ec2d5c34c68d77feb68ed4d78..ba1ab3bcece37ffed41b87f4cbb3372091d7a8e5 100644 (file)
@@ -36,7 +36,7 @@ public class VBridgeConfig implements Serializable {
     /**
      * Version number for serialization.
      */
-    private static final long serialVersionUID = 7501956469021832807L;
+    private static final long serialVersionUID = 9009552129571959205L;
 
     /**
      * An arbitrary description of the vBridge.
@@ -193,6 +193,31 @@ public class VBridgeConfig implements Serializable {
         }
     }
 
+    /**
+     * Append human readable strings which represents the contents of this
+     * object to the specified {@link StringBuilder}.
+     *
+     * @param builder  A {@link StringBuilder} instance.
+     * @param prefix   A string to be inserted before contents.
+     *                 {@code null} must not be specified.
+     * @return  {@code true} if at least one character is appended to the
+     *          specified {@link StringBuilder} instance.
+     *          Otherwise {@code false}.
+     */
+    boolean appendContents(StringBuilder builder, String prefix) {
+        int len = builder.length();
+        String pfx = prefix;
+        if (description != null) {
+            builder.append(pfx).append("desc=").append(description);
+            pfx = ",";
+        }
+        if (ageInterval >= 0) {
+            builder.append(pfx).append("ageInterval=").append(ageInterval);
+        }
+
+        return (builder.length() != len);
+    }
+
     /**
      * Determine whether the given object is identical to this object.
      *
@@ -266,17 +291,8 @@ public class VBridgeConfig implements Serializable {
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder("VBridgeConfig[");
-        if (description != null) {
-            builder.append("desc=").append(description);
-        }
-        if (ageInterval >= 0) {
-            if (description != null) {
-                builder.append(',');
-            }
-            builder.append("ageInterval=").append(ageInterval);
-        }
+        appendContents(builder, "");
         builder.append(']');
-
         return builder.toString();
     }
 }
index e6039b278fa987857b3687e0b8be6455e4b5ef9c..30f761ccba23d661956039211f2b5d78c9cfead9 100644 (file)
@@ -369,25 +369,18 @@ public class VInterface extends VInterfaceConfig {
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder("VInterface[");
+        String prefix;
         if (name != null) {
-            builder.append("name=").append(name).append(',');
-        }
-
-        String desc = getDescription();
-        if (desc != null) {
-            builder.append("desc=").append(desc).append(',');
+            builder.append("name=").append(name);
+            prefix = ",";
+        } else {
+            prefix = "";
         }
 
-        Boolean en = getEnabled();
-        if (en != null) {
-            if (en.booleanValue()) {
-                builder.append("enabled,");
-            } else {
-                builder.append("disabled,");
-            }
+        if (appendContents(builder, prefix)) {
+            prefix = ",";
         }
-
-        builder.append("state=").append(state.toString()).
+        builder.append(prefix).append("state=").append(state.toString()).
             append(",entityState=").append(entityState.toString()).append(']');
 
         return builder.toString();
index 41ad97c289fb00eeec701f21a33d746311ce76dc..88bb63ebbf6d8f690aec5226df190fbfeabd03aa 100644 (file)
@@ -37,7 +37,7 @@ public class VInterfaceConfig implements Serializable {
     /**
      * Version number for serialization.
      */
-    private static final long serialVersionUID = -6152398562520322469L;
+    private static final long serialVersionUID = 4425846321318048040L;
 
     /**
      * An arbitrary description of the virtual interface.
@@ -129,6 +129,37 @@ public class VInterfaceConfig implements Serializable {
         return enabled;
     }
 
+    /**
+     * Append human readable strings which represents the contents of this
+     * object to the specified {@link StringBuilder}.
+     *
+     * @param builder  A {@link StringBuilder} instance.
+     * @param prefix   A string to be inserted before contents.
+     *                 {@code null} must not be specified.
+     * @return  {@code true} if at least one character is appended to the
+     *          specified {@link StringBuilder} instance.
+     *          Otherwise {@code false}.
+     */
+    boolean appendContents(StringBuilder builder, String prefix) {
+        int len = builder.length();
+        String pfx = prefix;
+        if (description != null) {
+            builder.append(pfx).append("desc=").append(description);
+            pfx = ",";
+        }
+
+        if (enabled != null) {
+            builder.append(pfx);
+            if (enabled.booleanValue()) {
+                builder.append("enabled");
+            } else {
+                builder.append("disabled");
+            }
+        }
+
+        return (builder.length() != len);
+    }
+
     /**
      * Determine whether the given object is identical to this object.
      *
@@ -206,23 +237,8 @@ public class VInterfaceConfig implements Serializable {
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder("VInterfaceConfig[");
-        if (description != null) {
-            builder.append("desc=").append(description);
-        }
-
-        if (enabled != null) {
-            if (description != null) {
-                builder.append(",");
-            }
-
-            if (enabled.booleanValue()) {
-                builder.append("enabled");
-            } else {
-                builder.append("disabled");
-            }
-        }
+        appendContents(builder, "");
         builder.append(']');
-
         return builder.toString();
     }
 }
index f63ab416fbe12d91a4f946e13c55f7c237633f03..557dd639225d4384ccfe5dcbf7944e2bbdfe9fee 100644 (file)
@@ -152,39 +152,15 @@ public class VTenant extends VTenantConfig {
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder("VTenant[");
-        char sep = 0;
+        String prefix;
         if (name != null) {
             builder.append("name=").append(name);
-            sep = ',';
+            prefix = ",";
+        } else {
+            prefix = "";
         }
 
-        String desc = getDescription();
-        if (desc != null) {
-            if (sep != 0) {
-                builder.append(sep);
-            }
-            builder.append("desc=").append(desc);
-            sep = ',';
-        }
-
-        int idle = getIdleTimeout();
-        if (idle >= 0) {
-            if (sep != 0) {
-                builder.append(sep);
-            }
-            builder.append("idleTimeout=").append(idle);
-            sep = ',';
-        }
-
-        int hard = getHardTimeout();
-        if (hard >= 0) {
-            if (sep != 0) {
-                builder.append(sep);
-            }
-            builder.append("hardTimeout=").append(hard);
-        }
-        builder.append(']');
-
+        appendContents(builder, prefix).append(']');
         return builder.toString();
     }
 }
index 196d18fd97925267a0c8d32eddb49764786a818d..d301aa41faaf58db000908c89e0dccf276bbe6ee 100644 (file)
@@ -42,7 +42,7 @@ public class VTenantConfig implements Serializable {
     /**
      * Version number for serialization.
      */
-    private static final long serialVersionUID = 7446090329841381143L;
+    private static final long serialVersionUID = -2906937162063076543L;
 
     /**
      * An arbitrary description of the VTN.
@@ -304,6 +304,34 @@ public class VTenantConfig implements Serializable {
         }
     }
 
+    /**
+     * Append human readable strings which represents the contents of this
+     * object to the specified {@link StringBuilder}.
+     *
+     * @param builder  A {@link StringBuilder} instance.
+     * @param prefix   A string to be inserted before contents.
+     *                 {@code null} must not be specified.
+     * @return  A {@link StringBuilder} instance specified by {@code builder}.
+     */
+    StringBuilder appendContents(StringBuilder builder, String prefix) {
+        String pfx = prefix;
+        if (description != null) {
+            builder.append(pfx).append("desc=").append(description);
+            pfx = ",";
+        }
+
+        if (idleTimeout >= 0) {
+            builder.append(pfx).append("idleTimeout=").append(idleTimeout);
+            pfx = ",";
+        }
+
+        if (hardTimeout >= 0) {
+            builder.append(pfx).append("hardTimeout=").append(hardTimeout);
+        }
+
+        return builder;
+    }
+
     /**
      * Determine whether the given object is identical to this object.
      *
@@ -379,28 +407,7 @@ public class VTenantConfig implements Serializable {
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder("VTenantConfig[");
-        char sep = 0;
-        if (description != null) {
-            builder.append("desc=").append(description);
-            sep = ',';
-        }
-
-        if (idleTimeout >= 0) {
-            if (sep != 0) {
-                builder.append(sep);
-            }
-            builder.append("idleTimeout=").append(idleTimeout);
-            sep = ',';
-        }
-
-        if (hardTimeout >= 0) {
-            if (sep != 0) {
-                builder.append(sep);
-            }
-            builder.append("hardTimeout=").append(hardTimeout);
-        }
-        builder.append(']');
-
+        appendContents(builder, "").append(']');
         return builder.toString();
     }
 }
index 73b31a698fca9fd8a336820f38c1cff100963f18..f70f3b6932d959f5f978de576c211fc49249d03a 100644 (file)
@@ -153,13 +153,7 @@ public class VlanMap extends VlanMapConfig {
             builder.append("id=").append(id).append(',');
         }
 
-        Node node = getNode();
-        if (node != null) {
-            builder.append("node=").append(node).append(',');
-        }
-
-        builder.append("vlan=").append((int)getVlan()).append(']');
-
+        appendContents(builder).append(']');
         return builder.toString();
     }
 }
index 6b0e971e6a252589ac334c6a519caff3e8ab8538..2f1f0678eb4299b9fea78b25e00d235f147e64eb 100644 (file)
@@ -39,7 +39,7 @@ public class VlanMapConfig implements Serializable {
     /**
      * Version number for serialization.
      */
-    private static final long serialVersionUID = -70616068870223019L;
+    private static final long serialVersionUID = 606475725018224880L;
 
     /**
      * {@link Node} information corresponding to the physical switch to be
@@ -157,6 +157,21 @@ public class VlanMapConfig implements Serializable {
                 node.equals(anotherNode));
     }
 
+    /**
+     * Append human readable strings which represents the contents of this
+     * object to the specified {@link StringBuilder}.
+     *
+     * @param builder  A {@link StringBuilder} instance.
+     * @return  A {@link StringBuilder} instance specified by {@code builder}.
+     */
+    StringBuilder appendContents(StringBuilder builder) {
+        if (node != null) {
+            builder.append("node=").append(node.toString()).append(",");
+        }
+
+        return builder.append("vlan=").append((int)vlan);
+    }
+
     /**
      * Determine whether the given object is identical to this object.
      *
@@ -224,12 +239,7 @@ public class VlanMapConfig implements Serializable {
     @Override
     public String toString() {
         StringBuilder builder = new StringBuilder("VlanMapConfig[");
-        if (node != null) {
-            builder.append("node=").append(node.toString()).append(",");
-        }
-
-        builder.append("vlan=").append((int)vlan).append(']');
-
+        appendContents(builder).append(']');
         return builder.toString();
     }
 }