All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gr.uom.java.xmi.diff.MergeAttributeRefactoring Maven / Gradle / Ivy

package gr.uom.java.xmi.diff;

import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.refactoringminer.api.Refactoring;
import org.refactoringminer.api.RefactoringType;

import gr.uom.java.xmi.decomposition.AbstractCodeMapping;
import gr.uom.java.xmi.decomposition.VariableDeclaration;
import gr.uom.java.xmi.UMLAttribute;

public class MergeAttributeRefactoring implements Refactoring, ReferenceBasedRefactoring {
	private Set mergedAttributes;
	private UMLAttribute newAttribute;
	private Set attributeMerges;
	private String classNameBefore;
	private String classNameAfter;

	public MergeAttributeRefactoring(Set mergedAttributes, UMLAttribute newAttribute,
			String classNameBefore, String classNameAfter, Set attributeMerges) {
		this.mergedAttributes = mergedAttributes;
		this.newAttribute = newAttribute;
		this.classNameBefore = classNameBefore;
		this.classNameAfter = classNameAfter;
		this.attributeMerges = attributeMerges;
	}

	public Set getMergedAttributes() {
		return mergedAttributes;
	}

	public UMLAttribute getNewAttribute() {
		return newAttribute;
	}

	public Set getReferences() {
		Set references = new LinkedHashSet();
		for(CandidateMergeVariableRefactoring candidate : attributeMerges) {
			references.addAll(candidate.getReferences());
		}
		return references;
	}

	public String getClassNameBefore() {
		return classNameBefore;
	}

	public String getClassNameAfter() {
		return classNameAfter;
	}

	public RefactoringType getRefactoringType() {
		return RefactoringType.MERGE_ATTRIBUTE;
	}

	public String getName() {
		return this.getRefactoringType().getDisplayName();
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(getName()).append("\t");
		sb.append(getMergedVariables());
		sb.append(" to ");
		sb.append(newAttribute.getVariableDeclaration());
		sb.append(" in class ").append(classNameAfter);
		return sb.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((classNameAfter == null) ? 0 : classNameAfter.hashCode());
		result = prime * result + ((classNameBefore == null) ? 0 : classNameBefore.hashCode());
		Set mergedVariables = getMergedVariables();
		result = prime * result + ((mergedVariables.isEmpty()) ? 0 : mergedVariables.hashCode());
		result = prime * result + ((newAttribute == null || newAttribute.getVariableDeclaration() == null) ? 0 : newAttribute.getVariableDeclaration().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MergeAttributeRefactoring other = (MergeAttributeRefactoring) obj;
		if (classNameAfter == null) {
			if (other.classNameAfter != null)
				return false;
		} else if (!classNameAfter.equals(other.classNameAfter))
			return false;
		if (classNameBefore == null) {
			if (other.classNameBefore != null)
				return false;
		} else if (!classNameBefore.equals(other.classNameBefore))
			return false;
		if (!this.getMergedVariables().equals(other.getMergedVariables()))
			return false;
		if (newAttribute == null) {
			if (other.newAttribute != null)
				return false;
		} else if (newAttribute.getVariableDeclaration() == null) {
			if (other.newAttribute.getVariableDeclaration() != null)
				return false;
		} else if (!newAttribute.getVariableDeclaration().equals(other.newAttribute.getVariableDeclaration()))
			return false;
		return true;
	}

	public Set> getInvolvedClassesBeforeRefactoring() {
		Set> pairs = new LinkedHashSet>();
		for(UMLAttribute mergedAttribute : this.mergedAttributes) {
			pairs.add(new ImmutablePair(mergedAttribute.getLocationInfo().getFilePath(), getClassNameBefore()));
		}
		return pairs;
	}

	public Set> getInvolvedClassesAfterRefactoring() {
		Set> pairs = new LinkedHashSet>();
		pairs.add(new ImmutablePair(getNewAttribute().getLocationInfo().getFilePath(), getClassNameAfter()));
		return pairs;
	}

	@Override
	public List leftSide() {
		List ranges = new ArrayList();
		for(VariableDeclaration mergedAttribute : getMergedVariables()) {
			ranges.add(mergedAttribute.codeRange()
					.setDescription("merged attribute declaration")
					.setCodeElement(mergedAttribute.toString()));
		}
		return ranges;
	}

	@Override
	public List rightSide() {
		List ranges = new ArrayList();
		ranges.add(newAttribute.getVariableDeclaration().codeRange()
				.setDescription("new attribute declaration")
				.setCodeElement(newAttribute.getVariableDeclaration().toString()));
		return ranges;
	}

	public Set getMergedVariables() {
		return mergedAttributes.stream().map(UMLAttribute::getVariableDeclaration).collect(Collectors.toCollection(LinkedHashSet::new));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy