All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.iot.common.util.MatchingKit Maven / Gradle / Ivy

There is a newer version: 2.3.3.RELEASE
Show newest version
package com.feingto.iot.common.util;

import lombok.extern.slf4j.Slf4j;

/**
 * 匹配工具
 *
 * @author longfei
 */
@Slf4j
public class MatchingKit {
    /**
     * 匹配字符串
     * "/"连接的字符串,其中包含单个"#"匹配多级,包含单个"+"匹配一级
     *
     * @param target 源字符串
     * @param source 待匹配的字符串
     */
    public static boolean ofTopic(String source, String target) {
        log.debug(">>> try matching source: {}, target: {}", source, target);
        boolean result = false;
        String[] sources = source.split("/");
        String[] targets = target.split("/");
        if (sources.length <= targets.length + 1) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < sources.length; i++) {
                if (sources[i].equals("+")) {
                    sb.append(sources[i]).append("/");
                } else if (sources[i].equals("#")) {
                    sb.append(sources[i]).append("/");
                    break;
                } else {
                    sb.append(i < targets.length ? targets[i] : sources[i]).append("/");
                }
            }
            String str = sb.toString();
            if (str.endsWith("/")) {
                str = str.substring(0, str.lastIndexOf("/"));
            }
            if (str.equals(source) && (str.endsWith("/#") || sources.length == targets.length)) {
                result = true;
            }
            log.debug(">>> matched source: {}, target: {}, result: {}", source, target, result);
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy