Optimize generated toString()
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / CodeHelpers.java
index 068de8a91eee686a9e6eca242dc3d0edb35e291d..f8b529ae64acf4f7a98465e2759a6d1ceb17403f 100644 (file)
@@ -11,6 +11,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.VerifyException;
 import java.util.Arrays;
 import java.util.List;
@@ -64,6 +65,35 @@ public final class CodeHelpers {
         return value;
     }
 
+    /**
+     * Append a named value to a ToStringHelper. If the value is null, this method does nothing.
+     *
+     * @param helper Helper to append to
+     * @param name Name of the value
+     * @param value Value to append
+     * @throws NullPointerException if the name or helper is null
+     */
+    public static void appendValue(final @NonNull ToStringHelper helper, final @NonNull String name,
+            final @Nullable Object value) {
+        if (value != null) {
+            helper.add(name, value);
+        }
+    }
+
+    /**
+     * Append a named value to a ToStringHelper. If the value is null, this method does nothing.
+     *
+     * @param helper Helper to append to
+     * @param name Name of the value
+     * @param value Value to append
+     * @throws NullPointerException if the name or helper is null
+     */
+    public static void appendValue(final ToStringHelper helper, final String name, final byte[] value) {
+        if (value != null) {
+            helper.add(name, Arrays.toString(value));
+        }
+    }
+
     /**
      * Compile a list of pattern regular expressions and return them as an array. The list must hold at least two
      * expressions.