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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
13 package org.eclipse.jdt.internal.ui.text.correction;
15 public class NameMatcher {
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
24 public static boolean isSimilarName(String name1, String name2) {
25 return getSimilarity(name1, name2) >= 0;
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
35 public static int getSimilarity(String name1, String name2) {
36 if (name1.length() > name2.length()) {
41 int name1len= name1.length();
42 int name2len= name2.length();
47 while (i < name1len && isSimilarChar(name1.charAt(i), name2.charAt(i))) {
53 int diff= name2len - name1len;
54 while (k > i && isSimilarChar(name1.charAt(k - 1), name2.charAt(k + diff - 1))) {
59 if (nMatched == name2len) {
63 if (name2len - nMatched > nMatched) {
67 int tolerance= name2len / 4 + 1;
68 return (tolerance - (k - i)) * 256 / tolerance;
71 private static boolean isSimilarChar(char ch1, char ch2) {
72 return Character.toLowerCase(ch1) == Character.toLowerCase(ch2);