//BoundedWildcardDemo.java.
//Demonstrate a bounded wildcard.

class BoundedWildcardDemo
{
    //A method to display the first two co-ordinates.
    //Can use a wildcard parameter since any element of the input
    //co-ordinate array is guaranteed to have at least the first
    //two co-ordinates.
    static void showXY(Coords<?> c)
    {
        System.out.println("X Y Coordinates:");
        for (int i = 0; i < c.coords.length; i++)
        {
            System.out.println(c.coords[i].x + " " + c.coords[i].y);
        }
        System.out.println();
    }

    //Prevents any TwoD objects in Coords
    static void showXYZ(Coords<? extends ThreeD> c)
    {
        System.out.println("X Y Z Coordinates:");
        for (int i = 0; i < c.coords.length; i++)
        {
            System.out.println
            (
                c.coords[i].x + " "
                + c.coords[i].y + " "
                + c.coords[i].z
            );
        }
        System.out.println();
    }

    static void showAll(Coords<? extends FourD> c)
    {
        System.out.println("X Y Z T Coordinates:");
        for (int i = 0; i < c.coords.length; i++)
        {
            System.out.println
            (
                c.coords[i].x + " "
                + c.coords[i].y + " "
                + c.coords[i].z + " "
                + c.coords[i].t
            );
        }
        System.out.println();
    }

    public static void main(String args[])
    {
        TwoD twoD[] =
        {
            new TwoD(0, 0),
            new TwoD(7, 9),
            new TwoD(18, 4),
            new TwoD(-1, -23)
        };

        Coords<TwoD> twoDlocations = new Coords<TwoD>(twoD);

        System.out.println("Contents of twoDlocations.");
        showXY(twoDlocations);    //OK, twoDlocations is a TwoD
        //showXYZ(twoDlocations); //Error, twoDlocations is not a ThreeD
        //showAll(twoDlocations); //Erorr, twoDlocations is not a FourD

        //Now, create some FourD objects.
        FourD fourD[] =
        {
            new FourD(1, 2, 3, 4),
            new FourD(6, 8, 14, 8),
            new FourD(22, 9, 4, 9),
            new FourD(3, -2, -23, 17)
        };

        Coords<FourD> fourDlocations = new Coords<FourD>(fourD);

        System.out.println("Contents of fourDlocations.");
        //These are all OK ...
        showXY(fourDlocations);
        showXYZ(fourDlocations);
        showAll(fourDlocations);
    }
}

