Made LogEntry use the Builder pattern

This commit is contained in:
Florian CUNY 2019-01-03 13:51:46 +01:00
parent 245062b36e
commit fa3dbc5519
2 changed files with 47 additions and 14 deletions

View File

@ -1,5 +1,7 @@
package world.bentobox.bentobox.api.logs; package world.bentobox.bentobox.api.logs;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
/** /**
@ -11,20 +13,14 @@ import java.util.Map;
* @author Poslovitch * @author Poslovitch
*/ */
public class LogEntry { public class LogEntry {
private long timestamp; private final long timestamp;
private String type; private final String type;
private Map<String, Object> data; private final Map<String, Object> data;
public LogEntry(String type, Map<String, Object> data) { private LogEntry(Builder builder) {
this.timestamp = System.currentTimeMillis(); this.timestamp = builder.timestamp;
this.type = type; this.type = builder.type;
this.data = data; this.data = builder.data;
}
public LogEntry(long timestamp, String type, Map<String, Object> data) {
this.timestamp = timestamp;
this.type = type;
this.data = data;
} }
public long getTimestamp() { public long getTimestamp() {
@ -38,4 +34,41 @@ public class LogEntry {
public Map<String, Object> getData() { public Map<String, Object> getData() {
return data; return data;
} }
public static class Builder {
private long timestamp;
private String type;
private Map<String, Object> data;
public Builder(String type) {
this.timestamp = System.currentTimeMillis();
this.type = type.toUpperCase(Locale.ENGLISH);
this.data = new LinkedHashMap<>();
}
public Builder timestamp(long timestamp) {
this.timestamp = timestamp;
return this;
}
public Builder data(Map<String, Object> data) {
this.data = data;
return this;
}
/**
* Puts this key and this value in the currently existing data map.
* @param key key to set
* @param value value to set
* @return the Builder instance
*/
public Builder data(String key, Object value) {
this.data.put(key, value);
return this;
}
public LogEntry build() {
return new LogEntry(this);
}
}
} }

View File

@ -44,7 +44,7 @@ public class LogEntryListAdapter implements AdapterInterface<List<LogEntry>, Lis
String type = (String) entry.get(TYPE); String type = (String) entry.get(TYPE);
Map<String, Object> data = (Map<String, Object>) entry.get(DATA); Map<String, Object> data = (Map<String, Object>) entry.get(DATA);
result.add(new LogEntry(timestamp, type, data)); result.add(new LogEntry.Builder(type).timestamp(timestamp).data(data).build());
} }
return result; return result;