/
SortingConfig.java
91 lines (78 loc) · 2.44 KB
/
SortingConfig.java
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
83
84
85
86
87
88
89
90
91
package mezz.jei.config.sorting;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public abstract class SortingConfig<T> {
private static final Logger LOGGER = LogManager.getLogger();
private final File file;
private final Collection<Consumer<List<T>>> listeners = new ArrayList<>();
@Nullable
private List<T> sorted;
public SortingConfig(File file) {
this.file = file;
}
abstract protected List<T> read(Reader reader) throws IOException;
abstract protected void write(FileWriter writer, List<T> sorted) throws IOException;
abstract protected Comparator<T> getDefaultSortOrder();
abstract protected Stream<T> generate();
private void save() {
List<T> sorted = getSorted();
try (FileWriter writer = new FileWriter(this.file)) {
write(writer, sorted);
} catch (IOException e) {
LOGGER.error("Failed to save to file {}", this.file, e);
}
}
private void load() {
final File file = this.file;
List<T> sortedOnFile = null;
if (file.exists()) {
try (FileReader reader = new FileReader(file)) {
sortedOnFile = read(reader);
} catch (IOException e) {
LOGGER.error("Failed to load from file {}", file, e);
}
}
Comparator<T> sortOrder = getDefaultSortOrder();
if (sortedOnFile != null) {
Comparator<T> existingOrder = Comparator.comparing(sortedOnFile::indexOf);
sortOrder = existingOrder.thenComparing(sortOrder);
}
final List<T> previousSorted = this.sorted;
this.sorted = generate()
.sorted(sortOrder)
.collect(Collectors.toList());
if (!Objects.equals(previousSorted, this.sorted)) {
List<T> unmodifiableList = Collections.unmodifiableList(this.sorted);
for (Consumer<List<T>> listener : this.listeners) {
listener.accept(unmodifiableList);
}
}
if (!Objects.equals(sortedOnFile, this.sorted)) {
save();
}
}
public List<T> getSorted() {
if (this.sorted == null) {
load();
}
return Collections.unmodifiableList(this.sorted);
}
public void addListener(Consumer<List<T>> listener) {
this.listeners.add(listener);
}
}