/*
 * GenericUseTest.java
 *
 * Created on June 7, 2004, 1:07 PM
 */
import java.util.ArrayList;
import java.math.BigInteger;
/**
 *
 * @author  cpresser
 */
public class GenericUseTest {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // An example of the use of Java Generics
        // Essentially allows you to specify a type for collections
        // create a list of numbers
        ArrayList<Number> list = new ArrayList<Number>();
        
        list.add(new Integer(5));
        list.add(6); //use autoboxing to put 6 into an Integer object
        list.add(new Double(3.14159/2));
        list.add(2.7);
        list.add(new BigInteger("1234567890123456789012345678901234567890"));
        list.add(new Long(127L));

        
        //an attempt to add something other than a Number or subclass of a 
        //number, results in a compile error...which is nice.
        //list.add("Hi");
        
        //print the list via a method call
        printListOne(list);
        
        //make a more specific list
        ArrayList<Integer> listTwo = new ArrayList<Integer>();
        for(int i = 0; i < 10; i++){
            listTwo.add(i);
        }
        
        //try printing it w/ printListOne
        //this causes a compile error, because ArrayList<Integer> is not a 
        //subclass of ArrayList<Number>.
        
        // printListOne(listTwo);
        
        //we can build printListTwo which uses a more generic type, but
        //allows you to pass in any ArrayList
        printListTwo(listTwo);
        
        //one more try here
        printListThree(listTwo);
    }
    
    //prints out alist of numbers
    public static void printListOne(ArrayList<Number> list){    
        System.out.println("-------printListOne--------");
        //Step through each item
        for(int i = 0; i < list.size(); i++){
            Number n = list.get(i); //know that what is stored is a Number
            //there is not much which we can take advantage of by virtue of the
            //fact that these are all numbers (comparison would be nice)
            //So we'll just print them out.
            System.out.println(n);
        }
    }
    
    //prints out alist of numbers
    //the question mark is a wild card.
    //any type of ArrayList will do
    public static void printListTwo(ArrayList<?> list){    
        System.out.println("-------printListTwo--------");
        //Step through each item
        for(int i = 0; i < list.size(); i++){
            //of course now we can't assume every element is a Number
            Number n = (Number)list.get(i);
            System.out.println(n);
        }
    }
    
    //one more stab at it
    //The items defined for the ArrayList must extend Number
    //So ArrayList<Number>, ArrayList<Integer>, ArrayList<Byte> etc
    // will all work.
    //This is checked at compile time.
    public static void printListThree(ArrayList<? extends Number> list){    
        System.out.println("-------printListThree--------");
        //Step through each item
        for(int i = 0; i < list.size(); i++){
            //of course now we can't assume every element is a Number
            Number n = list.get(i);
            System.out.println(n);
        }
    }
}