1   package com.stateofflow.eclipse.metrics.calculators.cohesion;
2   
3   import java.util.Set;
4   
5   import org.eclipse.core.runtime.CoreException;
6   import org.eclipse.jdt.core.IJavaElement;
7   import org.eclipse.jdt.core.JavaModelException;
8   import org.eclipse.jdt.core.search.SearchMatch;
9   import org.eclipse.jdt.core.search.SearchRequestor;
10  
11  abstract class FieldResultCollector extends SearchRequestor {
12      private final Source source;
13      private final ElementToFieldSetMap elementToFieldsMap = new ElementToFieldSetMap();
14  
15      public FieldResultCollector(final String source) {
16          this.source = new Source(source);
17      }
18  
19      public boolean isEmpty() {
20          return elementToFieldsMap.isEmpty();
21      }
22  
23      public Set<IJavaElement> getKeys() {
24          return elementToFieldsMap.keySet();
25      }
26  
27      protected boolean hasField(final IJavaElement enclosingElement, final int start, final int end) {
28          return elementToFieldsMap.hasField(enclosingElement, source.getFieldName(start, end));
29      }
30  
31      protected void addField(final IJavaElement enclosingElement, final int start, final int end) {
32          elementToFieldsMap.addField(enclosingElement, source.getFieldName(start, end));
33      }
34  
35      public FieldSet getFields(final IJavaElement enclosingElement) {
36          return elementToFieldsMap.get(enclosingElement);
37      }
38  
39      protected Source getSource() {
40          return source;
41      }
42  
43      @Override
44      public void acceptSearchMatch(final SearchMatch match) throws CoreException {
45          final int start = match.getOffset();
46          final int end = start + match.getLength();
47          acceptSearchMatch((IJavaElement) match.getElement(), start, end);
48      }
49  
50      protected abstract void acceptSearchMatch(IJavaElement enclosingElement, int start, int end) throws JavaModelException;
51  }