Skip to main content
summaryrefslogtreecommitdiffstats
blob: ed139038b4a636079533a86fc051b47a7ec69880 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
* Copyright (c) 2007 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    Robert Fuhrer (rfuhrer@watson.ibm.com) - initial API and implementation

*******************************************************************************/

package org.eclipse.imp.builder;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;

/**
 * Tracks dependencies among compilation units in a given project.<br>
 * @author rfuhrer@watson.ibm.com
 */
public class DependencyInfo {
    protected final Map<String /*unitPath*/, Set<String /*unitPath*/>> fDependsUpon= new HashMap<String,Set<String>>();
    protected final Map<String /*unitPath*/, Set<String /*unitPath*/>> fIsDependedUponBy= new HashMap<String,Set<String>>();
    protected final IProject fProject;
    protected final String fWorkspacePath;

    public DependencyInfo(IProject project) {
        fProject= project;
        fWorkspacePath= fProject.getProject().getWorkspace().getRoot().getLocation().toString();
    }

    protected Set<String /*unitPath*/> getEntry(Map<String /*unitPath*/, Set<String /*unitPath*/>> map, String unitPath) {
        Set<String> result;

        if (!map.containsKey(unitPath))
            result= Collections.emptySet();
        else
            result= map.get(unitPath);
        return result;
    }

    protected Set<String> getOrCreateEntry(Map<String,Set<String>> map, String unitPath) {
        Set<String> result;

        if (!map.containsKey(unitPath))
            map.put(unitPath, result= new HashSet<String>());
        else
            result= (Set<String>) map.get(unitPath);
        return result;
    }

    /**
     * Records a dependency between the two compilation units.
     * @param fromPath a compilation unit path; should be workspace-relative
     * @param uponPath a compilation unit path; should be workspace-relative
     */
    public void addDependency(String fromPath, String uponPath) {
        Set<String> fwdEntry= getOrCreateEntry(fDependsUpon, fromPath);

        fwdEntry.add(uponPath);

        Set<String> bkwdEntry= getOrCreateEntry(fIsDependedUponBy, uponPath);

        bkwdEntry.add(fromPath);
    }

    /**
     * Clears the memory of all dependencies among all compilation units tracked by
     * this DependencyInfo instance.
     */
    public void clearAllDependencies() {
        fDependsUpon.clear();
        fIsDependedUponBy.clear();
    }

    /**
     * Clears any dependencies related to the given compilation unit
     * @param unitPath should be workspace-relative
     */
    public void clearDependenciesOf(String unitPath) {
        Set<String> entry= getEntry(fDependsUpon, unitPath);

        fDependsUpon.put(unitPath, new HashSet<String>());
        for(Iterator<String> iter= entry.iterator(); iter.hasNext(); ) {
            String uponPath= (String) iter.next();
            Set<String> uponSet= getEntry(fIsDependedUponBy, uponPath);

            uponSet.remove(unitPath);
        }
    }

    /**
     * @return a Map from workspace-relative unit paths to Sets of dependent
     * workspace-relative unit paths
     */
    public Map<String /*path*/, Set<String /*path*/>> getDependencies() {
        return Collections.unmodifiableMap(fDependsUpon);
    }

    /**
     * @param unitPath should be workspace-relative
     * @return a Set of dependent workspace--relative unit paths
     */
    public Set<String /*path*/> getDependentsOf(String unitPath) {
        return (Set) fIsDependedUponBy.get(unitPath);
    }

    public void dump() {
        System.out.print(toString());
//        System.out.println("*** Dependencies ***:");
//        for(Iterator<String> iter= fDependsUpon.keySet().iterator(); iter.hasNext(); ) {
//            String unit= iter.next();
//            Set<String /*path*/> dependents= (Set) fDependsUpon.get(unit);
//    
//            System.out.println("Unit " + unit + ": ");
//            for(Iterator<String> iterator= dependents.iterator(); iterator.hasNext(); ) {
//                String uponUnit= iterator.next();
//                System.out.print("  ");
//                System.out.print(uponUnit);
//                if (iterator.hasNext()) System.out.print(", ");
//            }
//            System.out.println();
//        }
    }

    @Override
    public String toString() {
        StringBuilder sb= new StringBuilder();
        sb.append("*** Dependencies ***:\n");
        for(Iterator<String> iter= fDependsUpon.keySet().iterator(); iter.hasNext(); ) {
            String unit= iter.next();
            Set<String /*path*/> dependents= (Set) fDependsUpon.get(unit);
    
            sb.append("Unit " + unit + ": \n");
            for(Iterator<String> iterator= dependents.iterator(); iterator.hasNext(); ) {
                String uponUnit= iterator.next();
                sb.append("  ");
                sb.append(uponUnit);
                if (iterator.hasNext()) sb.append(", ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}

Back to the top