KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > StringMeasure


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.parser;
20
21 /**
22  *
23  * @author Tomas Hurka
24  */

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     /** this method implements metrics on Strings.
35      * @param x first string
36      * @param y second string
37      * @return returns int value between 0 and 100.
38      * Where 0 means the strings are
39      * identical, 100 strings are completly
40      * different.
41      */

42     public final int getDistance(final Object JavaDoc first, final Object JavaDoc second) {
43         if (first==second)
44             return 0;
45         if (first==null || second==null)
46             return INFINITE_DISTANCE;
47         final String JavaDoc x=(String JavaDoc)first;
48         final String JavaDoc y=(String JavaDoc)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