]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/analyze/ExtractAndMoveMethodAnalyzer.java
a2f63ad39a32e4100178e9e3c6eb06c09dab6a32
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / analyze / ExtractAndMoveMethodAnalyzer.java
1 package no.uio.ifi.refaktor.analyze;
2
3 import java.util.Collections;
4 import java.util.Comparator;
5 import java.util.LinkedList;
6
7 import no.uio.ifi.refaktor.analyze.collectors.PrefixesCollector;
8 import no.uio.ifi.refaktor.analyze.collectors.SelectionValidator;
9 import no.uio.ifi.refaktor.analyze.collectors.UnfixesCollector;
10 import no.uio.ifi.refaktor.changers.ExtractAndMoveMethodChanger;
11 import no.uio.ifi.refaktor.changers.RefaktorChangerException;
12 import no.uio.ifi.refaktor.prefix.Prefix;
13 import no.uio.ifi.refaktor.prefix.PrefixSet;
14 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
15
16 //TODO: rewrite docs
17 /**
18  * A property extractor that collects all the expression prefixes within a selection
19  * (see {@link PrefixesCollector}) that forms the base for calculating the
20  * candidates for the move refactoring, and also the unfixes 
21  * that are non-candidates for the move refactoring. 
22  * 
23  * The set of prefixes that are not enclosing any unfixes is put in the set of safe prefixes.
24  * This set is used by an Extract and Move Method refactoring to find a suitable target
25  * for the Move Method. 
26  * 
27  * The class is typically used by the {@link ExtractAndMoveMethodChanger}.
28  */
29 public class ExtractAndMoveMethodAnalyzer implements Analyzer {
30
31         private final CompilationUnitTextSelection selection;
32         private final PrefixesCollector prefixesCollector;
33         private final UnfixesCollector unfixesCollector;
34
35         public ExtractAndMoveMethodAnalyzer(CompilationUnitTextSelection selection) {
36                 this.selection = selection;
37                 prefixesCollector = new PrefixesCollector(selection);
38                 unfixesCollector = new UnfixesCollector(selection);
39         }
40
41         public PrefixSet calculateSafePrefixes() {
42                 return prefixesCollector.getPrefixes().minusEnclosedPrefixesFrom(unfixesCollector.getUnfixes());
43         }
44         
45         protected PrefixSet getPrefixes() {
46                 return prefixesCollector.getPrefixes();
47         }
48
49         protected PrefixSet getUnfixes() {
50                 return unfixesCollector.getUnfixes();
51         }
52
53         public Prefix getMostFrequentPrefix() {
54                 LinkedList<Prefix> listOfSafePrefixes = getListOfSafePrefixes();
55                 sortAscendingByCountAndLength(listOfSafePrefixes);
56                 return listOfSafePrefixes.getLast();
57         }
58
59         private LinkedList<Prefix> getListOfSafePrefixes() {
60                 return calculateSafePrefixes().toList();
61         }
62
63         private void sortAscendingByCountAndLength(LinkedList<Prefix> values) {
64                 Collections.sort(values, new Comparator<Prefix>() {
65                         @Override
66                         public int compare(Prefix p1, Prefix p2) {
67                                 if (p1.getCount() > p2.getCount()) {
68                                         return 1;
69                                 } else if (p1.getCount() < p2.getCount()) {
70                                         return -1;
71                                 } else if (p1.length() > p2.length()) {
72                                         return 1;
73                                 } else if (p1.length() < p2.length()) {
74                                         return -1;
75                                 }
76                                 return 0;
77                         }
78                 });
79         }
80
81         public void checkPreconditions() throws RefaktorChangerException {
82                 SelectionValidator.checkIfSelectionIsValid(selection);
83                 analyze();
84                 checkIfUsefulTargetFound();
85         }
86
87         private void checkIfUsefulTargetFound() {
88                 if (!hasUsefulResults())
89                         throw new NoTargetFoundException();
90         }
91
92         private boolean hasUsefulResults() {
93                 return !calculateSafePrefixes().isEmpty();
94         }
95
96         @Override
97         public void analyze() {
98                 CollectorManager.collectProperties(selection, prefixesCollector, unfixesCollector);
99         }
100 }