/** * Return true if the message's key matches this key matcher. */ public boolean matches(KeyedMessage other) { return matches(other.getKey()); }
if (matcher.matches(message.asKeyedMessage())) { MessageListenerGroup group = mMessageListeners.get(matcher); group.receive(message);
public void add(KeyedMessage message) { ThreadPreconditions.checkOnMainThread(); MessageKey key = message.getKey(); boolean dataSetChanged = false; if(mMessages.containsKey(key)) { mMessages.put(key, message); // Already exists in values, just need to update it KeyMatcher exactMatcher = ExactKeyMatcher.buildExactMatcher(key); for(int i = 0; i < mValues.size(); i++) { KeyedMessage existingMessage = ((KeyedMessage)mValues.get(i)); if(exactMatcher.matches(existingMessage.getKey())) { mValues.set(i, message); dataSetChanged |= shouldRefreshView(message, existingMessage); break; } } } else { // Need to recreate values list because positions will be shifted mMessages.put(key, message); mValues = new ArrayList<VehicleMessage>(mMessages.values()); Collections.sort(mValues); dataSetChanged = true; } if(dataSetChanged) { notifyDataSetChanged(); } }
@Test public void exactMatcherMatchesOriginal() { KeyMatcher matcher = ExactKeyMatcher.buildExactMatcher(keyed); assertTrue(matcher.matches(keyed)); }
@Test public void wildcardMatcherMatchesAll() { assertTrue(KeyMatcher.getWildcardMatcher().matches(keyed)); }
@Test public void exactMatcherMatchesMessageWithSameKey() { KeyMatcher matcher = ExactKeyMatcher.buildExactMatcher(keyed); KeyedMessage another = new TestKeyedMessage(); assertTrue(matcher.matches(another)); }
@Test public void matchesKeyFromCommand() { Command originalCommand = new Command(command); KeyMatcher matcher = ExactKeyMatcher.buildExactMatcher(originalCommand); assertTrue(matcher.matches(response)); } }