TopicMatcher { public static boolean match(String topicFilter, String topicName) { if (!TopicMatcher.isValid(topicFilter, true) || !TopicMatcher.isValid(topicName, false)) { return false; } if ((topicFilter.startsWith(MULTI_LEVEL_WILDCARD) || topicFilter.startsWith(SINGLE_LEVEL_WILDCARD)) && topicName.startsWith("$")) { return false; } String[] tfs = topicFilter.split("/"); String[] tns = topicName.split("/"); for (int i = 0; i < tns.length; ++i) { if (i >= tfs.length) { return false; } else if (tfs[i].equals(MULTI_LEVEL_WILDCARD)) { return true; } else if (tfs[i].equals(SINGLE_LEVEL_WILDCARD)) { continue; } else if (tfs[i].equals(tns[i])) { continue; } else { return false; } } return true; }  static boolean isValid(String topicFilter, boolean allowWildcard); static boolean match(String topicFilter, String topicName); static final String MULTI_LEVEL_WILDCARD_PATTERN; static final String TOPIC_WILDCARDS; }
@Test public void testMatch() throws Exception { Assert.assertThat(TopicMatcher.match("test/1/2", "test/1/2"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("test+", "test_1"), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("test#", "test_1"), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("+test+", "1_test"), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("#test", "1_test"), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("#", ""), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("#", "/"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("#", "test"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("#", "test/1"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("+", "test"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("+", "test/1"), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("test/+", "test/1"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("test/#", "test/1"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("test/#", "test/1/2"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("test/+/", "test/1"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("test/+/", "test/1/"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("test/+/2", "test/1/2"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("test/#/2", "test/1/2"), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("/#", "/test"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("/test/+", "/test/1"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("/test/#", "/test/1"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("/test/#", "/test/1/2"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("/test/+/", "/test/1"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("/test/+/", "/test/1/"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("/test/+/2", "/test/1/2"), IsEqual.equalTo(true)); Assert.assertThat(TopicMatcher.match("/test/#/2", "/test/1/2"), IsEqual.equalTo(false)); Assert.assertThat(TopicMatcher.match("/test/+/2", "/test/1/3"), IsEqual.equalTo(false)); }