Skip to content

Commit d59c0d3

Browse files
committed
multiple bounds example
1 parent 521bc00 commit d59c0d3

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2015-2017 Toby Weston
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package s4j.java.chapter14;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
public class MultipleBounds {
24+
25+
// sets two upper bounds to the generic type A
26+
public static <A extends Animal & Comparable<Animal>> void sort(List<A> list) {
27+
Collections.sort(list);
28+
}
29+
30+
public static void main(String... args) {
31+
32+
List<Lion> enclosure = new ArrayList<>();
33+
enclosure.add(new Lion());
34+
enclosure.add(new Lion());
35+
sort(enclosure);
36+
37+
List<Animal> zoo = new ArrayList<>();
38+
zoo.add(new Lion());
39+
zoo.add(new Lion());
40+
zoo.add(new Zebra());
41+
sort(zoo);
42+
}
43+
44+
45+
static class Animal implements Comparable<Animal> {
46+
@Override
47+
public int compareTo(Animal o) {
48+
return 0;
49+
}
50+
}
51+
52+
static class Lion extends Animal { }
53+
static class Zebra extends Animal { }
54+
55+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2015-2017 Toby Weston
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package s4j.scala.chapter14
18+
19+
class MultipleBounds {
20+
21+
// we can't set two upper bounds like we can in Java but we can say the bound type
22+
// must also extend certain traits
23+
def sort[A <: Lion with Comparable[Animal]](list: List[A]) = { }
24+
25+
def main(args: String*) {
26+
27+
var enclosure = List[Lion]()
28+
enclosure = new Lion +: enclosure
29+
enclosure = new Lion +: enclosure
30+
// sort[Lion, Animal](enclosure) // compiler error
31+
sort(enclosure) // must remove the type hints
32+
33+
// var zoo = List[Animal]()
34+
// zoo = new Zebra +: zoo
35+
// zoo = new Lion +: zoo
36+
// zoo = new Lion +: zoo
37+
// sort(zoo)
38+
}
39+
40+
class Animal extends Comparable[Animal] {
41+
def compareTo(o: Animal): Int = 0
42+
}
43+
class Lion extends Animal
44+
class Zebra extends Animal
45+
}

0 commit comments

Comments
 (0)