]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/fix/AbstractMultiFix.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / fix / AbstractMultiFix.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.fix;
12
13 import java.util.ArrayList;
14 import java.util.Map;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.jdt.core.compiler.IProblem;
19 import org.eclipse.jdt.core.dom.CompilationUnit;
20
21 import org.eclipse.jdt.ui.cleanup.CleanUpContext;
22 import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
23 import org.eclipse.jdt.ui.text.java.IProblemLocation;
24
25 import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation;
26
27
28 public abstract class AbstractMultiFix extends AbstractCleanUp implements IMultiFix {
29
30         protected AbstractMultiFix() {
31         }
32
33         protected AbstractMultiFix(Map<String, String> settings) {
34                 super(settings);
35         }
36
37         @Override
38         public final ICleanUpFix createFix(CleanUpContext context) throws CoreException {
39                 CompilationUnit unit= context.getAST();
40                 if (unit == null)
41                         return null;
42
43                 if (context instanceof MultiFixContext) {
44                         return createFix(unit, ((MultiFixContext)context).getProblemLocations());
45                 } else {
46                         return createFix(unit);
47                 }
48         }
49
50         protected abstract ICleanUpFix createFix(CompilationUnit unit) throws CoreException;
51
52         protected abstract ICleanUpFix createFix(CompilationUnit unit, IProblemLocation[] problems) throws CoreException;
53
54         /**
55          * {@inheritDoc}
56          */
57         public int computeNumberOfFixes(CompilationUnit compilationUnit) {
58                 return -1;
59         }
60
61         /**
62          * Utility method to: count number of problems in <code>problems</code> with <code>problemId</code>
63          * @param problems the set of problems
64          * @param problemId the problem id to look for
65          * @return number of problems with problem id
66          */
67         protected static int getNumberOfProblems(IProblem[] problems, int problemId) {
68                 int result= 0;
69                 for (int i= 0; i < problems.length; i++) {
70                         if (problems[i].getID() == problemId)
71                                 result++;
72                 }
73                 return result;
74         }
75
76         /**
77          * Convert set of IProblems to IProblemLocations
78          * @param problems the problems to convert
79          * @return the converted set
80          */
81         protected static IProblemLocation[] convertProblems(IProblem[] problems) {
82                 IProblemLocation[] result= new IProblemLocation[problems.length];
83
84                 for (int i= 0; i < problems.length; i++) {
85                         result[i]= new ProblemLocation(problems[i]);
86                 }
87
88                 return result;
89         }
90
91         /**
92          * Returns unique problem locations. All locations in result
93          * have an id element <code>problemIds</code>.
94          *
95          * @param problems the problems to filter
96          * @param problemIds the ids of the resulting problem locations
97          * @return problem locations
98          */
99         protected static IProblemLocation[] filter(IProblemLocation[] problems, int[] problemIds) {
100                 ArrayList<IProblemLocation> result= new ArrayList<IProblemLocation>();
101
102                 for (int i= 0; i < problems.length; i++) {
103                         IProblemLocation problem= problems[i];
104                         if (contains(problemIds, problem.getProblemId()) && !contains(result, problem)) {
105                                 result.add(problem);
106                         }
107                 }
108
109                 return result.toArray(new IProblemLocation[result.size()]);
110         }
111
112         private static boolean contains(ArrayList<IProblemLocation> problems, IProblemLocation problem) {
113                 for (int i= 0; i < problems.size(); i++) {
114                         IProblemLocation existing= problems.get(i);
115                         if (existing.getProblemId() == problem.getProblemId() && existing.getOffset() == problem.getOffset() && existing.getLength() == problem.getLength()) {
116                                 return true;
117                         }
118                 }
119
120                 return false;
121         }
122
123         private static boolean contains(int[] ids, int id) {
124                 for (int i= 0; i < ids.length; i++) {
125                         if (ids[i] == id)
126                                 return true;
127                 }
128                 return false;
129         }
130 }