1 19 package org.netbeans.modules.javacore.parser; 20 21 25 public class StringMeasure implements Measure { 26 public static final StringMeasure INSTANCE = new StringMeasure(); 27 private static final int SAME=0; 28 private static final int CASE_SAME=1; 29 private static final int DIFFERENT=10; 30 31 private StringMeasure() { 32 } 33 34 42 public final int getDistance(final Object first, final Object second) { 43 if (first==second) 44 return 0; 45 if (first==null || second==null) 46 return INFINITE_DISTANCE; 47 final String x=(String )first; 48 final String y=(String )second; 49 final int xlen=x.length(); 50 final int ylen=y.length(); 51 int errors=0; 52 int xindex=0,yindex=0; 53 final char xarr[]=new char[xlen+1]; 54 final char yarr[]=new char[ylen+1]; 55 56 x.getChars(0, xlen, xarr, 0); 57 y.getChars(0, ylen, yarr, 0); 58 while(xindex<xlen && yindex<ylen){ 59 final char xchar=xarr[xindex]; 60 final char ychar=yarr[yindex]; 61 final int cherr=compareChars(xchar,ychar); 62 63 if (cherr!=DIFFERENT) { 64 errors+=cherr; 65 xindex++; 66 yindex++; 67 continue; 68 } 69 final char xchar1=xarr[xindex+1]; 70 final char ychar1=yarr[yindex+1]; 71 if (xchar1!=0 && ychar1!=0) { 72 final int cherr1=compareChars(xchar1,ychar1); 73 74 if (cherr1!=DIFFERENT) { 75 errors+=DIFFERENT+cherr1; 76 xindex+=2; 77 yindex+=2; 78 continue; 79 } 80 final int xerr=compareChars(xchar,ychar1); 81 final int xerr1=compareChars(xchar1,ychar); 82 83 if (xerr!=DIFFERENT && xerr1!=DIFFERENT) { 84 errors+=DIFFERENT+xerr+xerr1; 85 xindex+=2; 86 yindex+=2; 87 continue; 88 } 89 } 90 if (xlen-xindex>ylen-yindex) { 91 xindex++; 92 } else if (xlen-xindex<ylen-yindex) { 93 yindex++; 94 } else { 95 xindex++; 96 yindex++; 97 } 98 errors+=DIFFERENT; 99 } 100 errors+=(xlen-xindex+ylen-yindex)*DIFFERENT; 101 return (INFINITE_DISTANCE*errors)/Math.max(ylen,xlen)/DIFFERENT; 102 } 103 104 private static final int compareChars(final char xc,final char yc) { 105 if (xc==yc) 106 return SAME; 107 char xlower = Character.toLowerCase(xc); 108 char ylower = Character.toLowerCase(yc); 109 if (xlower==ylower) { 110 return CASE_SAME; 111 } 112 return DIFFERENT; 113 } 114 } 115 | Popular Tags |