Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8f88cf2b6e66face26a9def02bc8eddfa251e9aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package org.eclipse.jdt.internal.core.builder.impl;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.internal.core.builder.IPackage;
import java.util.*;

/**
 * The state's package map.
 */
public class PackageMap extends StateTables {
	Hashtable fTable;
	/**
	 * Creates a new package map
	 */
	PackageMap() {
		fTable = new Hashtable(23);
	}
	/**
	 * Returns true if the package exists in the state, otherwise
	 * returns false
	 */
	boolean containsPackage(IPackage pkg) {
		return fTable.containsKey(pkg);
	}
	/**
	 * Creates a copy of the package map.
	 */
	PackageMap copy() {
		try {
			PackageMap copy = (PackageMap) super.clone();
			copy.fTable = (Hashtable) fTable.clone();
			return copy;
		}
		catch (CloneNotSupportedException e) {
			// Should not happen.
			throw new Error();
		}
	}
	/**
	 * Returns an enumeration of all packages in the state.  The enumeration
	 * is of non state-specific package handles.
	 */
	public Enumeration getAllPackages() {
		return fTable.keys();
	}
	/**
	 * Returns all packages in the state.  The result is an array
	 * of non state-specific package handles.
	 */
	public IPackage[] getAllPackagesAsArray() {
		IPackage[] pkgs = new IPackage[fTable.size()];
		int i = 0;
		for (Enumeration e = fTable.keys(); e.hasMoreElements();) {
			pkgs[i++] = (IPackage) e.nextElement();
		}
		return pkgs;
	}
	/**
	 * Returns the package map entry for a given package.
	 * Returns null if the package is not present.
	 */
	PackageMapEntry getEntry(IPackage pkg) {
		return (PackageMapEntry)fTable.get(pkg);
	}
	/**
	 * Returns the package fragments for a given package.  The returned
	 * fragments are sorted according to the class path.
	 * Returns null if the package is not present.
	 */
	IPath[] getFragments(IPackage pkg) {
		PackageMapEntry entry = (PackageMapEntry)fTable.get(pkg);
		return entry == null ? null : entry.getFragments();
	}
	/**
	 * Adds a fragment entry for the given package.
	 * Mutable operation, should only be used when recreating package map.
	 */
	void putFragment(IPackage pkg, IPath frag) {
		PackageMapEntry entry = (PackageMapEntry)fTable.get(pkg);
		if (entry == null) {
			entry = new PackageMapEntry(pkg);
			fTable.put(pkg, entry);
		}
		entry.addFragment(frag);
	}
	/**
	 * Adds an array of fragments for the given package.  Assumes that the
	 * given array of fragments are in classpath order.
	 */
	void putFragments(IPackage pkg, IPath[] frags) {
		PackageMapEntry entry = (PackageMapEntry)fTable.get(pkg);
		if (entry == null) {
			entry = new PackageMapEntry(pkg);
			fTable.put(pkg, entry);
		}
		entry.addFragments(frags);
	}
	/**
	 * Returns the number of packages in the state.  
	 */
	int size() {
		return fTable.size();
	}
	/**
	 * For debugging only.
	 */
	public String toString() {
		IPackage[] pkgs = getAllPackagesAsArray();
		Arrays.sort(pkgs, StateImpl.getPackageComparator());
		StringBuffer sb = new StringBuffer();
		sb.append(super.toString() + ":\n"/*nonNLS*/);
		for (int i = 0; i < pkgs.length; ++i) {
			sb.append("  "/*nonNLS*/ + pkgs[i].getName() + ": "/*nonNLS*/);
			IPath[] fragments = getFragments(pkgs[i]);
			for (int j = 0; j < fragments.length; ++j) {
				if (j != 0) sb.append(", "/*nonNLS*/);
				sb.append(fragments[j]);
			}
			sb.append("\n"/*nonNLS*/);
		}
		return sb.toString();
	}
}

Back to the top