Level/src/main/java/bentobox/addon/level/database/object/TopTenData.java

78 lines
1.9 KiB
Java
Raw Normal View History

2018-08-04 08:36:04 +02:00
package bentobox.addon.level.database.object;
2018-04-03 03:20:26 +02:00
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
2018-03-11 20:06:31 +01:00
import com.google.gson.annotations.Expose;
2018-08-01 18:49:43 +02:00
import world.bentobox.bentobox.database.objects.DataObject;
/**
* This class stores and sorts the top ten.
* @author ben
*
*/
2018-01-07 20:25:24 +01:00
public class TopTenData implements DataObject {
2018-05-26 04:59:44 +02:00
// UniqueId is the world name
2018-03-11 20:06:31 +01:00
@Expose
2018-05-26 04:59:44 +02:00
private String uniqueId = "";
2018-03-11 20:06:31 +01:00
@Expose
2018-04-03 03:20:26 +02:00
private Map<UUID, Long> topTen = new LinkedHashMap<>();
public TopTenData() {}
2018-03-11 20:06:31 +01:00
public Map<UUID, Long> getTopTen() {
2018-04-03 03:20:26 +02:00
return topTen.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).limit(10)
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
2018-04-03 03:20:26 +02:00
public void setTopTen(Map<UUID, Long> topTen) {
this.topTen = topTen;
}
@Override
public String getUniqueId() {
return uniqueId;
}
@Override
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
/**
* Add level for this island owner or team leader, sort the top ten and limit to ten entries
* @param uuid - UUID of owner or team leader
* @param level - island level
*/
public void addLevel(UUID uuid, Long level) {
this.topTen.put(uuid, level);
}
/**
* Get the level for this UUID, or zero if the UUID is not found
* @param uuid
* @return island level
*/
public long getLevel(UUID uuid) {
if (topTen.containsKey(uuid))
return topTen.get(uuid);
return 0L;
}
/**
* Removes ownerUUID from the top ten
* @param ownerUUID
*/
public void remove(UUID ownerUUID) {
this.topTen.remove(ownerUUID);
}
}