APIv3/src/main/java/net/frozenorb/apiv3/model/Rank.java

88 lines
2.8 KiB
Java
Raw Normal View History

2016-06-16 16:29:33 +02:00
package net.frozenorb.apiv3.model;
2016-03-21 23:28:17 +01:00
2016-05-09 05:18:55 +02:00
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import com.mongodb.async.client.MongoCollection;
import com.mongodb.client.result.DeleteResult;
2016-06-16 06:08:44 +02:00
import fr.javatic.mongo.jacksonCodec.Entity;
import fr.javatic.mongo.jacksonCodec.objectId.Id;
2016-03-21 23:28:17 +01:00
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
2016-06-16 16:29:33 +02:00
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document;
2016-03-21 23:28:17 +01:00
2016-06-01 20:23:37 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2016-05-09 05:18:55 +02:00
import java.util.concurrent.TimeUnit;
2016-04-27 02:46:34 +02:00
2016-06-16 06:08:44 +02:00
@Entity
2016-03-21 23:28:17 +01:00
public final class Rank {
private static final MongoCollection<Rank> ranksCollection = APIv3.getDatabase().getCollection("ranks", Rank.class);
2016-05-09 05:18:55 +02:00
private static Map<String, Rank> rankCache = null;
private static List<Rank> rankAltCache = null;
2016-05-09 05:18:55 +02:00
private static long rankCacheUpdated = 0;
2016-03-21 23:28:17 +01:00
@Getter @Id private String id;
@Getter private int weight;
@Getter private String displayName;
@Getter private String gameColor;
@Getter private String websiteColor;
@Getter private boolean staffRank;
2016-04-08 13:12:31 +02:00
public static List<Rank> findAll() {
2016-05-09 05:18:55 +02:00
updateCacheIfNeeded();
return ImmutableList.copyOf(rankAltCache);
2016-04-08 13:12:31 +02:00
}
2016-03-21 23:28:17 +01:00
public static Rank findById(String id) {
updateCacheIfNeeded();
return rankCache.get(id);
2016-04-27 02:46:34 +02:00
}
public Rank() {} // For Jackson
2016-03-21 23:28:17 +01:00
2016-04-08 13:12:31 +02:00
public Rank(String id, int weight, String displayName, String gameColor, String websiteColor, boolean staffRank) {
2016-03-21 23:28:17 +01:00
this.id = id;
this.weight = weight;
this.displayName = displayName;
this.gameColor = gameColor;
this.websiteColor = websiteColor;
2016-04-08 13:12:31 +02:00
this.staffRank = staffRank;
2016-03-21 23:28:17 +01:00
}
2016-05-09 05:18:55 +02:00
private static void updateCacheIfNeeded() {
if (rankCache == null || (System.currentTimeMillis() - rankCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
Map<String, Rank> working = new HashMap<>();
List<Rank> workingAlt = new ArrayList<>();
List<Rank> allRanks = SyncUtils.blockMulti(ranksCollection.find());
allRanks.sort((a, b) -> Ints.compare(a.getWeight(), b.getWeight()));
2016-05-09 05:18:55 +02:00
for (Rank rank : allRanks) {
2016-05-09 05:18:55 +02:00
working.put(rank.getId(), rank);
workingAlt.add(rank);
2016-05-09 05:18:55 +02:00
}
rankCache = working;
rankAltCache = workingAlt;
2016-05-09 05:18:55 +02:00
rankCacheUpdated = System.currentTimeMillis();
}
}
public void insert() {
BlockingCallback<Void> callback = new BlockingCallback<>();
ranksCollection.insertOne(this, callback);
callback.get();
}
public void delete() {
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
ranksCollection.deleteOne(new Document("_id", id), callback);
callback.get();
}
2016-03-21 23:28:17 +01:00
}