-
Notifications
You must be signed in to change notification settings - Fork 2
/
MessageResolverMux.java
136 lines (116 loc) · 4.25 KB
/
MessageResolverMux.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.taptrack.tcmptappy2;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A MessageResolverMux multiplexes several message resolvers.
*
* If it is created with CommandFamilyMessageResolvers, they will be
* separated out and used first in order to attempt to resolve
* the message. Other message resolvers will then be called in the
* order they are specified until one of them returns a non-null
* result or an exception is thrown.
*
* Note that this muxer does not support multiple resolvers for each
* command family. In case of duplicates, the last one you specify
* will be the one used.
*/
public class MessageResolverMux implements MessageResolver {
private static class CommandFamilyIdKey {
private final byte[] familyId;
private CommandFamilyIdKey(byte[] familyId) {
this.familyId = familyId;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CommandFamilyIdKey that = (CommandFamilyIdKey) o;
return Arrays.equals(familyId, that.familyId);
}
@Override
public int hashCode() {
return Arrays.hashCode(familyId);
}
}
private final Map<CommandFamilyIdKey, MessageResolver> familyResolverMap;
private final List<MessageResolver> otherResolverList;
public MessageResolverMux(MessageResolver... resolvers) {
Map<CommandFamilyIdKey,MessageResolver> resolverMap = null;
List<MessageResolver> otherResolverList = null;
for (MessageResolver resolver :
resolvers) {
if (resolver instanceof CommandFamilyMessageResolver) {
if (resolverMap == null) {
resolverMap = new HashMap<>(1);
}
resolverMap.put(
new CommandFamilyIdKey(((CommandFamilyMessageResolver) resolver).getCommandFamilyId()),
resolver
);
} else {
if (otherResolverList == null) {
otherResolverList = new ArrayList<>(1);
}
otherResolverList.add(resolver);
}
}
if (resolverMap != null) {
familyResolverMap = resolverMap;
} else {
familyResolverMap = Collections.emptyMap();
}
if (otherResolverList != null) {
this.otherResolverList = otherResolverList;
} else {
this.otherResolverList = Collections.emptyList();
}
}
@Nullable
@Override
public TCMPMessage resolveCommand(@NonNull TCMPMessage message) throws MalformedPayloadException {
CommandFamilyIdKey key = new CommandFamilyIdKey(message.getCommandFamily());
MessageResolver familyResolver = familyResolverMap.get(key);
if (familyResolver != null) {
TCMPMessage result = familyResolver.resolveCommand(message);
if (result != null) {
return result;
}
}
for(MessageResolver resolver: otherResolverList) {
TCMPMessage result = resolver.resolveCommand(message);
if (result != null) {
return result;
}
}
return null;
}
@Nullable
@Override
public TCMPMessage resolveResponse(@NonNull TCMPMessage message) throws MalformedPayloadException {
CommandFamilyIdKey key = new CommandFamilyIdKey(message.getCommandFamily());
MessageResolver familyResolver = familyResolverMap.get(key);
if (familyResolver != null) {
TCMPMessage result = familyResolver.resolveResponse(message);
if (result != null) {
return result;
}
}
for(MessageResolver resolver: otherResolverList) {
TCMPMessage result = resolver.resolveResponse(message);
if (result != null) {
return result;
}
}
return null;
}
}