com.gw.common.utils.YmTopicHandlerMapping Maven / Gradle / Ivy
The newest version!
package com.gw.common.utils;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class YmTopicHandlerMapping {
protected Logger logger =LoggerFactory.getLogger(getClass());
@Autowired
private Map hashtable;
public YmTopicHandlerMapping() {
}
public ContentHandler get(String topic) {
ContentHandler handler = this.hashtable.get(topic);
if (handler == null) {
for (String item : this.hashtable.keySet()) {
if (compareTopic(item, topic) >= 0) {
handler = this.hashtable.get(item);
break;
}
}
}
return handler;
}
public static int compareTopic(String srcTopic, String destTopic) {
if (srcTopic.equals(destTopic)) {
return 0;
}
if ("**".equals(srcTopic)) {
return 1;
}
if ("**".equals(destTopic)) {
return -1;
}
String[] strs1 = srcTopic.split("\\.");
String[] strs2 = destTopic.split("\\.");
int result = -2;
for (int i = 0; i < strs1.length && i < strs2.length; i++) {
String sub1 = strs1[i];
String sub2 = strs2[i];
if (sub1.equals(sub2)) {
result = 0;
} else if ("*".equals(sub1) || "**".equals(sub1)) {
result = 1;
} else if ("*".equals(sub2) || "**".equals(sub2)) {
result = -1;
} else {
result = -2;
}
if (result != 0) {
break;
}
}
if (result == 0) {
if (strs1.length > strs2.length) {
String last = strs1[strs2.length];
if ("*".equals(last) || "**".equals(last)) {
result = 1;
} else {
result = -2;
}
} else if (strs1.length < strs2.length) {
String last = strs2[strs1.length];
if ("*".equals(last) || "**".equals(last)) {
result = -1;
} else {
result = -2;
}
}
}
return result;
}
}