Added LogEntry and its database adapter

This commit is contained in:
Florian CUNY 2019-01-03 13:25:52 +01:00
parent 96bf67eb19
commit a638b09ce3
2 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package world.bentobox.bentobox.api.logs;
import java.util.Map;
/**
* Represents an event that occurred and that is logged.
* <br/>
* An {@link world.bentobox.bentobox.database.objects.adapters.AdapterInterface} is provided to be able to save/retrieve
* a list of instances of this object to/from the database: {@link world.bentobox.bentobox.database.objects.adapters.LogEntryListAdapter}.
*
* @author Poslovitch
*/
public class LogEntry {
private long timestamp;
private String type;
private Map<String, String> data;
public LogEntry(String type, Map<String, String> data) {
this.timestamp = System.currentTimeMillis();
this.type = type;
this.data = data;
}
public LogEntry(long timestamp, String type, Map<String, String> data) {
this.timestamp = timestamp;
this.type = type;
this.data = data;
}
public long getTimestamp() {
return timestamp;
}
public String getType() {
return type;
}
public Map<String, String> getData() {
return data;
}
}

View File

@ -0,0 +1,73 @@
package world.bentobox.bentobox.database.objects.adapters;
import world.bentobox.bentobox.api.logs.LogEntry;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author Poslovitch
*/
/* The serialization might look a bit weird here, as I'm using JSON's array of object.
This means that, once serialized, the data will look like this (on YAML):
history:
- timestamp: 0
type: "test"
data:
player: "uuid"
action: "ISLAND_LEVEL_UPDATED"
value: 45
- timestamp: 4181
type: "lol"
data:
help: "yep"
*/
public class LogEntryListAdapter implements AdapterInterface<List<LogEntry>, List<Map<String, Object>>> {
private static final String TIMESTAMP = "timestamp";
private static final String TYPE = "type";
private static final String DATA = "data";
@SuppressWarnings("unchecked")
@Override
public List<LogEntry> deserialize(Object object) {
List<LogEntry> result = new LinkedList<>();
if (object == null) {
return result;
}
List<Map<String, Object>> serialized = (List<Map<String, Object>>) object;
for (Map<String, Object> entry : serialized) {
long timestamp = (long) entry.get(TIMESTAMP);
String type = (String) entry.get(TYPE);
Map<String, String> data = (Map<String, String>) entry.get(DATA);
result.add(new LogEntry(timestamp, type, data));
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public List<Map<String, Object>> serialize(Object object) {
List<Map<String, Object>> result = new LinkedList<>();
if (object == null) {
return result;
}
List<LogEntry> history = (List<LogEntry>) object;
history.forEach(logEntry -> {
Map<String, Object> value = new LinkedHashMap<>();
value.put(TIMESTAMP, logEntry.getTimestamp());
value.put(TYPE, logEntry.getType());
value.put(DATA, logEntry.getData());
result.add(value);
});
return result;
}
}