Hello, can you answer me a simple question?
We have 2 looping lists: ArrayList<Object> list1 = new ArrayList<>(); ArrayList<Object> list2 = new ArrayList<>(); list2.add(list1); list1.add(list2); Class GridToStringBuilder represent them into string. Simple public static <T> String toString(Class<T> cls, T obj) method returns string "ArrayList [size=1]" for each list. *Question is* how should look the result of toString(Class<T> cls, T obj, String name, @Nullable Object val) method and other methods with additional values? "ArrayList [size=1, name=ArrayList[size=1]]" or something else? The trouble is in GridStringBuilder.a(Object obj) method - it tries to append collection's toString() method which invokes toString() on every element. |
Hi Dmitriy,
We should print all objects, because this is the whole purpose of GridToStringBuilder class - to print as much as possible in pretty format. For cyclic references, we should introduce smarter approach, ensuring that certain object is printed only once, I would use a kind of stack or IdentityHashMap to track already printed entries and then print a reference to them instead of the whole body. We use similar approach for binary objects, see BinaryObjectExImpl.toString(...) method. In this case (hash + body) is printed normally, but when a cycle is found, we print only (hash). This is not very suitable for GridToStringBuilder, as we definitely do not want output to be flooded with hashes, which are useless in common case. May be it makes sense to object's position in already printed string. Vladimir. On Wed, Feb 15, 2017 at 3:07 PM, Дмитрий Рябов <[hidden email]> wrote: > Hello, can you answer me a simple question? > > We have 2 looping lists: > > ArrayList<Object> list1 = new ArrayList<>(); > ArrayList<Object> list2 = new ArrayList<>(); > > list2.add(list1); > list1.add(list2); > > Class GridToStringBuilder represent them into string. > > Simple public static <T> String toString(Class<T> cls, T obj) method > returns string "ArrayList [size=1]" for each list. > > *Question is* how should look the result of toString(Class<T> cls, T obj, > String name, @Nullable Object val) method and other methods with additional > values? > "ArrayList [size=1, name=ArrayList[size=1]]" or something else? > > The trouble is in GridStringBuilder.a(Object obj) method - it tries to > append collection's toString() method which invokes toString() on every > element. > |
Hello, Vladimir. Do you mean something like "ArrayList [size=1,
name=ArrayList [size=1, ArrayList {position 0}]]"? 2017-02-16 12:32 GMT+03:00 Vladimir Ozerov <[hidden email]>: > Hi Dmitriy, > > We should print all objects, because this is the whole purpose of > GridToStringBuilder class - to print as much as possible in pretty format. > For cyclic references, we should introduce smarter approach, ensuring that > certain object is printed only once, I would use a kind of stack or > IdentityHashMap to track already printed entries and then print a reference > to them instead of the whole body. > > We use similar approach for binary objects, see > BinaryObjectExImpl.toString(...) > method. In this case (hash + body) is printed normally, but when a cycle is > found, we print only (hash). This is not very suitable for > GridToStringBuilder, as we definitely do not want output to be flooded with > hashes, which are useless in common case. May be it makes sense to object's > position in already printed string. > > Vladimir. > > > On Wed, Feb 15, 2017 at 3:07 PM, Дмитрий Рябов <[hidden email]> > wrote: > > > Hello, can you answer me a simple question? > > > > We have 2 looping lists: > > > > ArrayList<Object> list1 = new ArrayList<>(); > > ArrayList<Object> list2 = new ArrayList<>(); > > > > list2.add(list1); > > list1.add(list2); > > > > Class GridToStringBuilder represent them into string. > > > > Simple public static <T> String toString(Class<T> cls, T obj) method > > returns string "ArrayList [size=1]" for each list. > > > > *Question is* how should look the result of toString(Class<T> cls, T obj, > > String name, @Nullable Object val) method and other methods with > additional > > values? > > "ArrayList [size=1, name=ArrayList[size=1]]" or something else? > > > > The trouble is in GridStringBuilder.a(Object obj) method - it tries to > > append collection's toString() method which invokes toString() on every > > element. > > > |
Yes, my idea was around it - we need to stop recursion somehow. On the
other hand, position-based format is not very user-friendly, apparently. May be some other approaches will be more suitable, but I cannot think of anything else. On Thu, Feb 16, 2017 at 2:26 PM, Дмитрий Рябов <[hidden email]> wrote: > Hello, Vladimir. Do you mean something like "ArrayList [size=1, > name=ArrayList [size=1, ArrayList {position 0}]]"? > > 2017-02-16 12:32 GMT+03:00 Vladimir Ozerov <[hidden email]>: > > > Hi Dmitriy, > > > > We should print all objects, because this is the whole purpose of > > GridToStringBuilder class - to print as much as possible in pretty > format. > > For cyclic references, we should introduce smarter approach, ensuring > that > > certain object is printed only once, I would use a kind of stack or > > IdentityHashMap to track already printed entries and then print a > reference > > to them instead of the whole body. > > > > We use similar approach for binary objects, see > > BinaryObjectExImpl.toString(...) > > method. In this case (hash + body) is printed normally, but when a cycle > is > > found, we print only (hash). This is not very suitable for > > GridToStringBuilder, as we definitely do not want output to be flooded > with > > hashes, which are useless in common case. May be it makes sense to > object's > > position in already printed string. > > > > Vladimir. > > > > > > On Wed, Feb 15, 2017 at 3:07 PM, Дмитрий Рябов <[hidden email]> > > wrote: > > > > > Hello, can you answer me a simple question? > > > > > > We have 2 looping lists: > > > > > > ArrayList<Object> list1 = new ArrayList<>(); > > > ArrayList<Object> list2 = new ArrayList<>(); > > > > > > list2.add(list1); > > > list1.add(list2); > > > > > > Class GridToStringBuilder represent them into string. > > > > > > Simple public static <T> String toString(Class<T> cls, T obj) method > > > returns string "ArrayList [size=1]" for each list. > > > > > > *Question is* how should look the result of toString(Class<T> cls, T > obj, > > > String name, @Nullable Object val) method and other methods with > > additional > > > values? > > > "ArrayList [size=1, name=ArrayList[size=1]]" or something else? > > > > > > The trouble is in GridStringBuilder.a(Object obj) method - it tries to > > > append collection's toString() method which invokes toString() on every > > > element. > > > > > > |
Free forum by Nabble | Edit this page |