/**
 * 
 */
package generics;

public class TestRawType {
	
}

@@ -28,35 +28,1597 @@
 s;%0A%0A
-public class TestRawType %7B%0A%09
+import java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Iterator;%0A%0A/** This Class Tests - Best Practices for using Generics by Avoiding raw types*/%0A%0A/* Class for Testing */%0Aclass Stamp%7B%0A%09private int id;%0A%09%0A%09public Stamp(int a)%7B%0A%09%09id = a;%0A%09%7D%0A%09%0A%09public int get()%7B%0A%09%09return id;%0A%09%7D%0A%7D%0A%0Apublic class TestRawType %7B%0A%09public static void main(String abc%5B%5D)%7B%0A%09%09%0A%09%09/* no compile-time error, only warnings generated */%0A%09%09%0A%09%09/* our Stamps Collection -- RAW TYPE COLLECTION */%0A%09%09final Collection stamps_1 = new ArrayList(); %0A%09%09stamps_1.add(new Stamp(5));%0A%09%09%0A%09%09/* adversary attacks our collection by inserting non-stamp type and succeeds */%0A%09%09stamps_1.add(new Integer(2)); %0A%09%09%0A%09%09/* error during run-time */%0A%09%09/* RAW TYPE ITERATOR */%0A%09%09for(Iterator i = stamps_1.iterator(); i.hasNext();)%7B%0A%09%09%09Stamp s = (Stamp)i.next(); //Will Definitely Throw java.lang.ClassCastException%0A%09%09%7D%0A%09%09%0A%09%09/** To Avoid Run-time error which is what always wanted, %0A%09%09 * enforce type-check at compile time by avoiding raw type and parameterizing %0A%09%09 * the generic type. %0A%09%09 * */%0A%09%09%0A%09%09/** With generics, you can tell the compiler what %0A%09%09 * type is permitted in the collection at compile time.*/%0A%09%09%0A%09%09/* No Warnings after this point */%0A%09%09final Collection%3CStamp%3E stamps_2 = new ArrayList%3CStamp%3E();%0A%09%09stamps_2.add(new Stamp(4));%0A%09%09/* Compile-time error. Wont allow non-stamp type to be added in the collection %0A%09%09 * as it checks at compile-time  */%0A%09%09Stamp s;%0A%09%09//stamps_2.add(new Integer(4)); %0A%09%09for(Iterator%3CStamp%3E i = stamps_2.iterator(); i.hasNext();)%7B%0A%09%09%09s = i.next(); //No Cast needed, Iterator is of Stamp.%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add code fot Testing Raw Type Generics