KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > browsing > LogicalPackage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.browsing;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.PlatformObject;
19
20
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.JavaModelException;
24
25 /**
26  * Contains a list of package fragments with the same name
27  * but residing in different source folders of a unique Java project.
28  */

29 public class LogicalPackage extends PlatformObject {
30
31     private Set JavaDoc fPackages;
32     private String JavaDoc fName;
33     private IJavaProject fJavaProject;
34
35     public LogicalPackage(IPackageFragment fragment){
36         Assert.isNotNull(fragment);
37         fPackages= new HashSet JavaDoc();
38         fJavaProject= fragment.getJavaProject();
39         Assert.isNotNull(fJavaProject);
40         add(fragment);
41         fName= fragment.getElementName();
42     }
43
44     public IJavaProject getJavaProject(){
45         return fJavaProject;
46     }
47
48     public IPackageFragment[] getFragments(){
49         return (IPackageFragment[]) fPackages.toArray(new IPackageFragment[fPackages.size()]);
50     }
51
52     public void add(IPackageFragment fragment){
53         Assert.isTrue(fragment != null && fJavaProject.equals(fragment.getJavaProject()));
54         fPackages.add(fragment);
55     }
56
57     public void remove(IPackageFragment fragment){
58         fPackages.remove(fragment);
59     }
60
61     public boolean contains(IPackageFragment fragment){
62         return fPackages.contains(fragment);
63     }
64
65     public String JavaDoc getElementName(){
66         return fName;
67     }
68
69     public int size(){
70         return fPackages.size();
71     }
72
73     /**
74      * Returns true if the given fragment has the same name and
75      * resides inside the same project as the other fragments in
76      * the LogicalPackage.
77      *
78      * @param fragment
79      * @return boolean
80      */

81     public boolean belongs(IPackageFragment fragment) {
82
83         if(fragment==null)
84             return false;
85
86         if(fJavaProject.equals(fragment.getJavaProject())){
87             return fName.equals(fragment.getElementName());
88         }
89
90         return false;
91     }
92     
93     public boolean hasSubpackages() throws JavaModelException {
94         for (Iterator JavaDoc iter= fPackages.iterator(); iter.hasNext();) {
95             IPackageFragment pack= (IPackageFragment) iter.next();
96             if (pack.hasSubpackages()) {
97                 return true;
98             }
99         }
100         return false;
101     }
102     
103     public boolean isDefaultPackage() {
104         return fName.length() == 0;
105     }
106
107     public boolean equals(Object JavaDoc o){
108         if (!(o instanceof LogicalPackage))
109             return false;
110
111         LogicalPackage lp= (LogicalPackage)o;
112         if (!fJavaProject.equals(lp.getJavaProject()))
113             return false;
114
115         IPackageFragment[] fragments= lp.getFragments();
116
117         if (fragments.length != getFragments().length)
118             return false;
119
120         //this works because a LogicalPackage cannot contain the same IPackageFragment twice
121
for (int i= 0; i < fragments.length; i++) {
122             IPackageFragment fragment= fragments[i];
123             if(!fPackages.contains(fragment))
124                 return false;
125         }
126
127         return true;
128     }
129
130     /* (non-Javadoc)
131      * @see java.lang.Object#hashCode()
132      */

133     public int hashCode() {
134         IPackageFragment[] fragments= getFragments();
135         return fJavaProject.hashCode() + getHash(fragments, fragments.length-1);
136     }
137
138     private int getHash(IPackageFragment[] fragments, int index) {
139         if (index <= 0)
140             return fragments[0].hashCode() * 17;
141         else return fragments[index].hashCode() * 17 + getHash(fragments, index-1);
142     }
143 }
144
Popular Tags