Skip to content

Commit 3655501

Browse files
committed
chapter 14
1 parent 03276d6 commit 3655501

15 files changed

Lines changed: 666 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package s4j.java.chapter14;
2+
3+
import s4j.java.chapter12.Customer;
4+
import s4j.java.chapter12.DiscountedCustomer;
5+
import s4j.java.chapter12.PricedItem;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Example {
10+
public static void main(String... args) {
11+
polymorphismInGenericLists();
12+
exampleOfBoundedTypes();
13+
assignmentRules();
14+
}
15+
16+
private static void polymorphismInGenericLists() {
17+
List<Customer> customers = new ArrayList<>();
18+
// customers.add(new HockeyPuck()); // compiler failure
19+
20+
Customer customerA = new Customer("Bob", "15 Fleetwood Mack Road");
21+
Customer customerB = new DiscountedCustomer("Derick Jonar", "23 Woodland Way");
22+
customerA.add(new PricedItem(10D));
23+
customerB.add(new PricedItem(10D));
24+
25+
customers.add(customerA);
26+
customers.add(customerB);
27+
28+
for (Customer customer : customers) {
29+
System.out.println(customer.getName() + " : " + customer.total());
30+
}
31+
}
32+
33+
public static void exampleOfBoundedTypes() {
34+
35+
// what's the difference between these two?
36+
37+
Stack<Integer> a = new NumberStack<>();
38+
a.push(12);
39+
Integer popA = a.pop();
40+
41+
Stack<Integer> b = new ListStack<>();
42+
b.push(12);
43+
Integer popB = b.pop();
44+
45+
46+
// Stack<String> b = new NumberStack<>(); // compiler failure
47+
}
48+
49+
private static void assignmentRules() {
50+
51+
// simple sub-type assignment
52+
53+
A a = new A();
54+
B b = new B();
55+
a = b;
56+
// b = a; // compiler failure
57+
58+
// not the same rules for generics
59+
List<B> listA = new ArrayList<>();
60+
List<A> listB = new ArrayList<>();
61+
// listA = listB; // compiler failure
62+
// listB = listA; // compiler failure
63+
}
64+
65+
public static <A extends Comparable> List<A> sort(List<A> list) {
66+
list.sort((first, second) -> first.compareTo(second));
67+
return list;
68+
}
69+
70+
public static class NumberStack<T extends Number> extends ListStack<T> { }
71+
private static class HockeyPuck {}
72+
private static class A {}
73+
private static class B extends A {}
74+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package s4j.java.chapter14;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Existential {
7+
8+
private final List<? extends Object> foo = new ArrayList<>();
9+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package s4j.java.chapter14;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class ListStack<T> implements Stack<T> {
8+
9+
static <A> A[] toArray(Stack<A> stack) {
10+
throw new UnsupportedOperationException();
11+
}
12+
13+
static <A extends Comparable<A>> A[] toSortedArray(Stack<A> stack) {
14+
throw new UnsupportedOperationException();
15+
}
16+
17+
public static <T> void copy(ListStack<? extends T> source, ListStack<? super T> destination) {
18+
for (T t : source.elements) {
19+
destination.elements.add(t);
20+
}
21+
}
22+
23+
private final List<T> elements = new ArrayList<>();
24+
25+
@Override
26+
public void push(T t) {
27+
elements.add(0, t);
28+
System.out.println(Arrays.toString(elements.toArray()));
29+
}
30+
31+
@Override
32+
public T pop() {
33+
if (elements.isEmpty())
34+
throw new IndexOutOfBoundsException();
35+
return elements.remove(0);
36+
}
37+
38+
public static void main(String... args) {
39+
Stack<String> stack = new ListStack<>();
40+
stack.push("C");
41+
stack.push("B");
42+
stack.push("A");
43+
System.out.println(stack.pop());
44+
System.out.println(stack.pop());
45+
System.out.println(stack.pop());
46+
// System.out.println(stack.pop());
47+
48+
49+
List<String> a = new ArrayList<>();
50+
List<? extends CharSequence> b = new ArrayList<>();
51+
List<? extends Object> c = new ArrayList<>();
52+
53+
b = a;
54+
c = b;
55+
c = a;
56+
}
57+
58+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
package s4j.java.chapter14;
3+
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
public class LowerBounds {
9+
10+
public static <A extends Comparable<? super A>> void sort(List<A> list) {
11+
Collections.sort(list);
12+
}
13+
14+
public static void main(String... args) {
15+
List<Animal> zoo = new ArrayList<>();
16+
zoo.add(new Wolf());
17+
zoo.add(new Lion());
18+
zoo.add(new Wolf());
19+
System.out.println(zoo);
20+
sort(zoo);
21+
System.out.println(zoo);
22+
23+
List<Cat> cattery = new ArrayList<>();
24+
sort(cattery);
25+
}
26+
27+
interface Animal extends Comparable<Animal> {
28+
default int compareTo(Animal o) {
29+
return this.getFoodChainRanking().compareTo(o.getFoodChainRanking());
30+
}
31+
Integer getFoodChainRanking();
32+
}
33+
34+
static class Lion implements Animal {
35+
@Override
36+
public Integer getFoodChainRanking() {
37+
return 10;
38+
}
39+
}
40+
41+
static class Wolf implements Animal {
42+
@Override
43+
public Integer getFoodChainRanking() {
44+
return 8;
45+
}
46+
}
47+
48+
static class DomesticPet {}
49+
50+
static class Cat extends DomesticPet implements Comparable<DomesticPet>{
51+
@Override
52+
public int compareTo(DomesticPet o) {
53+
return 0;
54+
}
55+
}
56+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package s4j.java.chapter14;
2+
3+
public interface Stack<T> {
4+
void push(T t);
5+
T pop();
6+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package s4j.java.chapter14;
2+
3+
@SuppressWarnings("all")
4+
public class Variance {
5+
6+
static class A {}
7+
static class B extends A {}
8+
9+
static class Container<T> {
10+
}
11+
12+
public static void main(String... args) {
13+
basicSubTyping();
14+
containerSubTyping();
15+
}
16+
17+
private static void basicSubTyping() {
18+
A a = new A();
19+
B b = new B();
20+
a = b;
21+
// b = a; // compiler failure
22+
}
23+
24+
private static void containerSubTyping() {
25+
Container<A> a = new Container<>();
26+
Container<B> b = new Container<>();
27+
28+
// a = b; // compiler failure
29+
}
30+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package s4j.java.chapter14;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import static s4j.java.chapter14.Zoo.Animal;
8+
import static s4j.java.chapter14.Zoo.Lion;
9+
10+
@SuppressWarnings("all")
11+
public class Wildcards {
12+
13+
public static void main(String... args) {
14+
cantAddToList();
15+
16+
// printObjects(new ArrayList<String>()); // compiler error
17+
printObjects(new ArrayList<Object>());
18+
19+
// print anything (List<?>)
20+
printUnknown(new ArrayList<String>());
21+
printUnknown(new ArrayList<Lion>());
22+
23+
// printAnimals(new ArrayList<String>());
24+
printAnimals(new ArrayList<Lion>());
25+
26+
// addNumbers(new ArrayList<String>());
27+
addNumbers(new ArrayList<Integer>());
28+
addNumbers(new ArrayList<Number>());
29+
addNumbers(new ArrayList<Object>());
30+
}
31+
32+
// can't add anything to this collection (except null), see http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html
33+
private static void cantAddToList() {
34+
List<?> list = new ArrayList<String>();
35+
// list.add(new Object()); // compiler error
36+
// list.add("Hello"); // compiler error
37+
list.add(null); // ok
38+
}
39+
40+
static void printObjects(List<Object> list) {
41+
for (Object element : list) {
42+
System.out.println(element);
43+
}
44+
}
45+
46+
static void printUnknown(List<?> list) {
47+
for (Object element : list) {
48+
System.out.println(element);
49+
}
50+
}
51+
52+
static void printAnimals(List<? extends Animal> animals) {
53+
for (Animal animal : animals) {
54+
System.out.println(animal);
55+
}
56+
}
57+
58+
static void addNumbers(List<? super Integer> numbers) {
59+
for (int i = 0; i < 100; i++) {
60+
numbers.add(i);
61+
}
62+
}
63+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
package s4j.java.chapter14;
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
@SuppressWarnings("all")
8+
public class Zoo {
9+
10+
public static <A extends Comparable<? super A>> void sort(List<A> list) {
11+
}
12+
13+
// public static <A extends Comparable<?>, U> void scalaLikeSort(List<A> list) {
14+
public static <A extends Comparable<?>> void scalaLikeSort(List<A> list) {
15+
// eg. Collections.sort(list);
16+
}
17+
18+
public static void main(String... args) {
19+
List<Lion> enclosure = new ArrayList<>();
20+
enclosure.add(new Lion());
21+
enclosure.add(new Lion());
22+
sort(enclosure);
23+
24+
List<Animal> zoo = new ArrayList<>();
25+
zoo.add(new Zebra());
26+
zoo.add(new Lion());
27+
zoo.add(new Lion());
28+
sort(zoo);
29+
}
30+
31+
static class Animal implements Comparable<Animal> {
32+
@Override
33+
public int compareTo(Animal o) {
34+
return 0;
35+
}
36+
}
37+
static class Lion extends Animal { }
38+
static class Zebra extends Animal { }
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package s4j.scala.chapter14
2+
3+
class Animal extends Comparable[Animal] {
4+
def compareTo(o: Animal): Int = 0
5+
}
6+
class Lion extends Animal
7+
class Zebra extends Animal
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package s4j.scala.chapter14
2+
3+
class Existential {
4+
5+
// this is described as an existential type
6+
7+
val foo: List[_ <: AnyRef] = List()
8+
9+
}

0 commit comments

Comments
 (0)