litesoft
@ 938
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.util; import java.util.*; /** * An Iterator Helper class. Optimally, SUN/Java would have provided a * seamless mechanism to add behavior/functionality to a class while * preserving the syntactic cleanliness of the usage.<p> * <p/> * An example from this file would be:<p> * <pre> * IteratorHelper.toString( iterator , label ) ; * could have been replaced with: * iterator.toString( label ) ; * </pre><p> * * Instead, we are left to create this class (with its clumsy usage). * Each method's first parameter is the <i>Iterator</i> that would have * been automatically passed in as <i>this</i>. * Note: As these would all be <i>instance</i> methods, the first * parameter may NOT be <i>null</i>.<p> * * When the first parameter can be null, by convention, I place these * methods in a aaaUtil class.<p> * * See <a href="IteratorUtil.html">IteratorUtil</a><p> * * This class is non-instantiable.<p> * * @author George Smith * @version 1.0 7/28/01 */ public class IteratorHelper { private IteratorHelper() { } /** * Returns a debug/human friendly <i>label</i>ed String that represents * the <i>iterator</i>.<p> * * @param pIterator the Iterator to dump (!null). * @param pLabel the label for the Iterator (null OK).<p> * * @return A <i>label</i>ed String that represents <i>iterator</i>. */ public static String toString( Iterator pIterator, String pLabel ) throws NullPointerException { StringBuilder sbElements = new StringBuilder(); int elements = 0; if ( pIterator.hasNext() ) { sbElements.append( '\n' ); while ( pIterator.hasNext() ) { elements++; Object entry = pIterator.next(); sbElements.append( " [" ); sbElements.append( (entry == null) ? "?null?" : entry.toString() ); sbElements.append( "]\n" ); } } StringBuilder sb = new StringBuilder(); if ( pLabel != null ) { sb.append( pLabel ); sb.append( ": " ); } sb.append( elements ); sb.append( " elements" ); sb.append( sbElements.toString() ); return sb.toString(); } } |