From eb661d336c3eea65da0c67adf104a37f402359bc Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 18 Jul 2011 16:14:34 -0400 Subject: [PATCH] INT-1969 simplified PayloadTypeRouter code --- .../integration/router/PayloadTypeRouter.java | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/router/PayloadTypeRouter.java b/spring-integration-core/src/main/java/org/springframework/integration/router/PayloadTypeRouter.java index bb787abed3..48aa1a1066 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/router/PayloadTypeRouter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/router/PayloadTypeRouter.java @@ -18,9 +18,7 @@ package org.springframework.integration.router; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; @@ -66,10 +64,7 @@ public class PayloadTypeRouter extends AbstractMessageRouter { private String findClosestMatch(Class type, boolean isArray) { int minTypeDiffWeight = Integer.MAX_VALUE; - String closestMatch = null; - int ambiguityAtClosestMatchLevel = 0; - Map> weightedMatches = new HashMap>(); - + List matches = new ArrayList(); for (String candidate : this.channelIdentifierMap.keySet()) { if (isArray) { if (!candidate.endsWith(ARRAY_SUFFIX)) { @@ -81,41 +76,34 @@ public class PayloadTypeRouter extends AbstractMessageRouter { else if (candidate.endsWith(ARRAY_SUFFIX)) { continue; } - int typeDiffWeight = determineTypeDifferenceWeight(candidate, type, 0); - List weightedMatch = weightedMatches.get(typeDiffWeight); - if (weightedMatch == null){ - weightedMatch = new ArrayList(); - weightedMatches.put(typeDiffWeight, weightedMatch); - } - weightedMatch.add(candidate); - if (typeDiffWeight < minTypeDiffWeight) { minTypeDiffWeight = typeDiffWeight; - closestMatch = (isArray) ? candidate + ARRAY_SUFFIX : candidate; - ambiguityAtClosestMatchLevel = 0; + // new winner, start accumulating matches from scratch + matches.clear(); + matches.add((isArray) ? candidate + ARRAY_SUFFIX : candidate); } else if (typeDiffWeight == minTypeDiffWeight && typeDiffWeight != Integer.MAX_VALUE) { - ambiguityAtClosestMatchLevel = typeDiffWeight; + // candidate tied with current winner, keep track + matches.add(candidate); } } - if (ambiguityAtClosestMatchLevel > 0) { + if (matches.size() > 1) { // ambiguity throw new IllegalStateException( - "Unresolvable ambiguity while attempting to find closest match for [" + type.getName() + "]. Found: " + weightedMatches.get(ambiguityAtClosestMatchLevel)); + "Unresolvable ambiguity while attempting to find closest match for [" + type.getName() + "]. Found: " + matches); } - if (closestMatch == null) { + if (CollectionUtils.isEmpty(matches)) { // no match return null; } - return this.channelIdentifierMap.get(closestMatch); + // we have a winner + return this.channelIdentifierMap.get(matches.get(0)); } private int determineTypeDifferenceWeight(String candidate, Class type, int level) { if (type.getName().equals(candidate)) { return level; } - for (Class iface : type.getInterfaces()) { - if (iface.getName().equals(candidate)) { return (level % 2 == 1) ? level + 2 : level + 1; } @@ -133,4 +121,5 @@ public class PayloadTypeRouter extends AbstractMessageRouter { } return this.determineTypeDifferenceWeight(candidate, type.getSuperclass(), level + 2); } + }