Created
October 30, 2012 09:32
-
-
Save fikovnik/3979247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package generics; | |
import static org.junit.Assert.*; | |
import static org.junit.Assert.assertEquals; | |
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class GenericsTest { | |
interface Printable { | |
public void print(); | |
} | |
static class Sum<T extends Number & Printable> { | |
private T num1; | |
private T num2; | |
public Sum(T num1, T num2) { | |
this.num1 = num1; | |
this.num2 = num2; | |
} | |
public double doubleValue() { | |
return num1.doubleValue() + num2.doubleValue(); | |
} | |
public int intValue() { | |
num1.print(); | |
num2.print(); | |
return num1.intValue() + num2.intValue(); | |
} | |
} | |
class PrintableNumber extends Number implements Printable { | |
private Integer i; | |
public PrintableNumber(Integer i) { | |
this.i = i; | |
} | |
@Override | |
public void print() { | |
System.out.println(i); | |
} | |
@Override | |
public int intValue() { | |
return i; | |
} | |
@Override | |
public long longValue() { | |
return i.longValue(); | |
} | |
@Override | |
public float floatValue() { | |
return i.floatValue(); | |
} | |
@Override | |
public double doubleValue() { | |
return i.doubleValue(); | |
} | |
} | |
@Test | |
public void testName() throws Exception { | |
Sum<PrintableNumber> sumi = new Sum<PrintableNumber>( | |
new PrintableNumber(1), new PrintableNumber(2)); | |
assertEquals(3, sumi.intValue()); | |
} | |
/** | |
* List contains only strings. | |
*/ | |
List<String> myList = new ArrayList<String>(); | |
@Test | |
public void testGenericInfo() throws Exception { | |
Class<?> cl = getClass(); | |
Field f = cl.getDeclaredFields()[0]; | |
System.out.println(f.getType()); | |
System.out.println(f.getGenericType()); | |
} | |
@Test | |
public void test() { | |
myList.add("Hi"); | |
// ... | |
String val = myList.get(0); | |
assertEquals("HI", val.toUpperCase()); | |
} | |
@Test | |
public void testBounds() throws Exception { | |
List<String> a = new ArrayList<String>(); | |
List<String> b = new ArrayList<String>(); | |
List<Integer> c = new ArrayList<Integer>(); | |
List<Integer> d = new ArrayList<Integer>(); | |
assertEquals(0, numOfSameElements(a, b)); | |
assertEquals(0, numOfSameElements(c, d)); | |
} | |
static int numOfSameElements(List<?> a, List<?> b) { | |
int result = 0; | |
for (Object s : a) { | |
if (b.contains(s)) { | |
result++; | |
} | |
} | |
return result; | |
} | |
@Test | |
public void testUnion() throws Exception { | |
List<Integer> c = new ArrayList<Integer>(); | |
List<Integer> d = new ArrayList<Integer>(); | |
List<Integer> r = union(c, d); | |
List<String> a = new ArrayList<String>(); | |
List<String> b = new ArrayList<String>(); | |
List<String> r2 = union(a, b); | |
} | |
@Test | |
public void testUnionHierarchy() throws Exception { | |
List<Integer> i = new ArrayList<Integer>(); | |
List<Double> d = new ArrayList<Double>(); | |
List<Number> r = GenericsTest.<Number> union(i, d); | |
} | |
static <T> List<T> union(List<? extends T> list1, List<? extends T> list2) { | |
List<T> r = new ArrayList<T>(list2); | |
for (T i : list1) { | |
if (r.contains(i)) { | |
r.add(i); | |
} | |
} | |
return r; | |
} | |
@Test | |
public void testArrayCovariance() throws Exception { | |
Object[] array = new String[10]; | |
array[0] = 10; | |
assertEquals(10, array[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment