1 19 package org.netbeans.modules.javacore.parser; 20 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 30 class ArrayMeasure implements Measure { 31 private final Measure measure; 32 33 ArrayMeasure(Measure elementsMeasure) { 34 measure = elementsMeasure; 35 } 36 37 public int getDistance(Object first, Object second) { 38 Object [] firstArr=(Object [])first; 39 Object [] secondArr=(Object [])second; 40 int maxLength=Math.max(firstArr.length, secondArr.length); 41 42 if (maxLength!=0) { 43 List array1 = new ArrayList (Arrays.asList(firstArr)); 44 List array2 = new ArrayList (Arrays.asList(secondArr)); 45 int minDistance = INFINITE_DISTANCE; 46 Object minDistanceElement = null; 47 int error = 0; 48 49 for (Iterator it1 = array1.iterator(); it1.hasNext();) { 50 Object element1 = it1.next(); 51 for (Iterator it2 = array2.iterator(); it2.hasNext();) { 52 Object element2 = it2.next(); 53 int distance = measure.getDistance(element1, element2); 54 if (distance == 0) { 55 it1.remove(); 56 it2.remove(); 57 break; 58 } 59 } 60 } 61 for (Iterator it1 = array1.iterator(); it1.hasNext();) { 62 Object element1 = it1.next(); 63 for (Iterator it2 = array2.iterator(); it2.hasNext();) { 64 Object element2 = it2.next(); 65 int distance = measure.getDistance(element1, element2); 66 if (distance < minDistance) { 67 minDistance = distance; 68 minDistanceElement = element2; 69 } 70 } 71 if (minDistanceElement != null) { 72 array2.remove(minDistanceElement); 73 it1.remove(); 74 minDistanceElement = null; 75 error += minDistance; 76 } 77 } 78 error += INFINITE_DISTANCE * Math.max(array1.size(), array2.size()); 79 error = 2 * error / maxLength; 80 return error > INFINITE_DISTANCE ? INFINITE_DISTANCE : error; 81 } 82 return 0; 83 } 84 } 85 | Popular Tags |