KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  *
28  * @author Martin Matula
29  */

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 JavaDoc first, Object JavaDoc second) {
38         Object JavaDoc[] firstArr=(Object JavaDoc[])first;
39         Object JavaDoc[] secondArr=(Object JavaDoc[])second;
40         int maxLength=Math.max(firstArr.length, secondArr.length);
41         
42         if (maxLength!=0) {
43             List JavaDoc array1 = new ArrayList JavaDoc(Arrays.asList(firstArr));
44             List JavaDoc array2 = new ArrayList JavaDoc(Arrays.asList(secondArr));
45             int minDistance = INFINITE_DISTANCE;
46             Object JavaDoc minDistanceElement = null;
47             int error = 0;
48             
49             for (Iterator JavaDoc it1 = array1.iterator(); it1.hasNext();) {
50                 Object JavaDoc element1 = it1.next();
51                 for (Iterator JavaDoc it2 = array2.iterator(); it2.hasNext();) {
52                     Object JavaDoc 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 JavaDoc it1 = array1.iterator(); it1.hasNext();) {
62                 Object JavaDoc element1 = it1.next();
63                 for (Iterator JavaDoc it2 = array2.iterator(); it2.hasNext();) {
64                     Object JavaDoc 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