]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/extractors/ExtractAndMoveMethodPrefixesExtractor.java
Adding the interface PropertiesExtractor.
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / extractors / ExtractAndMoveMethodPrefixesExtractor.java
1 package no.uio.ifi.refaktor.extractors;
2
3 import java.util.Collections;
4 import java.util.Comparator;
5 import java.util.LinkedList;
6
7 import no.uio.ifi.refaktor.changers.ExtractAndMoveMethodChanger;
8 import no.uio.ifi.refaktor.changers.RefaktorChangerException;
9 import no.uio.ifi.refaktor.changers.RefaktorChangerException.Reason;
10 import no.uio.ifi.refaktor.extractors.collectors.PrefixesCollector;
11 import no.uio.ifi.refaktor.extractors.collectors.SelectionChecker;
12 import no.uio.ifi.refaktor.extractors.collectors.UnfixesCollector;
13 import no.uio.ifi.refaktor.prefix.Prefix;
14 import no.uio.ifi.refaktor.prefix.PrefixSet;
15 import no.uio.ifi.refaktor.utils.CompilationUnitTextSelection;
16
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 ExtractAndMoveMethodPrefixesExtractor implements PropertiesExtractor {
30
31         private final CompilationUnitTextSelection selection;
32         private final PrefixesCollector prefixesCollector;
33         private final UnfixesCollector unfixesCollector;
34         private PrefixSet safePrefixes = null;
35
36         public ExtractAndMoveMethodPrefixesExtractor(CompilationUnitTextSelection selection) {
37                 this.selection = selection;
38                 prefixesCollector = new PrefixesCollector(selection);
39                 unfixesCollector = new UnfixesCollector(selection);
40         }
41
42         public PrefixSet getSafePrefixes() {
43                 if (safePrefixes == null) {
44                         safePrefixes = createSafePrefixes();
45                 }
46                 return safePrefixes;
47         }
48
49         private PrefixSet createSafePrefixes() {
50                 return prefixesCollector.getPrefixes().minusEnclosedPrefixesFrom(unfixesCollector.getUnfixes());
51         }
52
53         public boolean hasUsefulResults() {
54                 return !getSafePrefixes().isEmpty();
55         }
56         
57         protected PrefixSet getPrefixes() {
58                 return prefixesCollector.getPrefixes();
59         }
60
61         protected PrefixSet getUnfixes() {
62                 return unfixesCollector.getUnfixes();
63         }
64
65         public Prefix getMostFrequentPrefix() {
66                 LinkedList<Prefix> listOfSafePrefixes = getListOfSafePrefixes();
67                 sortAscendingByCountAndLength(listOfSafePrefixes);
68                 return listOfSafePrefixes.getLast();
69         }
70
71         private LinkedList<Prefix> getListOfSafePrefixes() {
72                 return getSafePrefixes().toList();
73         }
74
75         private void sortAscendingByCountAndLength(LinkedList<Prefix> values) {
76                 Collections.sort(values, new Comparator<Prefix>() {
77                         @Override
78                         public int compare(Prefix p1, Prefix p2) {
79                                 if (p1.getCount() > p2.getCount()) {
80                                         return 1;
81                                 } else if (p1.getCount() < p2.getCount()) {
82                                         return -1;
83                                 } else if (p1.length() > p2.length()) {
84                                         return 1;
85                                 } else if (p1.length() < p2.length()) {
86                                         return -1;
87                                 }
88                                 return 0;
89                         }
90                 });
91         }
92
93         public void checkPreconditions() throws RefaktorChangerException {
94                 checkIfSelectionIsValid();
95                 extractProperties();
96                 checkIfUsefulTargetFound();
97         }
98
99         public void checkIfSelectionIsValid() {
100                 if (!selectionIsValid())
101                         throw new RefaktorChangerException(Reason.SELECTION_INVALID);
102         }
103
104         private void checkIfUsefulTargetFound() {
105                 if (!hasUsefulResults())
106                         throw new RefaktorChangerException(Reason.NO_TARGET_FOUND);
107         }
108
109         @Override
110         public void extractProperties() {
111                 CollectorManager.collectProperties(selection, prefixesCollector, unfixesCollector);
112         }
113
114         private boolean selectionIsValid() {
115                 return SelectionChecker.isSelectionValid(selection);
116         }
117 }