public static boolean isMatch()

in mqtt-common/src/main/java/org/apache/rocketmq/mqtt/common/util/TopicUtils.java [136:176]


    public static boolean isMatch(String topic, String topicFilter) {
        if (topic.equals(topicFilter)) {
            return true;
        }
        if (!isWildCard(topicFilter)) {
            return false;
        }

        String[] subscribeTopics = topicFilter.split(Constants.MQTT_TOPIC_DELIMITER);
        String[] messageTopics = topic.split(Constants.MQTT_TOPIC_DELIMITER);
        int targetTopicLength = messageTopics.length;
        int sourceTopicLength = subscribeTopics.length;
        int minTopicLength = Math.min(targetTopicLength, sourceTopicLength);

        for (int i = 0; i < minTopicLength; i++) {
            String sourceTopic = subscribeTopics[i];

            if (!isWildCard(sourceTopic)) {
                if (!sourceTopic.equals(messageTopics[i])) {
                    return false;
                }
            }
            // multi level
            // [MQTT-4.7.1-2] In either case '#' MUST be the last character specified in the Topic Filter
            // and "t/t1#" is invalid
            if (Constants.NUMBER_SIGN.equals(sourceTopic)) {
                return i == sourceTopicLength - 1;
            }
            boolean last = i == minTopicLength - 1 &&
                (sourceTopicLength == targetTopicLength ||
                    sourceTopicLength == targetTopicLength + 1 &&
                        Constants.NUMBER_SIGN.equals(subscribeTopics[sourceTopicLength - 1])

                );
            if (last) {
                return true;
            }
        }

        return false;
    }