Skip to content

Commit

Permalink
* New math utility class for number formatting
Browse files Browse the repository at this point in the history
* Semi-finalized new PaginatedList maker
* Added PaginatedList instance to ComponentDecoration method args
* Marked PaginatedAssortment, SortedDoubleMap & SortedLongMap for removal
* Cleaned up old StringUtils usages
* Added default instance getter for UniformedComponents
  • Loading branch information
Hempfest committed May 5, 2021
1 parent 1ac0df3 commit 9788be3
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
@FunctionalInterface
public interface ComponentDecoration<T> {

void apply(T object, int page, int max, int placement);
void apply(PaginatedList<T> pagination, T object, int page, int max, int placement);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.sanctum.labyrinth.formatting;

import java.math.BigDecimal;
import java.math.MathContext;
import com.github.sanctum.labyrinth.library.MathUtils;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
* Encapsulate a list of objects to be sorted and paginated.
*
* @param <T> The object type representative of this pagination operation.
*/
public class PaginatedList<T> {
private final List<T> typeList;
private Comparator<? super T> comparable;
Expand All @@ -18,46 +22,89 @@ public PaginatedList(List<T> list) {
this.typeList = list;
}

protected double format(double amount) {
BigDecimal b1 = new BigDecimal(amount);
MathContext m = new MathContext(2);
BigDecimal b2 = b1.round(m);
return b2.doubleValue();
/**
* Format/trim a given amount to a specific length format.
*
* @param amount The amount to format.
* @param precision The math precision to stop the decimal placing at.
* @return The newly formatted double.
*/
public double format(Number amount, int precision) {
return MathUtils.use(amount).format(precision);
}

/**
* Setup a comparator for this list's sorting procedure.
*
* @param comparable The comparing operation to run for the given object type.
* @return The same paginated list procedure.
*/
public PaginatedList<T> compare(Comparator<? super T> comparable) {
this.comparable = comparable;
return this;
}

/**
* Provided the page number, total page count, object in queue & current paginated list instance,
* decorate any possible actions to take while iterating through the entries.
*
* @param decoration The primary execution to be ran for every entry given.
* @return The same paginated list procedure.
*/
public PaginatedList<T> decorate(ComponentDecoration<T> decoration) {
this.decoration = decoration;
return this;
}

/**
* Specify an amount of entries to be display per page.
* Lower entry counts will result in larger page counts.
*
* @param linesPerPage The amount of entries per page to display.
* @return The same paginated list procedure.
*/
public PaginatedList<T> limit(int linesPerPage) {
this.linesPerPage = linesPerPage;
return this;
}

/**
* Provided the page number and total page count, provide a starting
* sequence for the pagination.
*
* @param compliment The starting execution to be ran one time.
* @return The same paginated list procedure.
*/
public PaginatedList<T> start(StartingCompliment<T> compliment) {
this.start = compliment;
return this;
}

/**
* Provided the page number and total page count, provide a finishing
* sequence for the pagination.
*
* @param compliment The finishing execution to be ran one time.
* @return The same paginated list procedure.
*/
public PaginatedList<T> finish(FinishingCompliment<T> compliment) {
this.finish = compliment;
return this;
}

/**
* Run all prior sorting arrangements and sequence operations for a specified page.
*
* @param pageNum The page to to collect.
* @return A list of collected objects from the sorting procedure.
*/
public List<T> get(int pageNum) {
LinkedList<T> list = new LinkedList<>();
int page = pageNum;

int o = linesPerPage;

List<T> tempList = new LinkedList<>(this.typeList);

int totalPageCount = 1;
if ((tempList.size() % o) == 0) {
if (tempList.size() > 0) {
Expand All @@ -68,8 +115,6 @@ public List<T> get(int pageNum) {
}

if (page <= totalPageCount) {
// begin line


if (this.start != null) {
this.start.apply(pageNum, totalPageCount);
Expand All @@ -78,22 +123,22 @@ public List<T> get(int pageNum) {
if (!tempList.isEmpty()) {
int i1 = 0, k = 0;
page--;
tempList.sort(this.comparable);
if (this.comparable != null) {
tempList.sort(this.comparable);
}
LinkedList<T> sorted_list = new LinkedList<>(tempList);

for (T value : sorted_list) {

k++;
if ((((page * o) + i1 + 1) == k) && (k != ((page * o) + o + 1))) {
i1++;

if (decoration != null) {
decoration.apply(value, pageNum, totalPageCount, k);
decoration.apply(this, value, pageNum, totalPageCount, k);
list.add(value);
}
}
tempList.remove(value);

}
if (this.finish != null) {
this.finish.apply(pageNum, totalPageCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
import java.util.Comparator;
import java.util.Map;

/**
* @deprecated This class has no known use and is marked for removal.
*/
@Deprecated
public class SortedDoubleMap implements Comparator<String> {

Map<String,Double> base;
public SortedDoubleMap(Map<String,Double> base){
this.base = base;
}

Map<String, Double> base;

/**
* Note: this comparator imposes orderings that are inconsistent with equals.
*/
public int compare(String a,String b){
// sorting from high to low
if(base.get(a) > base.get(b)){
return -1;
}
if(base.get(a) < base.get(b)){
return 1;
}
// entries with the same values are sorted alphabetically
return a.compareTo(b);
}
}
public SortedDoubleMap(Map<String, Double> base) {
this.base = base;
}


/**
* Note: this comparator imposes orderings that are inconsistent with equals.
*/
public int compare(String a, String b){
// sorting from high to low
if(base.get(a) > base.get(b)){
return -1;
}
if(base.get(a) < base.get(b)){
return 1;
}
// entries with the same values are sorted alphabetically
return a.compareTo(b);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
import java.util.Comparator;
import java.util.Map;

/**
* @deprecated This class has no known use and is marked for removal.
*/
@Deprecated
public class SortedLongMap implements Comparator<String> {

Map<String,Long> base;
public SortedLongMap(Map<String,Long> base){
this.base = base;
}
Map<String, Long> base;

/**
* Note: this comparator imposes orderings that are inconsistent with equals.
*/
public int compare(String a,String b){
// sorting from high to low
if(base.get(a) > base.get(b)){
return -1;
}
if(base.get(a) < base.get(b)){
return 1;
}
// entries with the same values are sorted alphabetically
return a.compareTo(b);
}
}
public SortedLongMap(Map<String, Long> base) {
this.base = base;
}

/**
* Note: this comparator imposes orderings that are inconsistent with equals.
*/
public int compare(String a, String b){
// sorting from high to low
if(base.get(a) > base.get(b)){
return -1;
}
if(base.get(a) < base.get(b)){
return 1;
}
// entries with the same values are sorted alphabetically
return a.compareTo(b);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.sanctum.labyrinth.formatting;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -32,4 +33,62 @@ public abstract class UniformedComponents<T> implements Serializable {

public abstract T get(int index);

public static <T> UniformedComponents<T> accept(List<T> list) {
return new UniformedComponents<T>() {
private static final long serialVersionUID = -5158318648096932311L;

@Override
public List<T> list() {
return new ArrayList<>(list);
}

@Override
public List<T> sort() {
return list();
}

@Override
public List<T> sort(Comparator<? super T> comparable) {
List<T> list = list();
list.sort(comparable);
return list;
}

@Override
public Collection<T> collect() {
return list();
}

@Override
public T[] array() {
return null;
}

@Override
public <R> Stream<R> map(Function<? super T, ? extends R> mapper) {
return list().stream().map(mapper);
}

@Override
public Stream<T> filter(Predicate<? super T> predicate) {
return list().stream().filter(predicate);
}

@Override
public T getFirst() {
return list().get(0);
}

@Override
public T getLast() {
return list().get(Math.max(list().size() - 1, 0));
}

@Override
public T get(int index) {
return list().get(index);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

public class NewComponent {

private static String color(String text) {
return StringUtils.translate(text);
return StringUtils.use(text).translate();
}

private static String color(OfflinePlayer source, String text) {
return StringUtils.translate(source, text);
return StringUtils.use(text).translate(source);
}

public TextComponent textHoverable(String normalText, String hoverText, String hoverTextMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;

public class OldComponent {

private static String color(String text) {
return StringUtils.translate(text);
return StringUtils.use(text).translate();
}

private static String color(OfflinePlayer source, String text) {
return StringUtils.translate(source, text);
return StringUtils.use(text).translate(source);
}

public TextComponent textHoverable(String normalText, String hoverText, String hoverTextMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

/**
* @deprecated This class has been outmatched and over all replaced by the superior {@link com.github.sanctum.labyrinth.formatting.PaginatedList}
*/
@Deprecated
public class PaginatedAssortment {

private Collection<String> targetList;
Expand Down

0 comments on commit 9788be3

Please sign in to comment.