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

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

package gr.uom.java.xmi.diff;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.tuple.Pair;

import gr.uom.java.xmi.UMLType;

public class UMLImplementedInterfaceListDiff {
	private Set removedInterfaces;
	private Set addedInterfaces;
	private Set> commonInterfaces;
	private Set> changedInterfaces;
	
	public UMLImplementedInterfaceListDiff(List oldInterfaces, List newInterfaces) {
		this.changedInterfaces = new LinkedHashSet<>();
		Set oldInterfaceSet = new LinkedHashSet<>(oldInterfaces);
		Set newInterfaceSet = new LinkedHashSet<>(newInterfaces);
		Set intersection = new LinkedHashSet<>();
		intersection.addAll(oldInterfaceSet);
		intersection.retainAll(newInterfaceSet);
		this.commonInterfaces = new LinkedHashSet<>();
		for(UMLType umlInterface : intersection) {
			UMLType oldInterface = oldInterfaces.get(oldInterfaces.indexOf(umlInterface));
			UMLType newInterface = newInterfaces.get(newInterfaces.indexOf(umlInterface));
			Pair pair = Pair.of(oldInterface, newInterface);
			commonInterfaces.add(pair);
		}
		oldInterfaceSet.removeAll(intersection);
		this.removedInterfaces = oldInterfaceSet;
		newInterfaceSet.removeAll(intersection);
		this.addedInterfaces = newInterfaceSet;
	}

	public Set getRemovedInterfaces() {
		return removedInterfaces;
	}

	public Set getAddedInterfaces() {
		return addedInterfaces;
	}

	public Set> getCommonInterfaces() {
		return commonInterfaces;
	}

	public Set> getChangedInterfaces() {
		return changedInterfaces;
	}

	private UMLType findMatchingInterface(Set interfaces, String className) {
		for(UMLType type : interfaces) {
			if(className.endsWith("." + type.getClassType()) || className.equals(type.getClassType())) {
				return type;
			}
		}
		return null;
	}

	private UMLType findMatchingInterface(Set interfaces, UMLType type) {
		for(UMLType i : interfaces) {
			if(type.equalClassType(i)) {
				return i;
			}
		}
		return null;
	}

	public void findInterfaceChanges(String nameBefore, String nameAfter) {
		UMLType removedInterface = findMatchingInterface(removedInterfaces, nameBefore);
		UMLType addedInterface = findMatchingInterface(addedInterfaces, nameAfter);
		if(removedInterface != null && addedInterface != null) {
			Pair pair = Pair.of(removedInterface, addedInterface);
			changedInterfaces.add(pair);
			removedInterfaces.remove(removedInterface);
			addedInterfaces.remove(addedInterface);
		}
	}

	public void findInterfaceChanges(UMLType typeBefore, UMLType typeAfter) {
		UMLType removedInterface = findMatchingInterface(removedInterfaces, typeBefore);
		UMLType addedInterface = findMatchingInterface(addedInterfaces, typeAfter);
		if(removedInterface != null && addedInterface != null) {
			Pair pair = Pair.of(removedInterface, addedInterface);
			changedInterfaces.add(pair);
			removedInterfaces.remove(removedInterface);
			addedInterfaces.remove(addedInterface);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy