I have two enums describing two UML profiles (meaning they define stereotypes that the profiles contain).
I also have two utility classes featuring nearly identical methods working on each of the profiles.
Example:
public static List<Element> getStereotypedElements(final InsertProfileHere stereo, final Package pkg) {
List<Element> extendedElements = new ArrayList<Element>();
if (isProfileApplied(pkg)) {
if (hasStereotype(stereo, pkg)) {
extendedElements.add(pkg);
}
extendedElements.addAll(getStereotypedElements(stereo, pkg.allOwnedElements()));
}
return extendedElements;
}
,where InsertProfileHere can be replaced with each of the two profile enums.
If anyone is interested, this method uses the Eclipse Modeling Framework or rather the UML2 metamodel implementation in EMF.
Anyway, I want to merge the two utility classes to avoid redundant code.
I've tried:
- a super interface for the two profiles
- didn't work because of static methods
- an abstract class for the Utility classes
- didn't work because of static methods
- encapsulating the profile enums in a class
Each didn't work for one or another reason.
Anyone got any ideas?
EDIT:
An example for another utility method:
public static boolean hasStereotype(
final InsertProfileHere stereo, final Element elem) {
for (Stereotype appliedStereo : elem.getAppliedStereotypes()) {
if (stereo == null) {
if (InsertProfileHere.contains(appliedStereo)) {
return true;
}
} else if (stereo.isEqual(appliedStereo)) {
return true;
}
}
return false;
}
EDIT2: And for good measure part of the implementation of the profile enum
public enum Profile1 {
STEREOTYPE1 ("readable stereotype1 name"),
STEREOTYPE2 ("readable stereotype2 name"),
STEREOTYPE3 ("readable stereotype3 name"),
public static final String PROFILE_NAME = "NameOfProfile";
private final String readableName;
private Profile1(final String newName) {
readableName = newName;
}
public static Profile1 getValue(final String name) {
for (Profile1 type : Profile1.values()) {
if (type.toString().equals(name)) {
return type;
}
}
return null;
}
public static boolean contains(Stereotype stereotype) {
return (stereotype.getProfile().
getDefinition().getNsURI().contains(PROFILE_NAME));
}