001package org.andromda.cartridges.meta; 002 003import java.text.Collator; 004import java.util.ArrayList; 005import java.util.Collection; 006import java.util.Collections; 007import java.util.Comparator; 008import java.util.List; 009import org.andromda.core.metafacade.MetafacadeConstants; 010import org.andromda.metafacades.uml.ConstraintFacade; 011import org.andromda.metafacades.uml.ModelElementFacade; 012import org.apache.commons.lang.StringUtils; 013 014/** 015 * Contains utilities for the AndroMDA meta cartridge. 016 * 017 * @author Chad Brandon 018 */ 019public class MetaCartridgeUtils 020{ 021 /** 022 * Sorts model elements by their fully qualified name. 023 * 024 * @param modelElements the collection of model elements to sort. 025 * @return the sorted collection. 026 */ 027 public static Collection<ModelElementFacade> sortByFullyQualifiedName(Collection<ModelElementFacade> modelElements) 028 { 029 List<ModelElementFacade> sortedElements = null; 030 if (modelElements != null) 031 { 032 sortedElements = new ArrayList<ModelElementFacade>(modelElements); 033 Collections.sort( 034 sortedElements, 035 new FullyQualifiedNameComparator()); 036 } 037 return sortedElements; 038 } 039 040 /** 041 * Used to sort operations by <code>fullyQualifiedName</code>. 042 */ 043 static final class FullyQualifiedNameComparator 044 implements Comparator 045 { 046 private final Collator collator = Collator.getInstance(); 047 048 /** 049 * 050 */ 051 FullyQualifiedNameComparator() 052 { 053 this.collator.setStrength(Collator.PRIMARY); 054 } 055 056 /** 057 * @see java.util.Comparator#compare(Object, Object) 058 */ 059 public int compare( 060 final Object objectA, 061 final Object objectB) 062 { 063 ModelElementFacade a = (ModelElementFacade)objectA; 064 ModelElementFacade b = (ModelElementFacade)objectB; 065 066 return this.collator.compare( 067 a.getFullyQualifiedName(), 068 b.getFullyQualifiedName()); 069 } 070 } 071 072 /** 073 * Retrieves the fully qualified constraint name given the constraint (this includes the 074 * full name of the context element and the constraint to which it applies). 075 * 076 * @param constraint the constraint of which to retrieve the name. 077 * @return the fully qualified name. 078 */ 079 public static String getFullyQualifiedConstraintName(final ConstraintFacade constraint) 080 { 081 final StringBuilder name = new StringBuilder(); 082 if (constraint != null) 083 { 084 final ModelElementFacade contextElement = constraint.getContextElement(); 085 final String contextElementName = 086 contextElement != null ? contextElement.getFullyQualifiedName(true) : null; 087 if (StringUtils.isNotBlank(contextElementName) && contextElementName != null) 088 { 089 name.append(contextElementName.trim()); 090 name.append(MetafacadeConstants.NAMESPACE_SCOPE_OPERATOR); 091 } 092 name.append(constraint.getName()); 093 } 094 return name.toString(); 095 } 096}