1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 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.core.tests.model;
15 
16 import java.util.*;
17 
18 import org.eclipse.jdt.core.ICorrectionRequestor;
19 
20 @SuppressWarnings({"rawtypes", "unchecked"})
21 public class CodeCorrectionTestsRequestor implements ICorrectionRequestor {
22 	private class Suggestion {
23 		public String text;
24 		public int start;
25 		public int end;
Suggestion(char[] text, int start, int end)26 		public Suggestion(char[] text, int start, int end){
27 			this.text = new String(text);
28 			this.start = start;
29 			this.end = end;
30 		}
31 	}
32 
33 	class SuggestionComparator implements Comparator {
34 		@Override
compare(Object o1,Object o2)35 		public int compare(Object o1,Object o2) {
36 			Suggestion s1 = (Suggestion)o1;
37 			Suggestion s2 = (Suggestion)o2;
38 
39 			int result = s1.text.compareTo(s2.text);
40 			if(result == 0) {
41 				result = s1.start - s2.start;
42 				if(result == 0) {
43 					result = s1.end - s2.end;
44 				}
45 			}
46 			return result;
47 		}
48 	}
49 
50 
51 	private Vector suggestions = new Vector(5);
52 
acceptClass(char[] packageName,char[] className,char[] correctionName,int modifiers,int correctionStart,int correctionEnd)53 	public void acceptClass(char[] packageName,char[] className,char[] correctionName,int modifiers,int correctionStart,int correctionEnd){
54 		this.suggestions.addElement(new Suggestion(correctionName, correctionStart, correctionEnd));
55 	}
56 
acceptField(char[] declaringTypePackageName,char[] declaringTypeName,char[] name,char[] typePackageName,char[] typeName,char[] correctionName,int modifiers,int correctionStart,int correctionEnd)57 	public void acceptField(char[] declaringTypePackageName,char[] declaringTypeName,char[] name,char[] typePackageName,char[] typeName,char[] correctionName,int modifiers,int correctionStart,int correctionEnd){
58 		this.suggestions.addElement(new Suggestion(correctionName, correctionStart, correctionEnd));
59 	}
60 
acceptInterface(char[] packageName,char[] interfaceName,char[] correctionName,int modifiers,int correctionStart,int correctionEnd)61 	public void acceptInterface(char[] packageName,char[] interfaceName,char[] correctionName,int modifiers,int correctionStart,int correctionEnd){
62 		this.suggestions.addElement(new Suggestion(correctionName, correctionStart, correctionEnd));
63 	}
64 
acceptLocalVariable(char[] name,char[] typePackageName,char[] typeName,int modifiers,int correctionStart,int correctionEnd)65 	public void acceptLocalVariable(char[] name,char[] typePackageName,char[] typeName,int modifiers,int correctionStart,int correctionEnd){
66 		this.suggestions.addElement(new Suggestion(name, correctionStart, correctionEnd));
67 	}
68 
acceptMethod(char[] declaringTypePackageName,char[] declaringTypeName,char[] selector,char[][] parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] returnTypePackageName,char[] returnTypeName,char[] correctionName,int modifiers,int correctionStart,int correctionEnd)69 	public void acceptMethod(char[] declaringTypePackageName,char[] declaringTypeName,char[] selector,char[][] parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] returnTypePackageName,char[] returnTypeName,char[] correctionName,int modifiers,int correctionStart,int correctionEnd){
70 		this.suggestions.addElement(new Suggestion(correctionName, correctionStart, correctionEnd));
71 	}
72 
acceptPackage(char[] packageName,char[] correctionName,int correctionStart,int correctionEnd)73 	public void acceptPackage(char[] packageName,char[] correctionName,int correctionStart,int correctionEnd){
74 		this.suggestions.addElement(new Suggestion(correctionName, correctionStart, correctionEnd));
75 	}
76 
getSuggestions()77 	public String getSuggestions(){
78 		Suggestion[] sortedSuggestions = getSortedSuggestions();
79 
80 		StringBuffer result = new StringBuffer();
81 		for (int i = 0; i < sortedSuggestions.length; i++) {
82 			if(i != 0)
83 				result.append('\n');
84 
85 			result.append(sortedSuggestions[i].text);
86 		}
87 		return result.toString();
88 	}
89 
getStarts()90 	public String getStarts(){
91 		Suggestion[] sortedSuggestions = getSortedSuggestions();
92 
93 		StringBuffer result = new StringBuffer();
94 		for (int i = 0; i < sortedSuggestions.length; i++) {
95 			if(i != 0)
96 				result.append('\n');
97 
98 			result.append(sortedSuggestions[i].start);
99 		}
100 		return result.toString();
101 	}
102 
getEnds()103 	public String getEnds(){
104 		Suggestion[] sortedSuggestions = getSortedSuggestions();
105 
106 		StringBuffer result = new StringBuffer();
107 		for (int i = 0; i < sortedSuggestions.length; i++) {
108 			if(i != 0)
109 				result.append('\n');
110 
111 			result.append(sortedSuggestions[i].end);
112 		}
113 		return result.toString();
114 	}
115 
getSortedSuggestions()116 	private Suggestion[] getSortedSuggestions(){
117 		Object[] unsorted = this.suggestions.toArray();
118 		Suggestion[] sorted = new Suggestion[unsorted.length];
119 		System.arraycopy(unsorted, 0, sorted, 0, unsorted.length);
120 		Arrays.sort(sorted, new SuggestionComparator());
121 		return sorted;
122 	}
123 }
124