Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Eclipse Java Development User Guide
Previous Page Home Next Page

toString() Generator: Content Listing

This topic discusses how toString() generator lists contents of arrays and how it limits number of items listed in Collections and Maps. 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 Lists

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

Listing limited contents of Collections (helper method)

A Collection cannot be turned into a List without copying its contents (assuming iy 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 Maps

In case of Maps, the same helper method is used as for Collections 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 toSting(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, Lists 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 reference

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


 
 
  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire