From f9181800cd26f63dbbba5288149afeafec2fbfe6 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Fri, 15 Nov 2013 23:42:54 +0100 Subject: [PATCH] Refactored some code --- .../syndication/feed/impl/ToStringBean.java | 59 ++++++++++++------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/sun/syndication/feed/impl/ToStringBean.java b/src/main/java/com/sun/syndication/feed/impl/ToStringBean.java index 2d99cb1..b48193d 100644 --- a/src/main/java/com/sun/syndication/feed/impl/ToStringBean.java +++ b/src/main/java/com/sun/syndication/feed/impl/ToStringBean.java @@ -21,10 +21,10 @@ import java.io.Serializable; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.util.Collection; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.Stack; import org.slf4j.Logger; @@ -171,21 +171,33 @@ public class ToStringBean implements Serializable { } private void printProperty(final StringBuffer sb, final String prefix, final Object value) { + if (value == null) { + sb.append(prefix).append("=null\n"); + } else if (value.getClass().isArray()) { + printArrayProperty(sb, prefix, value); + } else if (value instanceof Map) { + @SuppressWarnings("unchecked") final Map map = (Map) value; - final Iterator> i = map.entrySet().iterator(); - if (i.hasNext()) { - while (i.hasNext()) { - final Map.Entry me = i.next(); - final String ePrefix = prefix + "[" + me.getKey() + "]"; - final Object eValue = me.getValue(); + final Set> entries = map.entrySet(); + + if (entries.isEmpty()) { + + sb.append(prefix).append("=[]\n"); + + } else { + + for (final Entry entry : entries) { + + final Object eKey = entry.getKey(); + final Object eValue = entry.getValue(); + final String ePrefix = String.format("%s[%s]", prefix, eKey); - // NEW final String[] tsInfo = new String[2]; tsInfo[0] = ePrefix; final Stack stack = PREFIX_TL.get(); @@ -202,21 +214,27 @@ public class ToStringBean implements Serializable { } else { sb.append(s); } + } - } else { - sb.append(prefix).append("=[]\n"); + } + } else if (value instanceof Collection) { + @SuppressWarnings("unchecked") final Collection collection = (Collection) value; - final Iterator i = collection.iterator(); - if (i.hasNext()) { - int c = 0; - while (i.hasNext()) { - final String cPrefix = prefix + "[" + c++ + "]"; - final Object cValue = i.next(); + if (collection.isEmpty()) { + + sb.append(prefix).append("=[]\n"); + + } else { + + int c = 0; + + for (final Object cValue : collection) { + + final String cPrefix = String.format("%s[%s]", prefix, c++); - // NEW final String[] tsInfo = new String[2]; tsInfo[0] = cPrefix; final Stack stack = PREFIX_TL.get(); @@ -234,10 +252,10 @@ public class ToStringBean implements Serializable { sb.append(s); } } - } else { - sb.append(prefix).append("=[]\n"); } + } else { + final String[] tsInfo = new String[2]; tsInfo[0] = prefix; final Stack stack = PREFIX_TL.get(); @@ -249,6 +267,7 @@ public class ToStringBean implements Serializable { } else { sb.append(s); } + } } @@ -256,7 +275,7 @@ public class ToStringBean implements Serializable { final int length = Array.getLength(array); for (int i = 0; i < length; i++) { final Object obj = Array.get(array, i); - printProperty(sb, prefix + "[" + i + "]", obj); + printProperty(sb, String.format("%s[%s]", prefix, i), obj); } }