Skip to content

Commit

Permalink
feat(sort): Using String::compareToIgnoreCase.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Jan 28, 2024
1 parent f0c8c05 commit c8d74aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,39 @@

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.Function;

public enum SortFunctions {

NAME(data -> ColorParser.clear(data.getName()), String::compareTo),
RATINGS(data -> data.rateRatio(ResidenceRate::recommend), Double::compare, NAME.dataComparator(false)),
SIZE(data -> data.getResidence().getMainArea().getSize(), Long::compare, NAME.dataComparator(false));
NAME(comparingData(data -> ColorParser.clear(data.getDisplayName()), String::compareToIgnoreCase)),
RATINGS(comparingData(d -> d.rateRatio(ResidenceRate::recommend), Double::compare)
.thenComparing(d -> d.countRate(ResidenceRate::recommend), Integer::compare)
.thenComparing(NAME.comparator)),
SIZE(comparingData(d -> d.getResidence().getMainArea().getSize(), Long::compare).thenComparing(NAME.comparator));

private final Comparator<ResidenceData> comparator;

SortFunctions(Comparator<ResidenceData> comparator) {
this.comparator = comparator;
}

<T> SortFunctions(Function<ResidenceData, T> function,
BiFunction<T, T, Integer> comparator) {
this((o1, o2) -> comparator.apply(function.apply(o1), function.apply(o2)));
}


<T> SortFunctions(Function<ResidenceData, T> function,
BiFunction<T, T, Integer> comparator,
Comparator<ResidenceData> thenCompare) {
this((o1, o2) -> {
int first = comparator.apply(function.apply(o1), function.apply(o2));
return first != 0 ? first : thenCompare.compare(o1, o2);
});
}

public Comparator<ResidenceData> dataComparator(boolean reverse) {
return reverse ? comparator.reversed() : comparator;
}

public Comparator<ClaimedResidence> residenceComparator(boolean reverse) {
return (r1, r2) -> dataComparator(reverse).compare(ResidenceListAPI.getResidenceData(r1), ResidenceListAPI.getResidenceData(r2));
return Comparator.comparing(ResidenceListAPI::getResidenceData, dataComparator(reverse));
}

public SortFunctions next() {
return next(this);
}

public static <U> Comparator<ResidenceData> comparingData(Function<ResidenceData, ? extends U> keyExtractor,
Comparator<? super U> keyComparator) {
return (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), keyExtractor.apply(c2));
}

public static SortFunctions next(SortFunctions v) {
return v.ordinal() >= values().length - 1 ? values()[0] : values()[v.ordinal() + 1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Comparator;
import java.util.Objects;

public class ResidenceAdminUI extends AutoPagedGUI {
Expand Down Expand Up @@ -87,6 +88,27 @@ public void onClick(Player clicker, ClickType type) {
}
});
}

ConfiguredItem sortItem = switch (getPlayerData().getSortFunction()) {
case NAME -> ResidenceListUI.CONFIG.ITEMS.SORT_BY_NAME;
case SIZE -> ResidenceListUI.CONFIG.ITEMS.SORT_BY_SIZE;
case RATINGS -> ResidenceListUI.CONFIG.ITEMS.SORT_BY_RATINGS;
};

setItem(53, new GUIItem(sortItem.get(getViewer(), (getPlayerData().isSortReversed() ? "⬇" : "⬆"))) {
@Override
public void onClick(Player clicker, ClickType type) {
if (type.isRightClick()) {
PluginConfig.GUI.CLICK_SOUND.playTo(getViewer());
getPlayerData().setSortReversed(!getPlayerData().isSortReversed());
open(clicker, owner);
} else if (type.isLeftClick()) {
PluginConfig.GUI.CLICK_SOUND.playTo(getViewer());
getPlayerData().setSortFunction(getPlayerData().getSortFunction().next());
open(clicker, owner);
}
}
});
}

@Override
Expand All @@ -97,7 +119,10 @@ public void onPageChange(int pageNum) {

public void loadResidences() {
UserListData data = getPlayerData();
ResidenceListAPI.listResidences().stream().filter(this::checkOwner).forEach(residence -> addItem(generateIcon(residence)));
Comparator<ClaimedResidence> comparator = data.getSortFunction().residenceComparator(data.isSortReversed());
ResidenceListAPI.listResidences().stream()
.filter(this::checkOwner).sorted(comparator)
.forEach(residence -> addItem(generateIcon(residence)));
}

protected GUIItem generateIcon(ClaimedResidence residence) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -123,16 +124,17 @@ public void loadResidences() {
UserListData data = getPlayerData();
List<ClaimedResidence> display = new ArrayList<>();

Comparator<ClaimedResidence> comparator = data.getSortFunction().residenceComparator(data.isSortReversed());

data.getPinned().stream()
.map(ResidenceListAPI::getResidence)
.filter(residence -> residence != null && checkOwner(residence))
.forEach(display::add);

.sorted(comparator).forEach(display::add);
ResidenceListAPI.listResidences().stream()
.filter(residence -> !display.contains(residence) && checkOwner(residence))
.forEach(display::add);
.sorted(comparator).forEach(display::add);

display.stream().sorted(data.getSortFunction().residenceComparator(data.isSortReversed())).filter(r -> {
display.stream().filter(r -> {
ResidenceData d = Main.getInstance().getResidenceManager().getData(r);
return d.isPublicDisplayed() || (d.isOwner(getViewer()));
}).forEach(residence -> addItem(generateIcon(data, residence)));
Expand Down Expand Up @@ -251,7 +253,7 @@ interface ITEMS extends Configuration {
"&fSort order: %(order)",
"&fSort functions:",
"&7 &fRATINGS",
"&7 &2&lNAME",
"&7 &fNAME",
"&7 &a➥ &d&lSIZE",
" ",
"&a ▶ LClick &8|&f Switch sort function",
Expand Down

0 comments on commit c8d74aa

Please sign in to comment.