]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/correction/NameMatcher.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / correction / NameMatcher.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12
13 package org.eclipse.jdt.internal.ui.text.correction;
14
15 public class NameMatcher {
16
17         /**
18          * Returns a similarity value of the two names.
19          * The range of is from 0 to 256. no similarity is negative
20          * @param name1 the first name
21          * @param name2 the second name
22          * @return <code>true</code> is returned if the names are similar
23          */
24         public static boolean isSimilarName(String name1, String name2) {
25                 return getSimilarity(name1, name2) >= 0;
26         }
27
28         /**
29          * Returns a similarity value of the two names.
30          * The range of is from 0 to 256. no similarity is negative
31          * @param name1 the first name
32          * @param name2 the second name
33          * @return the similarity valuer
34          */
35         public static int getSimilarity(String name1, String name2) {
36                 if (name1.length() > name2.length()) {
37                         String tmp= name1;
38                         name1= name2;
39                         name2= tmp;
40                 }
41                 int name1len= name1.length();
42                 int name2len= name2.length();
43
44                 int nMatched= 0;
45
46                 int i= 0;
47                 while (i < name1len && isSimilarChar(name1.charAt(i), name2.charAt(i))) {
48                         i++;
49                         nMatched++;
50                 }
51
52                 int k= name1len;
53                 int diff= name2len - name1len;
54                 while (k > i && isSimilarChar(name1.charAt(k - 1), name2.charAt(k + diff - 1))) {
55                         k--;
56                         nMatched++;
57                 }
58
59                 if (nMatched == name2len) {
60                         return 200;
61                 }
62
63                 if (name2len - nMatched > nMatched) {
64                         return -1;
65                 }
66
67                 int tolerance= name2len / 4 + 1;
68                 return (tolerance - (k - i)) * 256 / tolerance;
69         }
70
71         private static boolean isSimilarChar(char ch1, char ch2) {
72                 return Character.toLowerCase(ch1) == Character.toLowerCase(ch2);
73         }
74
75 }