gr.uom.java.xmi.diff.ExtractSuperclassRefactoring Maven / Gradle / Ivy
package gr.uom.java.xmi.diff;
import gr.uom.java.xmi.UMLClass;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.refactoringminer.api.Refactoring;
import org.refactoringminer.api.RefactoringType;
public class ExtractSuperclassRefactoring implements Refactoring {
private UMLClass extractedClass;
private Set subclassSetBefore;
private Set subclassSetAfter;
public ExtractSuperclassRefactoring(UMLClass extractedClass, Set subclassSetBefore, Set subclassSetAfter) {
this.extractedClass = extractedClass;
this.subclassSetBefore = subclassSetBefore;
this.subclassSetAfter = subclassSetAfter;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName()).append("\t");
sb.append(extractedClass);
sb.append(" from classes ");
sb.append(subclassSetBefore);
return sb.toString();
}
public String getName() {
return this.getRefactoringType().getDisplayName();
}
public RefactoringType getRefactoringType() {
if(extractedClass.isInterface())
return RefactoringType.EXTRACT_INTERFACE;
else
return RefactoringType.EXTRACT_SUPERCLASS;
}
public UMLClass getExtractedClass() {
return extractedClass;
}
public Set getSubclassSetBefore() {
Set subclassSet = new LinkedHashSet();
for(UMLClass umlClass : this.subclassSetBefore) {
subclassSet.add(umlClass.getName());
}
return subclassSet;
}
public Set getUMLSubclassSetBefore() {
return new LinkedHashSet(subclassSetBefore);
}
public Set getSubclassSetAfter() {
Set subclassSet = new LinkedHashSet();
for(UMLClass umlClass : this.subclassSetAfter) {
subclassSet.add(umlClass.getName());
}
return subclassSet;
}
public Set getUMLSubclassSetAfter() {
return new LinkedHashSet(subclassSetAfter);
}
public Set> getInvolvedClassesBeforeRefactoring() {
Set> pairs = new LinkedHashSet>();
for(UMLClass umlClass : this.subclassSetBefore) {
pairs.add(new ImmutablePair(umlClass.getLocationInfo().getFilePath(), umlClass.getName()));
}
return pairs;
}
public Set> getInvolvedClassesAfterRefactoring() {
Set> pairs = new LinkedHashSet>();
for(UMLClass umlClass : this.subclassSetAfter) {
pairs.add(new ImmutablePair(umlClass.getLocationInfo().getFilePath(), umlClass.getName()));
}
pairs.add(new ImmutablePair(getExtractedClass().getLocationInfo().getFilePath(), getExtractedClass().getName()));
return pairs;
}
@Override
public List leftSide() {
List ranges = new ArrayList();
for(UMLClass subclass : subclassSetBefore) {
ranges.add(subclass.codeRange()
.setDescription("original sub-type declaration")
.setCodeElement(subclass.getName()));
}
return ranges;
}
@Override
public List rightSide() {
List ranges = new ArrayList();
for(UMLClass subclass : subclassSetAfter) {
ranges.add(subclass.codeRange()
.setDescription("sub-type declaration after extraction")
.setCodeElement(subclass.getName()));
}
ranges.add(extractedClass.codeRange()
.setDescription("extracted super-type declaration")
.setCodeElement(extractedClass.getName()));
return ranges;
}
}