2016-08-22 19:31:03 +02:00
|
|
|
package me.impy.aegis.db;
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2016-08-22 19:31:03 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2017-12-04 21:21:31 +01:00
|
|
|
import java.util.Collections;
|
2016-08-22 19:31:03 +02:00
|
|
|
import java.util.List;
|
2018-03-13 18:30:47 +01:00
|
|
|
import java.util.UUID;
|
2016-08-22 19:31:03 +02:00
|
|
|
|
|
|
|
public class Database {
|
2017-08-26 21:15:53 +02:00
|
|
|
private static final int VERSION = 1;
|
2017-12-27 21:01:53 +01:00
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
private List<DatabaseEntry> _entries = new ArrayList<>();
|
2016-11-13 18:21:00 +01:00
|
|
|
|
2018-02-13 22:06:24 +01:00
|
|
|
public JSONObject serialize() throws Exception {
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONArray array = new JSONArray();
|
2017-08-26 21:15:53 +02:00
|
|
|
for (DatabaseEntry e : _entries) {
|
2016-11-13 18:21:00 +01:00
|
|
|
array.put(e.serialize());
|
|
|
|
}
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONObject obj = new JSONObject();
|
2017-08-26 21:15:53 +02:00
|
|
|
obj.put("version", VERSION);
|
2016-11-13 18:21:00 +01:00
|
|
|
obj.put("entries", array);
|
2018-02-13 22:06:24 +01:00
|
|
|
return obj;
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-13 22:06:24 +01:00
|
|
|
public void deserialize(JSONObject obj) throws Exception {
|
2017-08-26 21:15:53 +02:00
|
|
|
// TODO: support different VERSION deserialization providers
|
2016-11-13 18:21:00 +01:00
|
|
|
int ver = obj.getInt("version");
|
2017-08-26 21:15:53 +02:00
|
|
|
if (ver != VERSION) {
|
2016-11-13 18:21:00 +01:00
|
|
|
throw new Exception("Unsupported version");
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONArray array = obj.getJSONArray("entries");
|
|
|
|
for (int i = 0; i < array.length(); i++) {
|
2017-12-27 21:08:24 +01:00
|
|
|
DatabaseEntry entry = new DatabaseEntry(null);
|
2017-12-27 22:34:53 +01:00
|
|
|
entry.deserialize(array.getJSONObject(i));
|
2018-03-13 18:30:47 +01:00
|
|
|
addKey(entry);
|
2016-11-13 18:21:00 +01:00
|
|
|
}
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-13 18:30:47 +01:00
|
|
|
public void addKey(DatabaseEntry entry) {
|
|
|
|
if (tryGetKeyByUUID(entry.getUUID()) != null) {
|
|
|
|
throw new AssertionError("entry found with the same uuid");
|
|
|
|
}
|
2017-08-26 21:15:53 +02:00
|
|
|
_entries.add(entry);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 21:06:23 +01:00
|
|
|
public void removeKey(DatabaseEntry entry) {
|
2018-03-13 18:30:47 +01:00
|
|
|
entry = getKeyByUUID(entry.getUUID());
|
2017-08-26 21:15:53 +02:00
|
|
|
_entries.remove(entry);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 22:04:22 +01:00
|
|
|
public void replaceKey(DatabaseEntry newEntry) {
|
2018-03-13 18:30:47 +01:00
|
|
|
DatabaseEntry oldEntry = getKeyByUUID(newEntry.getUUID());
|
2018-01-02 14:36:56 +01:00
|
|
|
_entries.set(_entries.indexOf(oldEntry), newEntry);
|
2017-12-27 22:04:22 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 01:50:00 +01:00
|
|
|
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) {
|
|
|
|
Collections.swap(_entries, _entries.indexOf(entry1), _entries.indexOf(entry2));
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:06:23 +01:00
|
|
|
public List<DatabaseEntry> getKeys() {
|
2017-12-04 21:21:31 +01:00
|
|
|
return Collections.unmodifiableList(_entries);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
2018-01-02 14:36:56 +01:00
|
|
|
|
2018-03-13 18:30:47 +01:00
|
|
|
private DatabaseEntry tryGetKeyByUUID(UUID uuid) {
|
2018-01-02 14:36:56 +01:00
|
|
|
for (DatabaseEntry entry : _entries) {
|
2018-03-13 18:30:47 +01:00
|
|
|
if (entry.getUUID().equals(uuid)) {
|
2018-01-02 14:36:56 +01:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 18:30:47 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private DatabaseEntry getKeyByUUID(UUID uuid) {
|
|
|
|
DatabaseEntry entry = tryGetKeyByUUID(uuid);
|
|
|
|
if (entry == null) {
|
|
|
|
throw new AssertionError("no entry found with the same uuid");
|
|
|
|
}
|
|
|
|
return entry;
|
2018-01-02 14:36:56 +01:00
|
|
|
}
|
2017-05-03 21:08:38 +02:00
|
|
|
}
|