1 33 package com.icesoft.applications.faces.address; 34 35 import java.util.Iterator ; 36 import java.util.TreeMap ; 37 38 42 public class Matchable { 43 44 private boolean match = true; 45 46 51 public boolean isMatch() { 52 return match; 53 } 54 55 60 public void setMatch(boolean match) { 61 this.match = match; 62 } 63 64 72 public Matchable getClosestMatch(String checkMatch, TreeMap matchSet) { 73 74 Matchable matchable = (Matchable) matchSet.get(checkMatch); 75 if (matchable != null) { 76 matchable.setMatch(true); 78 return matchable; 79 } else { 80 Iterator itor = matchSet.keySet().iterator(); 82 String prevVal = null; 83 String nextVal = null; 84 int thisCompare; 85 int lastCompare; 86 87 while (itor.hasNext()) { 88 nextVal = (String ) (itor.next()); 89 thisCompare = checkMatch.compareTo(nextVal); 90 if (thisCompare < 0) { 91 if (prevVal == null) { 92 matchable = (Matchable) matchSet.get(nextVal); 94 } else { 95 byte nextByte[] = nextVal.getBytes(); 98 byte prevByte[] = prevVal.getBytes(); 99 byte matchByte[] = checkMatch.getBytes(); 100 lastCompare = 0; 101 int nextDiff, prevDiff; 102 try { 103 nextDiff = nextByte[lastCompare] 105 - matchByte[lastCompare]; 106 prevDiff = matchByte[lastCompare] 107 - prevByte[lastCompare]; 108 while (nextDiff == prevDiff) { 109 lastCompare++; 110 nextDiff = nextByte[lastCompare] 111 - matchByte[lastCompare]; 112 prevDiff = matchByte[lastCompare] 113 - prevByte[lastCompare]; 114 } 115 if (nextDiff > prevDiff) { 116 matchable = (Matchable) matchSet 118 .get(prevVal); 119 } else { 120 matchable = (Matchable) matchSet 122 .get(nextVal); 123 } 124 matchable.setMatch(false); 125 return matchable; 126 127 } catch (Exception e) { 128 if (lastCompare >= matchByte.length) { 130 if (prevByte.length <= nextByte.length) { 132 matchable = (Matchable) matchSet 133 .get(prevVal); 134 } else { 135 matchable = (Matchable) matchSet 136 .get(nextVal); 137 } 138 } else if (lastCompare >= nextByte.length) { 139 matchable = (Matchable) matchSet 141 .get(prevVal); 142 } else { 143 matchable = (Matchable) matchSet 144 .get(nextVal); 145 } 146 } 147 } 148 matchable.setMatch(false); 149 return matchable; 150 } 151 prevVal = nextVal; 152 } 153 matchable = (Matchable) matchSet.get(nextVal); 155 matchable.setMatch(false); 156 return matchable; 157 } 158 } 159 } | Popular Tags |