KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > CollectionMatcher


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.jmiimpl.javamodel;
20
21 import java.util.*;
22 import org.netbeans.jmi.javamodel.NamedElement;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.modules.javacore.parser.ElementInfo;
25
26 /**
27  *
28  * @author mm109185
29  */

30 class CollectionMatcher {
31     private Object JavaDoc[] oldArr;
32     private Object JavaDoc[] newArr;
33     private int[] map;
34
35     /** Creates a new instance of CollectionMatcher */
36     CollectionMatcher(Object JavaDoc[] oldElements, Object JavaDoc[] newElements) {
37         oldArr=new Object JavaDoc[oldElements.length];
38         System.arraycopy(oldElements,0,oldArr,0,oldElements.length);
39         newArr=new Object JavaDoc[newElements.length];
40         for(int i=0;i<newElements.length;i++) {
41             MetadataElement el=(MetadataElement)newElements[i];
42             newArr[i]=oldElements instanceof String JavaDoc[] ? ((NamedElement) el).getName() : el.getInternalForm();
43         }
44     }
45     
46     /** @returns indexes to oldElements array. oldIndex=getPositions()[newIndex]
47      * -1 - element was deleted
48      */

49     int[] getPositions() {
50         if (map==null)
51             mapArray();
52         return map;
53     }
54     
55     /** @returns Set of deleted objects
56      */

57     Set getDeleted() {
58         int i;
59         Set deleted=new HashSet();
60
61         if (map==null)
62             mapArray();
63         for (i=0;i<oldArr.length;i++) {
64             Object JavaDoc obj=oldArr[i];
65
66             if (obj!=null)
67                 deleted.add(obj);
68         }
69         return deleted;
70     }
71
72     private final void mapArray() {
73         int i;
74         Map oldMap=new HashMap();
75         
76         for (i=0;i<oldArr.length;i++) {
77             oldMap.put(oldArr[i],new Integer JavaDoc(i));
78         }
79         map=new int[newArr.length];
80         Arrays.fill(map,-1);
81         for (i=0;i<newArr.length;i++) {
82             Object JavaDoc newObj=newArr[i];
83             Integer JavaDoc oldIndex=(Integer JavaDoc)oldMap.get(newObj);
84                 
85             if (oldIndex!=null) {
86                 int index=oldIndex.intValue();
87
88                 map[i]=index;
89                 oldArr[index]=null;
90             }
91         }
92     }
93 }
94
Popular Tags