1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.corext.refactoring.util;
15 
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 
23 import org.eclipse.core.resources.IFile;
24 
25 import org.eclipse.ltk.core.refactoring.Change;
26 import org.eclipse.ltk.core.refactoring.CompositeChange;
27 import org.eclipse.ltk.core.refactoring.TextChange;
28 import org.eclipse.ltk.core.refactoring.TextFileChange;
29 
30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
31 
32 public class QualifiedNameSearchResult {
33 
34 	private Map<IFile, TextChange> fChanges;
35 
QualifiedNameSearchResult()36 	public QualifiedNameSearchResult() {
37 		fChanges= new HashMap<>();
38 	}
39 
getChange(IFile file)40 	public TextChange getChange(IFile file) {
41 		TextChange result= fChanges.get(file);
42 		if (result == null) {
43 			result= new TextFileChange(file.getName(), file);
44 			fChanges.put(file, result);
45 		}
46 		return result;
47 	}
48 
getAllChanges()49 	public TextChange[] getAllChanges() {
50 		Collection<TextChange> values= fChanges.values();
51 		return values.toArray(new TextChange[values.size()]);
52 	}
53 
getAllFiles()54 	public IFile[] getAllFiles() {
55 		Set<IFile> keys= fChanges.keySet();
56 		return keys.toArray(new IFile[keys.size()]);
57 	}
58 
getSingleChange(IFile[] alreadyTouchedFiles)59 	public Change getSingleChange(IFile[] alreadyTouchedFiles) {
60 		Collection<TextChange> values= fChanges.values();
61 		if (values.isEmpty())
62 			return null;
63 
64 		CompositeChange result= new CompositeChange(RefactoringCoreMessages.QualifiedNameSearchResult_change_name);
65 		result.markAsSynthetic();
66 		List<IFile> files= Arrays.asList(alreadyTouchedFiles);
67 		for (TextChange textChange : values) {
68 			TextFileChange change= (TextFileChange)textChange;
69 			if (!files.contains(change.getFile())) {
70 				result.add(change);
71 			}
72 		}
73 		return result;
74 	}
75 }
76