Obtains the string representation of an iterator. - Java java.util

Java examples for java.util:Iterator

Description

Obtains the string representation of an iterator.

Demo Code


import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

public class Main{
    /**/*from www.  j  av  a2 s .c o m*/
     * Obtains the string representation of an iterator.
     * 
     * @param iterator the iterator with the elements to be converted
     * @return a string of the form <code><i1, ..., in></code>
     */
    public static String toString(final Iterator<?> iterator) {
        String result = "";

        int rowNum = 0;
        if (!iterator.hasNext()) {
            result = "EmptyIterator iterator";
        }
        while (iterator.hasNext()) {
            final Object o;
            String str = "";
            try {
                o = iterator.next();
            } catch (Exception e) {
                str = "Exception when performing iterator.next():"
                        + e.toString();
                break;
            }
            try {
                str = codebase.DebugUtil.toString(o);
            } catch (Exception e) {
                str = "Exception when performing o.toString():"
                        + e.toString();
                break;
            }

            final String nextElement = Integer.toString(rowNum) + ":" + str;
            if (result.length() == 0) {
                result = nextElement;
            } else {
                result = result + ", " + nextElement;
            }

            rowNum += 1;
        }
        return "<" + result + ">";
    }
    /**
     * Obtains a string that represents an object.
     * <p>
     * An <code>null</code> reference is translated <code>null</code>, a String object to
     * <code>'object'</code> and and object array to <code>[o1,...,on]</code>.
     * 
     * @param o Object to print as a string
     * @return the string representation of the object
     */
    public static String toString(final Object o) {
        String result = "";
        if (o == null) {
            result = "null";
        } else {
            if (o instanceof Object[]) {
                result = toString((Object[]) o);
            } else if (o instanceof byte[]) {
                result = ArrayUtil.toString((byte[]) o);
            } else if (o instanceof int[]) {
                result = ArrayUtil.toString((int[]) o);
            } else if (o instanceof long[]) {
                result = ArrayUtil.toString((long[]) o);
            } else if (o instanceof String) {
                result = "'" + o.toString() + "'";
            } else {
                result = o.toString();
            }
        }
        return result;

    }
    /**
     * Obtains the string representation of an array of objects.
     * 
     * @param objs the objects array to be converted
     * @return a string of the form <code>[o1, ..., on]</code>
     */
    public static String toString(final Object[] objs) {
        final StringBuffer result = new StringBuffer();
        for (int i = 0; i < objs.length; i++) {
            if (result.length() != 0) {
                result.append("," + toString(objs[i]));
            } else {
                result.append(toString(objs[i]));
            }
        }

        return "[" + result.toString() + "]";
    }
}

Related Tutorials