Fix parsing log entry types (#1438)

This commit is contained in:
Luck 2019-02-19 14:17:33 +00:00
parent beeb4fa169
commit 287cc308d6
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 30 additions and 1 deletions

View File

@ -102,6 +102,35 @@ public interface LogEntry extends Comparable<LogEntry> {
enum Type {
USER('U'), GROUP('G'), TRACK('T');
/**
* Parses a {@link Type} from a string.
*
* @param type the string
* @return a type
* @throws IllegalArgumentException if a type could not be parsed
* @since 4.4
*/
public static @NonNull Type parse(String type) {
try {
return valueOf(type);
} catch (IllegalArgumentException e) {
// ignore
}
try {
return valueOf(type.charAt(0));
} catch (IllegalArgumentException e) {
// ignore
}
throw new IllegalArgumentException("Unknown type: " + type);
}
/**
* Returns a {@link Type} by its code.
*
* @param code the code - see {@link Type#getCode()}.
* @return a type
* @throws IllegalArgumentException if a type could not be resolved
*/
public static @NonNull Type valueOf(char code) {
switch (code) {
case 'U':

View File

@ -58,7 +58,7 @@ public final class LogEntryJsonSerializer {
builder.actor(UUID.fromString(data.get("actor").getAsString()));
builder.actorName(data.get("actorName").getAsString());
builder.type(LogEntry.Type.valueOf(data.get("type").getAsString()));
builder.type(LogEntry.Type.parse(data.get("type").getAsString()));
if (data.has("acted")) {
builder.actor(UUID.fromString(data.get("acted").getAsString()));
}