1 11 package org.eclipse.jdt.internal.ui.browsing; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 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 29 public class LogicalPackage extends PlatformObject { 30 31 private Set fPackages; 32 private String fName; 33 private IJavaProject fJavaProject; 34 35 public LogicalPackage(IPackageFragment fragment){ 36 Assert.isNotNull(fragment); 37 fPackages= new HashSet (); 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 getElementName(){ 66 return fName; 67 } 68 69 public int size(){ 70 return fPackages.size(); 71 } 72 73 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 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 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 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 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 |