1 19 24 25 package org.netbeans.modules.javacore.parser; 26 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collections ; 30 import java.util.List ; 31 import org.netbeans.jmi.javamodel.*; 32 import org.openide.ErrorManager; 33 34 38 public class ArrayMapper { 39 private Object [] oldArr; 40 private Object [] newArr; 41 private int[] map; 42 private List deleted; 43 private List added; 44 private boolean identical; 45 46 public ArrayMapper(Object [] o,Object [] n) { 47 oldArr=new Object [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 getDeletedIndexes() { 65 if (deleted==null) { 66 int i; 67 68 if (map==null) 69 mapArray(); 70 deleted=new ArrayList (); 71 for (i=0;i<oldArr.length;i++) { 72 Object obj=oldArr[i]; 73 74 if (obj!=null) 75 deleted.add(new Integer (i)); 76 } 77 } 78 return deleted; 79 } 80 81 List getAdded() { 82 if (added==null) { 83 int i; 84 85 if (map==null) 86 mapArray(); 87 added=new ArrayList (); 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 { 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 obj) { 106 return distance-((DistanceInfo)obj).distance; 107 } 108 } 109 110 private final void mapArray() { 111 List distances=new ArrayList (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 newObj=newArr[i]; 118 int j; 119 List currentDistances=new ArrayList (oldArr.length/2); 120 121 for (j=0;j<oldArr.length;j++) { 122 int oldIndex=(i+j)%oldArr.length; 123 Object 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 mdr,Object 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 |