Skip to content

Commit baf99c6

Browse files
author
jim.deng
committed
2020年12月17日11:40:30 02
1 parent b311d3f commit baf99c6

6 files changed

Lines changed: 71 additions & 12 deletions

File tree

onjava/TypeCounter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
// Counts instances of a type family
6-
package onjava;
6+
//package onjava;
77
import java.util.*;
88
import java.util.stream.*;
99

10-
public class
11-
TypeCounter extends HashMap<Class<?>, Integer> {
10+
public class TypeCounter extends HashMap<Class<?>, Integer> {
1211
private Class<?> baseType;
1312
public TypeCounter(Class<?> baseType) {
1413
this.baseType = baseType;

selftypeinfo/typeinfo/PetCount3.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.LinkedHashMap;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.function.Consumer;
1516
import java.util.function.Function;
1617
import java.util.stream.Collectors;
1718

@@ -74,11 +75,13 @@ public String toString() {
7475

7576
public static void main(String[] args) {
7677
Counter petCount = new Counter();
78+
Consumer<Pet> count = petCount::count;
79+
Consumer<Pet> petConsumer = p -> System.err.print(p.getClass().getSimpleName() + " ");
7780
Pets.stream()
7881
.limit(20)
79-
.peek(petCount::count)
80-
.forEach(p -> System.err.print(
81-
p.getClass().getSimpleName() + " "));
82+
.peek(count)
83+
.forEach(petConsumer);//self-note: 这两个consumer可互换位置
84+
8285
System.err.println("\n" + petCount);
8386
}
8487

@@ -104,3 +107,12 @@ public static <K, V> Map<K, V> listToMap(List<K> keys, List<V> values) {
104107
{Rat=2, Pug=3, Mutt=3, Mouse=2, Cat=9, Dog=6, Cymric=5,
105108
EgyptianMau=2, Rodent=5, Hamster=1, Manx=7, Pet=20}
106109
*/
110+
111+
112+
/**
113+
*
114+
* {EgyptianMau=2, Pug=3, Rat=2, Cymric=5, Mouse=2, Cat=9, Manx=7, Rodent=5, Mutt=3, Dog=6, Pet=20, Hamster=1}
115+
* {EgyptianMau=2, Pug=3, Rat=2, Cymric=5, Mouse=2, Cat=9, Manx=7, Rodent=5, Mutt=3, Dog=6, Pet=20, Hamster=1}
116+
* {EgyptianMau=2, Pug=3, Rat=2, Cymric=5, Mouse=2, Cat=9, Manx=7, Rodent=5, Mutt=3, Dog=6, Pet=20, Hamster=1}
117+
*
118+
*/

selftypeinfo/typeinfo/PetCount4.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// typeinfo/PetCount4.java
1+
package typeinfo;// typeinfo/PetCount4.java
22
// (c)2020 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import typeinfo.pets.*;
6-
import onjava.*;
6+
//import onjava.*;
77

88
public class PetCount4 {
99
public static void main(String[] args) {

selftypeinfo/typeinfo/RegisteredFactories.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,26 @@ class Part implements Supplier<Part> {
1212
public String toString() {
1313
return getClass().getSimpleName();
1414
}
15-
static List<Supplier<? extends Part>> prototypes =
16-
Arrays.asList(
15+
static List<Supplier<? extends Part>> prototypes;
16+
17+
static {
18+
AirFilter airFilter = new AirFilter();
19+
prototypes = Arrays.asList(
1720
new FuelFilter(),
18-
new AirFilter(),
21+
airFilter,
1922
new CabinAirFilter(),
2023
new OilFilter(),
2124
new FanBelt(),
2225
new PowerSteeringBelt(),
2326
new GeneratorBelt()
2427
);
28+
}
29+
2530
private static Random rand = new Random(47);
31+
@Override
2632
public Part get() {
2733
int n = rand.nextInt(prototypes.size());
34+
System.out.println("RANDNUM: "+n);
2835
return prototypes.get(n).get();
2936
}
3037
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package typeinfo;// onjava/TypeCounter.java
2+
// (c)2020 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Counts instances of a type family
6+
//package onjava;
7+
8+
import java.util.HashMap;
9+
import java.util.stream.Collectors;
10+
11+
public class TypeCounter extends HashMap<Class<?>, Integer> {
12+
private Class<?> baseType;
13+
public TypeCounter(Class<?> baseType) {
14+
this.baseType = baseType;
15+
}
16+
public void count(Object obj) {
17+
Class<?> type = obj.getClass();
18+
if(!baseType.isAssignableFrom(type)) throw new RuntimeException(
19+
obj + " incorrect type: " + type +
20+
", should be type or subtype of " + baseType);
21+
countClass(type);
22+
}
23+
private void countClass(Class<?> type) {
24+
Integer quantity = get(type);
25+
put(type, quantity == null ? 1 : quantity + 1);
26+
Class<?> superClass = type.getSuperclass();
27+
if(superClass != null &&
28+
baseType.isAssignableFrom(superClass))
29+
countClass(superClass);
30+
}
31+
@Override
32+
public String toString() {
33+
String result = entrySet().stream()
34+
.map(pair -> String.format("%s=%s",
35+
pair.getKey().getSimpleName(),
36+
pair.getValue()))
37+
.collect(Collectors.joining(", "));
38+
return "{" + result + "}";
39+
}
40+
}

selftypeinfo/typeinfo/pets/Pets.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static List<Pet> list(int size) {
2929
}
3030

3131
public static Stream<Pet> stream() {
32-
return Stream.generate(CREATOR);
32+
Stream<Pet> generate = Stream.generate(CREATOR);
33+
return generate;
3334
}
3435
}

0 commit comments

Comments
 (0)