aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/cowtipper/cowlection/data/LogEntry.java
blob: aa59483881235ce44eec9f9584b8175d2c84493c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package de.cowtipper.cowlection.data;

import net.minecraft.util.EnumChatFormatting;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.regex.Pattern;

public class LogEntry {
    private static final Pattern UTF_PARAGRAPH_SYMBOL = Pattern.compile("§");
    private LocalDateTime time;
    private Path filePath;
    private String message;

    public LogEntry(LocalDateTime time, Path filePath, String logEntry) {
        this.time = time;
        this.filePath = filePath;
        this.message = logEntry;
    }

    public LogEntry(String message) {
        this.message = message;
    }

    public LocalDateTime getTime() {
        return time;
    }

    public Path getFilePath() {
        return filePath;
    }

    public String getMessage() {
        return message;
    }

    public void addLogLine(String logLine) {
        message += "\n" + logLine;
    }

    public void removeFormatting() {
        this.message = EnumChatFormatting.getTextWithoutFormattingCodes(message);
    }

    public void fixWeirdCharacters() {
        if (message.contains("§")) {
            message = UTF_PARAGRAPH_SYMBOL.matcher(message).replaceAll("§");
        }
    }

    /**
     * Is this log entry a 'real' log entry or just an error message from the search process?
     *
     * @return true if error message, otherwise false
     */
    public boolean isError() {
        return time == null && filePath == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LogEntry logEntry = (LogEntry) o;
        return new EqualsBuilder()
                .append(time, logEntry.time)
                .append(filePath, logEntry.filePath)
                .append(message, logEntry.message)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(time)
                .append(filePath)
                .append(message)
                .toHashCode();
    }
}