Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7caeed00aee654dda71f794c5f6a0eb854e844c8 (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 Wind River Systems, Inc. and others.
 * 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: 
 * Markus Schorn - initial API and implementation 
 ******************************************************************************/ 
package org.eclipse.cdt.internal.ui.refactoring.rename;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;


public class CRefactoringMatchStore {
    private Map<IFile, IPath> fFileToPathMap= new HashMap<IFile, IPath>();
    private Map<IPath, SortedMap<CRefactoringMatch, CRefactoringMatch>> fPathToMatches= new HashMap<IPath, SortedMap<CRefactoringMatch, CRefactoringMatch>>();
    private Comparator<CRefactoringMatch> fOffsetComparator;

    public CRefactoringMatchStore() {
        fOffsetComparator= new Comparator<CRefactoringMatch>() {
            public int compare(CRefactoringMatch o1, CRefactoringMatch o2) {
                return o1.getOffset() - o2.getOffset();
            }
        };
    }
    
    public void addMatch(CRefactoringMatch match) {
        IPath path= resolvePath(match.getFile());
        if (path != null) {
            Map<CRefactoringMatch, CRefactoringMatch> matchesForPath= getMapForPath(path, true);
            matchesForPath.put(match, match);
        }
    }
        
    private Map<CRefactoringMatch, CRefactoringMatch> getMapForPath(IPath path, boolean create) {
        SortedMap<CRefactoringMatch, CRefactoringMatch> map= fPathToMatches.get(path);
        if (map == null && create) {
            map= new TreeMap<CRefactoringMatch, CRefactoringMatch>(fOffsetComparator);
            fPathToMatches.put(path, map);
        }
        return map;
    }

    private IPath resolvePath(IFile file) {
        IPath path= fFileToPathMap.get(file);
        if (path == null) {
            path= file.getLocation();
            if (path == null) {
                path= file.getFullPath();
            }
            fFileToPathMap.put(file, path);
        }
        return path;
    }

    public int getFileCount() {
        return fFileToPathMap.size();
    }

    public List<IFile> getFileList() {
        return new ArrayList<IFile>(fFileToPathMap.keySet());
    }

    public boolean contains(IResource file) {
        return fFileToPathMap.containsKey(file);
    }

    public Collection<CRefactoringMatch> getMatchesForFile(IResource file) {
        return getMatchesForPath(fFileToPathMap.get(file));
    }

    public Collection<CRefactoringMatch> getMatchesForPath(IPath path) {
        if (path != null) {
            Map<CRefactoringMatch, CRefactoringMatch> map= fPathToMatches.get(path);
            if (map != null) {
                return map.keySet();
            }
        }
        return Collections.emptySet();
    }

    public CRefactoringMatch findMatch(IPath path, int nodeOffset) {
        Map<CRefactoringMatch, CRefactoringMatch> map= fPathToMatches.get(path);
        if (map != null) {
            return map.get(new CRefactoringMatch(null, nodeOffset, 0, 0));
        }
        return null;
    }

    public void removePath(IPath path) {
        Map<CRefactoringMatch, CRefactoringMatch> map= fPathToMatches.remove(path);
        if (map != null && !map.isEmpty()) {
            IFile file= (map.values().iterator().next()).getFile();
            fFileToPathMap.remove(file);
        }
    }

    public Collection<CRefactoringMatch> findMatchesInRange(Path path, int offset, int end) {
        if (path != null) {
            SortedMap<CRefactoringMatch, CRefactoringMatch> map= fPathToMatches.get(path);
            if (map != null) {
                return map.subMap(new CRefactoringMatch(null, offset, 0, 0),
                        new CRefactoringMatch(null, end, 0, 0)).keySet();
            }
        }
        return Collections.emptySet();
    }
}

Back to the top