Kafka代码写法总结1Java
文章发布较早,内容可能过时,阅读注意甄别。
# 总结1
# k,v泛型
public class AdminApiDriver<K, V> {
private final Logger log;
private final long retryBackoffMs;
private final long deadlineMs;
private final AdminApiHandler<K, V> handler;
private final AdminApiFuture<K, V> future;
private final BiMultimap<ApiRequestScope, K> lookupMap = new BiMultimap<>();
private final BiMultimap<FulfillmentScope, K> fulfillmentMap = new BiMultimap<>();
private final Map<ApiRequestScope, RequestState> requestStates = new HashMap<>();
/**
* Helper class which maintains a bi-directional mapping from a key to a set of values.
* Each value can map to one and only one key, but many values can be associated with
* a single key.
*
* @param <K> The key type
* @param <V> The value type
*/
private static class BiMultimap<K, V> {
private final Map<V, K> reverseMap = new HashMap<>();
private final Map<K, Set<V>> map = new HashMap<>();
void put(K key, V value) {
remove(value);
reverseMap.put(value, key);
map.computeIfAbsent(key, k -> new HashSet<>()).add(value);
}
void remove(V value) {
K key = reverseMap.remove(value);
if (key != null) {
Set<V> set = map.get(key);
if (set != null) {
set.remove(value);
if (set.isEmpty()) {
map.remove(key);
}
}
}
}
Optional<K> getKey(V value) {
return Optional.ofNullable(reverseMap.get(value));
}
Set<Map.Entry<K, Set<V>>> entrySet() {
return map.entrySet();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Node节点定义
public class Node {
private static final Node NO_NODE = new Node(-1, "", -1);
private final int id;
private final String idString;
private final String host;
private final int port;
private final String rack;
// Cache hashCode as it is called in performance sensitive parts of the code (e.g. RecordAccumulator.ready)
private Integer hash;
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12