Skip to content

Commit

Permalink
Use Collator.getInstance() for sorting Profiles
Browse files Browse the repository at this point in the history
- This provides lexical comparison based on locale
- And use proper Java lambdas
  • Loading branch information
Phillipus committed Jul 24, 2024
1 parent a424d59 commit a743113
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
package com.archimatetool.editor.diagram;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

Expand Down Expand Up @@ -214,7 +214,8 @@ private void createSpecializationsGroup() {
}

// Sort Profiles into Elements, then Relations
Collections.sort(profiles, Comparator.comparing(IProfile::getConceptClass, (c1, c2) -> {
Collator collator = Collator.getInstance();
profiles.sort(Comparator.comparing(IProfile::getConceptClass, (c1, c2) -> {
if((IArchimatePackage.eINSTANCE.getArchimateElement().isSuperTypeOf(c1) && IArchimatePackage.eINSTANCE.getArchimateElement().isSuperTypeOf(c2))
|| (IArchimatePackage.eINSTANCE.getArchimateRelationship().isSuperTypeOf(c1) && IArchimatePackage.eINSTANCE.getArchimateRelationship().isSuperTypeOf(c2))) {
return 0;
Expand All @@ -225,7 +226,7 @@ private void createSpecializationsGroup() {
}

return 1;
}).thenComparing(IProfile::getName));
}).thenComparing(IProfile::getName, (name1, name2) -> collator.compare(name1, name2)));

PaletteGroup group = new PaletteGroup(Messages.ArchimateDiagramEditorPalette_0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/
package com.archimatetool.editor.propertysections;

import java.util.Collections;
import java.util.Comparator;
import java.text.Collator;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
Expand Down Expand Up @@ -143,20 +142,16 @@ public Object[] getElements(Object inputElement) {
return new Object[0];
}

List<IProfile> list = ArchimateModelUtils.findProfilesForConceptType(firstSelected.getArchimateModel(), firstSelected.eClass());
List<IProfile> profiles = ArchimateModelUtils.findProfilesForConceptType(firstSelected.getArchimateModel(), firstSelected.eClass());

// Sort the Profiles by name
Collections.sort(list, new Comparator<IProfile>() {
@Override
public int compare(IProfile p1, IProfile p2) {
return p1.getName().compareToIgnoreCase(p2.getName());
}
});
Collator collator = Collator.getInstance();
profiles.sort((p1, p2) -> collator.compare(p1.getName(), p2.getName()));

// Add the "none" Profile at the top
list.add(0, NONE_PROFILE);
profiles.add(0, NONE_PROFILE);

return list.toArray();
return profiles.toArray();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -502,14 +500,8 @@ private void deleteSelectedProfiles() {
* Sort profiles on names
*/
private void sortProfiles() {
Collections.sort(fProfilesCopy, new Comparator<IProfile>() {
Collator collator = Collator.getInstance();

@Override
public int compare(IProfile p1, IProfile p2) {
return collator.compare(p1.getName(), p2.getName());
}
});
Collator collator = Collator.getInstance();
fProfilesCopy.sort((p1, p2) -> collator.compare(p1.getName(), p2.getName()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -179,14 +177,8 @@ private List<IAction> createSpecializationActions(EClass[] classes, IFolder fold

List<IProfile> profiles = new ArrayList<>(folder.getArchimateModel().getProfiles());

Collections.sort(profiles, new Comparator<IProfile>() {
private Collator collator = Collator.getInstance();

@Override
public int compare(IProfile p1, IProfile p2) {
return collator.compare(p1.getName(), p2.getName());
}
});
Collator collator = Collator.getInstance();
profiles.sort((p1, p2) -> collator.compare(p1.getName(), p2.getName()));

Set<EClass> classesSet = Set.of(classes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
*/
package com.archimatetool.editor.views.tree.search;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -378,16 +377,16 @@ public ImageDescriptor getImageDescriptor() {

private void populatePropertiesMenu() {
// Models that are loaded are the ones in the Models Tree
Set<String> set = new LinkedHashSet<String>(); // LinkedHashSet is faster when sorting
Set<String> set = new LinkedHashSet<>(); // LinkedHashSet is faster when sorting

for(IArchimateModel model : IEditorModelManager.INSTANCE.getModels()) {
getAllUniquePropertyKeysForModel(model, set);
}

List<String> list = new ArrayList<String>(set);
List<String> list = new ArrayList<>(set);

// Sort alphabetically
Collections.sort(list, (s1, s2) -> s1.compareToIgnoreCase(s2)); // Don't use Collator.getInstance() as it's too slow
// Sort alphabetically, but don't use Collator.getInstance() as it's too slow
list.sort((s1, s2) -> s1.compareToIgnoreCase(s2));

// Limit to a sensible menu size
if(list.size() > 1000) {
Expand Down Expand Up @@ -439,12 +438,8 @@ private void populateSpecializationsMenu() {
}

// Sort alphabetically
Collections.sort(profiles, new Comparator<IProfile>() {
@Override
public int compare(IProfile p1, IProfile p2) {
return p1.getName().compareToIgnoreCase(p2.getName());
}
});
Collator collator = Collator.getInstance();
profiles.sort((p1, p2) -> collator.compare(p1.getName(), p2.getName()));

for(final IProfile profile : profiles) {
IAction action = new Action(profile.getName(), IAction.AS_CHECK_BOX) {
Expand Down

0 comments on commit a743113

Please sign in to comment.