# toString() Generator: Content Listing

This topic discusses how toString() generator lists contents of arrays and how it limits number of items listed in `Collection`s and `Map`s. Used method depends not only on the member type, but also on selected JDK compatibility of the project.

## Listing contents of Arrays

- ### Without limiting number of elements

  For JDK1.5 and later, generated method uses `Arrays.toString()`. As this method is not available in JDK1.4, `Arrays.asList()` is used instead in this case, but only for non-primitive arrays. Primitive arrays are handled with a helper method, showed below.

- ### Limiting number of elements

  If the array enclose a non-primitive type, the same solution is used for all versions of JDK: `Arrays.asList()` with `List.subList()`. For example, for a member named anArray, generated code looks like this: `Arrays.asList(anArray).subList(0, Math.min(anArray.length, maxLen))`
  In case of primitive arrays, `Arrays.asList()` cannot be used so in JDK1.5 or lower a helper method is used instead. In JDK1.6 there's another solution: `Arrays.toString(Arrays.copyOf(anArray, Math.min(anArray.length, maxLen))`. Although copying an array is not an optimal solution, the copied part is usually rather small and efficiency is not affected. A good thing is that a helper method can be avoided.

- ### Helper method

  A helper method returns a string listing items of a given array, in the form of `[1, 2, 3]`. The method iterates over the array and uses `instanceof` to determine its enclosing type. To make the code shorter, it checks only for types that can actually be passed to it.

      private String arrayToString(Object array, int len, int maxLen) {
      	StringBuffer stringBuffer = new StringBuffer();
      	len = Math.min(len, maxLen);
      	stringBuffer.append("[");
      	for (int i = 0; i < len; i++) {
      		if (i > 0)
      			stringBuffer.append(", ");
      		if (array instanceof float[])
      			stringBuffer.append(((float[]) array)[i]);
      		if (array instanceof int[])
      			stringBuffer.append(((int[]) array)[i]);
      		if (array instanceof Object[])
      			stringBuffer.append(((Object[]) array)[i]);
      	}
      	stringBuffer.append("]");
      	return stringBuffer.toString();
      }

  NOTES:
  - This method is overwritten every time the generator runs.
  - If the number of items is not limited, the method doesn't take the `maxLen` argument and doesn't use `Math.min()`
  - StringBuffer or StringBuilder is used depending on selected JDK compatibility

## Listing limited contents of `List`s

The same solution is used for all JDK versions: `aList.subList(0, Math.min(aList.size(), maxLen))`

## Listing limited contents of `Collection`s (helper method)

A `Collection` cannot be turned into a `List` without copying its contents (assuming it isn't a `List` already), so a helper method is used to iterate over first `maxLen` elements and build a string out of them:

    private String toString(Collection collection, int maxLen) {
    	StringBuffer stringBuffer = new StringBuffer();
    	stringBuffer.append("[");
    	int i = 0;
    	for (Iterator iterator = collection.iterator(); iterator.hasNext() && i < maxLen; i++) {
    		if (i > 0)
    			stringBuffer.append(", ");
    		stringBuffer.append(iterator.next());
    	}
    	stringBuffer.append("]");
    	return stringBuffer.toString();
    }

NOTES:

- This method is not overwritten every time the generator runs, so it can be easily customized by hand.
- StringBuilder or StringBuffer is used depending on selected JDK compatibility

## Listing limited contents of `Map`s

In case of `Map`s, the same helper method is used as for `Collection`s only that `map.entrySet()` is passed to it.

## Summary

This table sums up what methods are used in different conditions:

| `java.util.List` | `java.util.Collection` | `java.util.Map` | Array of primitive type | Array of non-primitive type
jdk 1.4 | - | - | - | helper method `arrayToString(array, len)` | `Arrays.asList(array)`
jdk 1.4/1.5, limit elements | `member.subList()` | helper method `toSting(collection, maxLen)` | helper method `toString(collection, maxLen)` with `map.entrySet()` | helper method `arrayToString(array, len, maxLen)` | `Arrays.asList(array).subList()`
jdk 1.5 | - | - | - | `Arrays.toString()` | `Arrays.asList(array)`
jdk 1.6 | - | - | - | `Arrays.toString()` | `Arrays.toString()`
jdk 1.6, limit elements | `member.subList()` | helper method `toString(Collection)` | helper method `toString(Collection)` with `map.entrySet()` | `Arrays.toString(Arrays.copyOf(member, ...))` | `Arrays.asList(array).subList()`

## Additional notes

- If a helper method must be generated for at least one member, it is also used for other members when possible. For example, a `List` is usually handled with `subList()` method, but if there's another member of `Collection` (not `List`) type and toString(collection) is generated, `List`s are also passed to it. This way the code is shorter and more consistent.
- Where necessary, the code responsible for listing contents is surrounded with null-checking code, for example: `array != null ? Arrays.asList(array) : null`
- If maximum number of listed items is set to 0, the generator uses simple string literal (`"[]"`) instead of methods described above.

### Related references

Generate toString() dialog
toString() Generator: Format Templates
toString() Generator: Code Styles