# HG changeset patch # User Jaroslav Tulach # Date 1318914623 -7200 # Node ID a2e90b86638a3d596ff5c3551e1635a727c61337 # Parent b632733724a864c54d78e168bbdf3423f8fc7758 One can use covariance and contravariance with generic types due to their erasure diff -r b632733724a8 -r a2e90b86638a samples/erasure/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/erasure/build.xml Tue Oct 18 07:10:23 2011 +0200 @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r b632733724a8 -r a2e90b86638a samples/erasure/nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/erasure/nbproject/project.xml Tue Oct 18 07:10:23 2011 +0200 @@ -0,0 +1,99 @@ + + + org.netbeans.modules.ant.freeform + + + Erasure + + + + Erasure + + + + + . + UTF-8 + + + + java + src-api1.0 + UTF-8 + + + + java + src-api2.0 + UTF-8 + + + + java + impl + UTF-8 + + + + + build + + + clean + + + run + + + clean + build + + + test + + + + + + + src-api1.0 + + + + src-api2.0 + + + + src-impl + + + build.xml + + + + + + + + + + + + + + + src-api1.0 + 1.6 + + + src-api2.0 + 1.6 + + + src-impl + src-api1.0 + 1.6 + + + + diff -r b632733724a8 -r a2e90b86638a samples/erasure/src-api1.0/api/Erasure.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/erasure/src-api1.0/api/Erasure.java Tue Oct 18 07:10:23 2011 +0200 @@ -0,0 +1,19 @@ +package api; + +import java.util.Set; + +public class Erasure { + private Erasure() { + } + + // BEGIN: variance.erasure.v1 + public static boolean arePositive(Set numbers) { + for (Integer n : numbers) { + if (n <= 0) { + return false; + } + } + return true; + } + // END: variance.erasure.v1 +} diff -r b632733724a8 -r a2e90b86638a samples/erasure/src-api2.0/api/Erasure.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/erasure/src-api2.0/api/Erasure.java Tue Oct 18 07:10:23 2011 +0200 @@ -0,0 +1,19 @@ +package api; + +import java.util.Set; + +public class Erasure { + private Erasure() { + } + + // BEGIN: variance.erasure.v2 + public static boolean arePositive(Set numbers) { + for (Number n : numbers) { + if (n.doubleValue() <= 0.0d) { + return false; + } + } + return true; + } + // END: variance.erasure.v2 +} diff -r b632733724a8 -r a2e90b86638a samples/erasure/src-impl/test/ErasureTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/erasure/src-impl/test/ErasureTest.java Tue Oct 18 07:10:23 2011 +0200 @@ -0,0 +1,20 @@ +package test; + +import api.Erasure; +import java.util.Set; +import java.util.TreeSet; + +public class ErasureTest { + // BEGIN: variance.erasure.test + public static void main(String[] args) { + Set oneToTen = new TreeSet(); + for (int i = 1; i <= 10; i++) { + oneToTen.add(i); + } + + boolean positive = Erasure.arePositive(oneToTen); + System.err.println("positive = " + positive); + assert positive : "The nubmers are positive: " + oneToTen; + } + // END: variance.erasure.test +}