Pages

UseStrList

public class UseStrList
{
    public static void main(String[] args)
    {
        StrList.printElements("args", args);
        StrList.main(args);
    }
}


class StrList
{
    public static String getValue(String[] array)
    {
        return array.toString();
    }
    public static void printElements(String label, String[] array)
    {
        StrList1.printElements(label, array);
    }
    public static void printElements(String[] array)
    {
        String label = getValue(array);
        StrList.printElements(label, array);
    }
    public static void main(String[] args)
    {
        String label = getValue(args);
        printElements(label, args);
        printElements("args", args);
        printElements(args);
        StrListUnitTester.main(args);
    }
}

class StrListUnitTester
{
    public static final StrListFactory f = new StrListFactory();
    
    public static void doTest(String[] array)
    {
        do
        {
            String label = StrList.getValue(array);
            StrList1.printElements(label, array);
            StrList2a.printElements(label, array);
            StrList2b.printElements(label, array);
            StrList2c.printElements(label, array);
            StrList3.printElements(label, array);
            array = f.produce();
        }
        while (!f.cycled);
    }
        
    public static void testRecursively(String[] array)
    {
        String label = StrList.getValue(array);
        StrList1.printElements(label, array);
        StrList2a.printElements(label, array);
        StrList2b.printElements(label, array);
        StrList2c.printElements(label, array);
        StrList3.printElements(label, array);

        if (!f.cycled)
        {
            array = f.produce();
            testRecursively(array);
        }
    }
    
    public static void main(String[] args)
    {
        StrListUnitTester.doTest(args);
        StrListUnitTester.testRecursively(args);
    }
}

class StrList1
{
    public static void printElements(String label, String[] array)
    {
        if (array == null)
        {
            Out.print("\n");
            return;
        }
        else
        {
            Out.print(label + ":");
        }
        for (String element : array)
        {
            Out.print(" " + element);
        }
        Out.print("\n");
    }
}

class StrList2a
{
    public static void printElements(String label, String[] array)
    {
        if (array == null)
        {
            System.out.print('\n');
            return;
        }
        else
        {
            Out.print(label + ':');
        }
        
        final int n = array.length;
        for (int i = 0; i < n; i++)
        {
            Out.print(' ' + array[i]);
        }
        
        Out.print('\n');
    }
}

class StrList2b
{
    public static void printElements(String label, String[] array)
    {
        final int maxElements = Integer.MAX_VALUE;

        if (array == null)
        {
            Out.print("\n");
            return;
        }
        else
        {
            System.out.print(label + ":");
        }
        
        final int n = array.length;
        for (int i = 0; i < n && i < maxElements; i++)
        {
            Out.print(" " + array[i]);
        }
        
        Out.print("\n");
    }
}

class StrList2c
{
    public static void printElements(String label, String[] array)
    {
        final int maxElements = Integer.MAX_VALUE;

        if (array == null)
        {
            Out.print("\n");
            return;
        }
        else
        {
            Out.print(label + ":");
        }
        
        final int n = array.length;
        for (int i = 0; i < n; i++)
        {
            if (i >= maxElements)
            {
                break;
            }
            Out.print(" " + array[i]);
        }
        
        Out.print("\n");
    }
}

class StrList3
{
    public static void printElements(String label, String[] array)
    {
        if (array == null)
        {
            Out.println();
            return;
        }
        else
        {
            Out.print(label + ":");
        }
        
        final int n = array.length;
        int i = 0;
        while (i < n)
        {
            Out.print(" " + array[i]);
            i++;
        }
        
        Out.println();
    }
}

class Out
{
    public static void print(char c)
    {
        System.out.print("" + c);
    }
    public static void print(String s)
    {
        System.out.print(s);
    }
    public static void println()
    {
        System.out.println();
    }
}

class StrListFactory
{
    private static final String
        e = ((Double)Math.E).toString();

    private final String[][] defQ = // default queue
    {
        {"K & R says, \"Print:"},
        {"\t", "h", e, "1", "l0"},
        {"\t", "world\n"},
        {"\""},
        {"."},
        { null },
        {},
        null
    };

    private final String[][] cirQ; // circular queue
    private final int n;
    private int i = 0;
    public boolean cycled = false;
    
    public StrListFactory()
    {
        this(null);
    }
    
    public StrListFactory(String[][] arrays)
    {
        cirQ = arrays == null ? defQ : arrays;
        n = cirQ.length;
    }

    public String[] produce()
    {
        String[] array = cirQ[i];
        i = (i + 1) % n;
        cycled = i == 0;
        return array;
    }
}