IDEA-CR-16002 sort settings dropdowns sdks together only by the same sdk comparator object instance; @NotNull annotating; make SdkComparator not lazy initializable singleton

This commit is contained in:
Dmitry.Krasilschikov
2016-11-30 17:45:40 +03:00
parent 9d5a638554
commit 6aff1c01b8
2 changed files with 28 additions and 5 deletions
@@ -305,19 +305,41 @@ public class JdkComboBox extends ComboBoxWithWidePopup {
}
}
private static class MyComparatorWrapper {
@NotNull
private final Comparator<Sdk> myComparator;
MyComparatorWrapper(@NotNull final Comparator<Sdk> comparator) {
myComparator = comparator;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return myComparator == ((MyComparatorWrapper)o).myComparator;
}
@Override
public int hashCode() {
return myComparator.hashCode();
}
}
@NotNull
private static Sdk[] sortSdks(@NotNull final Sdk[] sdks) {
MultiMap<Comparator<Sdk>, Sdk> comparatorToSdkMap = new MultiMap<>();
MultiMap<MyComparatorWrapper, Sdk> comparatorToSdkMap = new MultiMap<>();
for (Sdk sdk : sdks) {
final SdkTypeId sdkType = sdk.getSdkType();
if (sdkType instanceof SdkType) {
comparatorToSdkMap.putValue(((SdkType)sdkType).getComparator(), sdk);
comparatorToSdkMap.putValue(new MyComparatorWrapper(((SdkType)sdkType).getComparator()), sdk);
} else {
comparatorToSdkMap.putValue(SdkType.ALPHABETICAL_COMPARATOR, sdk);
comparatorToSdkMap.putValue(new MyComparatorWrapper(SdkType.ALPHABETICAL_COMPARATOR), sdk);
}
}
return comparatorToSdkMap.entrySet().stream().flatMap(entry -> entry.getValue().stream().sorted(entry.getKey()))
return comparatorToSdkMap.entrySet().stream().flatMap(entry -> entry.getValue().stream().sorted(entry.getKey().myComparator))
.toArray(size -> new Sdk[size]);
}
@@ -94,9 +94,10 @@ public abstract class SdkType implements SdkTypeId {
/**
* Returns comparator which is used to order sdks in project or module settings combo boxes.
* If different sdk types return the same comparator object then they are sorted together.
* If different sdk types return the same comparator instance then they are sorted together.
*/
@NotNull
public Comparator<Sdk> getComparator() {
return ALPHABETICAL_COMPARATOR;
}