OnJava8-Examples/containers/TypesForSets.java

86 lines
2.7 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: containers/TypesForSets.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
// Methods necessary to put your own type in a Set.
2015-05-06 12:09:38 -07:00
import java.lang.reflect.InvocationTargetException;
2015-04-20 15:36:01 -07:00
import java.util.*;
class SetType {
int i;
public SetType(int n) { i = n; }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public boolean equals(Object o) {
return o instanceof SetType && (i == ((SetType)o).i);
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public String toString() { return Integer.toString(i); }
}
class HashType extends SetType {
public HashType(int n) { super(n); }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public int hashCode() { return i; }
}
class TreeType extends SetType
implements Comparable<TreeType> {
public TreeType(int n) { super(n); }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public int compareTo(TreeType arg) {
return (arg.i < i ? -1 : (arg.i == i ? 0 : 1));
}
}
public class TypesForSets {
static <T> Set<T> fill(Set<T> set, Class<T> type) {
try {
for(int i = 0; i < 10; i++)
set.add(
type.getConstructor(int.class).newInstance(i));
2015-05-06 12:09:38 -07:00
} catch(NoSuchMethodException |
SecurityException |
InstantiationException |
IllegalAccessException |
IllegalArgumentException |
InvocationTargetException e) {
2015-04-20 15:36:01 -07:00
throw new RuntimeException(e);
}
return set;
}
static <T> void test(Set<T> set, Class<T> type) {
fill(set, type);
fill(set, type); // Try to add duplicates
fill(set, type);
System.out.println(set);
}
public static void main(String[] args) {
2015-05-05 11:20:13 -07:00
test(new HashSet<>(), HashType.class);
test(new LinkedHashSet<>(), HashType.class);
test(new TreeSet<>(), TreeType.class);
2015-04-20 15:36:01 -07:00
// Things that don't work:
2015-05-05 11:20:13 -07:00
test(new HashSet<>(), SetType.class);
test(new HashSet<>(), TreeType.class);
test(new LinkedHashSet<>(), SetType.class);
test(new LinkedHashSet<>(), TreeType.class);
2015-04-20 15:36:01 -07:00
try {
2015-05-05 11:20:13 -07:00
test(new TreeSet<>(), SetType.class);
2015-04-20 15:36:01 -07:00
} catch(Exception e) {
System.out.println("Expected: " + e.getMessage());
2015-04-20 15:36:01 -07:00
}
try {
2015-05-05 11:20:13 -07:00
test(new TreeSet<>(), HashType.class);
2015-04-20 15:36:01 -07:00
} catch(Exception e) {
System.out.println("Expected: " + e.getMessage());
2015-04-20 15:36:01 -07:00
}
}
} /* Output: (Sample)
[2, 4, 9, 8, 6, 1, 3, 7, 5, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[9, 9, 7, 5, 1, 2, 6, 3, 0, 7, 2, 4, 4, 7, 9, 1, 3, 6, 2, 4, 3, 0, 5, 0, 8, 8, 8, 6, 5, 1]
[0, 5, 5, 6, 5, 0, 3, 1, 9, 8, 4, 2, 3, 9, 7, 3, 4, 4, 0, 7, 1, 9, 6, 2, 1, 8, 2, 8, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Expected: java.lang.ClassCastException: SetType cannot be cast to java.lang.Comparable
Expected: java.lang.ClassCastException: HashType cannot be cast to java.lang.Comparable
2015-05-05 11:20:13 -07:00
*///:~