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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package Torello.Java;

import Torello.Java.Function.IntTFunction;
import Torello.Java.StringParse;
import Torello.Java.StrPrint;

class PrintListAbbrev
{
    static <ELEM> String print1(
            final Iterable<ELEM>                        list,
            final IntTFunction<? super ELEM, String>    listItemPrinter,
            final int                                   lineWidth,
            final int                                   indentation,
            final boolean                               seeEscapedNewLinesAsText,
            final boolean                               printNulls,
            final boolean                               showLineNumbers
        )
    {
        if ((lineWidth < 8) || (lineWidth > 200)) throw new IllegalArgumentException(
            "[" + lineWidth + "] was passed to parameter 'lineWidth', but this value must be " +
            "between 8 and 200 is expected"
        );

        if (indentation >= 200) throw new IllegalArgumentException(
            "[" + indentation + "] was passed to parameter 'indentation', but this value must " +
            "be 200 or less."
        );
    
        String indentationStr = (indentation > 0)
            ? ('\n' + StringParse.nChars(' ', indentation))
            : "\n";

        StringBuilder   sb      = new StringBuilder();
        int             counter = 0;

        for (ELEM elem : list)
        {
            counter++;
            if ((elem == null) && (! printNulls)) continue;

            sb.append(indentationStr);

            if (showLineNumbers)
            {
                if (counter < 10)   sb.append(StringParse.zeroPad10e2(counter) + ") ");
                else                sb.append(counter + ") ");
            }

            if ((elem == null))
                sb.append("null");
            else
                sb.append(
                    StringParse.trimLeft(
                        StrPrint.abbrevEndRDSF(
                            listItemPrinter.apply(counter, elem), lineWidth,
                            seeEscapedNewLinesAsText
                        )));
        }

        return sb.toString();
    }

    static <ELEM> String print2(
            final ELEM[]                              list,
            final IntTFunction<? super ELEM, String>  listItemPrinter,
            final int                                 lineWidth,
            final int                                 indentation,
            final boolean                             seeEscapedNewLinesAsText,
            final boolean                             printNulls,
            final boolean                             showLineNumbers
        )
    {
        if ((lineWidth < 8) || (lineWidth > 200)) throw new IllegalArgumentException(
            "[" + lineWidth + "] was passed to parameter 'lineWidth', but this value must be " +
            "between 8 and 200 is expected"
        );
    
        if (indentation >= 200) throw new IllegalArgumentException(
            "[" + indentation + "] was passed to parameter 'indentation', but this value must " +
            "be 200 or less."
        );

        String indentationStr = (indentation > 0)
            ? ('\n' + StringParse.nChars(' ', indentation))
            : "\n";

        StringBuilder   sb      = new StringBuilder();
        int             counter = 0;

        for (ELEM elem : list)
        {
            counter++;
            if ((elem == null) && (! printNulls)) continue;

            sb.append(indentationStr);

            if (showLineNumbers)
            {
                if (counter < 10)   sb.append(StringParse.zeroPad10e2(counter) + ") ");
                else                sb.append(counter + ") ");
            }

            if ((elem == null)) sb.append("null");

            else sb.append(
                StringParse.trimLeft(
                    StrPrint.abbrevEndRDSF(
                        listItemPrinter.apply(counter, elem), lineWidth,
                        seeEscapedNewLinesAsText
                    )));
        }

        return sb.toString();
    }
}