KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * ArrayMapper.java
21  *
22  * Created on January 6, 2003, 3:27 PM
23  */

24
25 package org.netbeans.modules.javacore.parser;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31 import org.netbeans.jmi.javamodel.*;
32 import org.openide.ErrorManager;
33
34 /**
35  *
36  * @author Tomas Hurka
37  */

38 public class ArrayMapper {
39     private Object JavaDoc[] oldArr;
40     private Object JavaDoc[] newArr;
41     private int[] map;
42     private List JavaDoc deleted;
43     private List JavaDoc added;
44     private boolean identical;
45
46     public ArrayMapper(Object JavaDoc[] o,Object JavaDoc[] n) {
47         oldArr=new Object JavaDoc[o.length];
48         System.arraycopy(o,0,oldArr,0,o.length);
49         newArr=n;
50     }
51     
52     public int[] getMap() {
53         if (map==null)
54             mapArray();
55         return map;
56     }
57     
58     public boolean isIdentical() {
59         if (map==null)
60             mapArray();
61         return identical;
62     }
63     
64     public List JavaDoc getDeletedIndexes() {
65         if (deleted==null) {
66             int i;
67             
68             if (map==null)
69                 mapArray();
70             deleted=new ArrayList JavaDoc();
71             for (i=0;i<oldArr.length;i++) {
72                 Object JavaDoc obj=oldArr[i];
73                 
74                 if (obj!=null)
75                     deleted.add(new Integer JavaDoc(i));
76             }
77         }
78         return deleted;
79     }
80     
81     List JavaDoc getAdded() {
82         if (added==null) {
83             int i;
84             
85             if (map==null)
86                 mapArray();
87             added=new ArrayList JavaDoc();
88             for (i=0;i<map.length;i++) {
89                 if (map[i]==-1)
90                     added.add(newArr[i]);
91             }
92         }
93         return added;
94     }
95     
96     private class DistanceInfo implements Comparable JavaDoc {
97         private final int newIndex,oldIndex,distance;
98         
99         private DistanceInfo(final int newI, final int oldI, final int dis) {
100             newIndex=newI;
101             oldIndex=oldI;
102             distance=dis;
103         }
104         
105         public final int compareTo(Object JavaDoc obj) {
106             return distance-((DistanceInfo)obj).distance;
107         }
108     }
109     
110     private final void mapArray() {
111         List JavaDoc distances=new ArrayList JavaDoc(oldArr.length*2);
112         int i,mapped=0,identicalObjects=0;
113         
114         map=new int[newArr.length];
115         Arrays.fill(map,-1);
116         for (i=0;i<newArr.length;i++) {
117             Object JavaDoc newObj=newArr[i];
118             int j;
119             List JavaDoc currentDistances=new ArrayList JavaDoc(oldArr.length/2);
120             
121             for (j=0;j<oldArr.length;j++) {
122                 int oldIndex=(i+j)%oldArr.length;
123                 Object JavaDoc oldObj=oldArr[oldIndex];
124                 
125                 if (oldObj!=null) {
126                     int dis=getDistance(oldObj,newObj);
127                     
128                     if (dis==0) {
129                         if (i==oldIndex)
130                             identicalObjects++;
131                         map[i]=oldIndex;
132                         oldArr[oldIndex]=null;
133                         mapped++;
134                         break;
135                     }
136                     if (dis<Measure.INFINITE_DISTANCE)
137                         currentDistances.add(new DistanceInfo(i,oldIndex,dis));
138                 }
139             }
140             if (j==oldArr.length)
141                 distances.addAll(currentDistances);
142         }
143         if (mapped!=oldArr.length) {
144             DistanceInfo[] sortedDistances=new DistanceInfo[distances.size()];
145             int j;
146             
147             Collections.sort(distances);
148             sortedDistances=(DistanceInfo[])distances.toArray(sortedDistances);
149             for (j=0;j<sortedDistances.length;j++) {
150                 DistanceInfo info=sortedDistances[j];
151                 int oldIndex=info.oldIndex;
152                 
153                 if (oldArr[oldIndex]!=null && map[info.newIndex]==-1) {
154                     if (info.newIndex==oldIndex)
155                         identicalObjects++;
156                     map[info.newIndex]=oldIndex;
157                     oldArr[oldIndex]=null;
158                     if (++mapped==oldArr.length)
159                         break;
160                 }
161             }
162         }
163         identical=(identicalObjects==oldArr.length && oldArr.length==newArr.length);
164     }
165     
166     private static int getDistance(Object JavaDoc mdr,Object JavaDoc ast) {
167         Measure measure=null;
168         
169         if (mdr instanceof EnumConstant)
170             measure=EnumConstInfoMeasure.INSTANCE;
171         else if (mdr instanceof Initializer)
172             measure=InitializerInfoMeasure.INSTANCE;
173         else if (mdr instanceof Import)
174             measure=ImportInfoMeasure.INSTANCE;
175         else if (mdr instanceof Parameter)
176             measure=ParameterInfoMeasure.INSTANCE;
177         else if (mdr instanceof FieldGroup)
178             measure=FieldGroupInfoMeasure.INSTANCE;
179         else if (mdr instanceof TypeParameter)
180             measure=TypeParamInfoMeasure.INSTANCE;
181         else if (mdr instanceof Field)
182             measure=FieldInfoMeasure.INSTANCE;
183         else if (mdr instanceof Method)
184             measure=MethodInfoMeasure.INSTANCE;
185         else if (mdr instanceof Constructor)
186             measure=ConstructorInfoMeasure.INSTANCE;
187         else if (mdr instanceof JavaEnum)
188             measure=EnumInfoMeasure.INSTANCE;
189         else if (mdr instanceof AnnotationType)
190             measure=AnnotationTypeInfoMeasure.INSTANCE;
191         else if (mdr instanceof Annotation)
192             measure=AnnotationInfoMeasure.INSTANCE;
193         else if (mdr instanceof Attribute)
194             measure=AttributeInfoMeasure.INSTANCE;
195         else if (mdr instanceof JavaClass)
196             measure=ClassInfoMeasure.INSTANCE;
197         else if (mdr instanceof AttributeValue)
198             measure=AnnotationValueInfoMeasure.INSTANCE;
199         else {
200             ErrorManager.getDefault().log(ErrorManager.EXCEPTION,"Unknown type "+mdr.getClass());
201             return Measure.INFINITE_DISTANCE;
202         }
203         return measure.getDistance(mdr,ast);
204     }
205
206     public boolean hasPermutation() {
207         int i;
208         int val=-1;
209         final int map[]=getMap();
210         
211         for (i=0;i<map.length;i++) {
212             final int mp=map[i];
213             
214             if (mp!=-1) {
215                 if (mp>val)
216                     val=mp;
217                 else
218                     return true;
219             }
220         }
221         return false;
222     }
223 }
224
225
Popular Tags