src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/EditDistance.java

changeset 397
b99d7e355d4b
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
393:6cdc6ed98780 397:b99d7e355d4b
23 * questions. 23 * questions.
24 */ 24 */
25 25
26 package com.sun.xml.internal.bind.v2.util; 26 package com.sun.xml.internal.bind.v2.util;
27 27
28 import java.util.AbstractMap;
29 import java.util.Arrays;
28 import java.util.Collection; 30 import java.util.Collection;
29 import java.util.Arrays; 31 import java.util.WeakHashMap;
30 32
31 /** 33 /**
32 * Computes the string edit distance. 34 * Computes the string edit distance.
33 * 35 *
34 * <p> 36 * <p>
39 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 41 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
40 */ 42 */
41 public class EditDistance { 43 public class EditDistance {
42 44
43 /** 45 /**
46 * Weak results cache to avoid additional computations.
47 * Because of high complexity caching is required.
48 */
49 private static final WeakHashMap<AbstractMap.SimpleEntry<String,String>, Integer> CACHE = new WeakHashMap<AbstractMap.SimpleEntry<String, String>, Integer>();
50
51 /**
44 * Computes the edit distance between two strings. 52 * Computes the edit distance between two strings.
45 * 53 *
46 * <p> 54 * <p>
47 * The complexity is O(nm) where n=a.length() and m=b.length(). 55 * The complexity is O(nm) where n=a.length() and m=b.length().
48 */ 56 */
49 public static int editDistance( String a, String b ) { 57 public static int editDistance( String a, String b ) {
50 return new EditDistance(a,b).calc(); 58 // let's check cache
59 AbstractMap.SimpleEntry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(a, b); // using this class to avoid creation of my own which will handle PAIR of values
60 Integer result = null;
61 if (CACHE.containsKey(entry))
62 result = CACHE.get(entry); // looks like we have it
63
64 if (result == null) {
65 result = new EditDistance(a, b).calc();
66 CACHE.put(entry, result); // cache the result
67 }
68 return result;
51 } 69 }
52 70
53 /** 71 /**
54 * Finds the string in the <code>group</code> closest to 72 * Finds the string in the <code>group</code> closest to
55 * <code>key</code> and returns it. 73 * <code>key</code> and returns it.

mercurial