-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleExample.java
More file actions
140 lines (100 loc) · 5.38 KB
/
Copy pathSimpleExample.java
File metadata and controls
140 lines (100 loc) · 5.38 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package example;
import com.kopemorta.userpreferences.Config;
import com.kopemorta.userpreferences.UserPreferences;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.File;
import java.nio.file.Files;
import java.util.stream.Collectors;
public class SimpleExample {
private static final File PREF_FILE = new File("user.preferences.txt");
private static final Config config = Config.builder().build();
public static void main(String[] args) throws Throwable {
final SimpleExamplePref preferences = new SimpleExamplePref();
UserPreferences.init(config); // не обязательно, в нашем случае для проверки
UserPreferences.registerInstance(preferences); // регистрируем экземпляр, обязательно
{ // Проверяем стандартные настройки
Util.checkInstance(preferences, 123, 1234L, "my custom string field @!!##^%#^463*)^$%*#RF{F");
Util.checkFile(PREF_FILE, 123, 1234L, "my custom string field @!!##^%#^463*)^$%*#RF{F");
System.out.println(preferences);
}
{ // Обновляем настройки
preferences.setMyIntegerField(1234567);
preferences.setMyLongField(123456789123456789L);
preferences.setMyStringField("my new custom string field");
}
{ // Проверяем новые настройки
Thread.sleep(1111); // ждем, что бы файл обновится успел
Util.checkInstance(preferences, 1234567, 123456789123456789L, "my new custom string field");
Util.checkFile(PREF_FILE, 1234567, 123456789123456789L, "my new custom string field");
System.out.println(preferences);
}
{ // Обновите файл и нажмите любую кнопку
System.out.println("Обновите файл, что бы он выглядел следующим образом и нажмите ENTER:" + System.lineSeparator() +
"{\n" +
" \"myIntegerField\": 1,\n" +
" \"myLongField\": 12,\n" +
" \"myStringField\": \"STRING\"\n" +
"}");
System.in.read();
}
{ // Проверяем новые настройки
Thread.sleep(1111); // ждем, что бы файл обновится успел
Util.checkInstance(preferences, 1, 12, "STRING");
Util.checkFile(PREF_FILE, 1, 12, "STRING");
System.out.println(preferences);
}
{ // Обновляем настройки на стандартные, что бы убедится что при перезапуске всё работает
preferences.setMyIntegerField(123);
preferences.setMyLongField(1234L);
preferences.setMyStringField("my custom string field @!!##^%#^463*)^$%*#RF{F");
}
{ // Еще раз проверяем новые настройки
Thread.sleep(1111); // ждем, что бы файл обновится успел
Util.checkInstance(preferences, 123, 1234L, "my custom string field @!!##^%#^463*)^$%*#RF{F");
Util.checkFile(PREF_FILE, 123, 1234L, "my custom string field @!!##^%#^463*)^$%*#RF{F");
System.out.println(preferences);
}
// Если сюда дошли - значит всё работает хорошо
}
@EqualsAndHashCode(callSuper = false)
@ToString
@Getter
@Setter
private static class SimpleExamplePref implements UserPreferences {
private int myIntegerField = 123;
private long myLongField = 1234L;
private String myStringField = "my custom string field @!!##^%#^463*)^$%*#RF{F";
@Override
public File userPreferencesFile() {
return PREF_FILE;
}
}
private static class Util {
private static void checkInstance(final SimpleExamplePref simpleExamplePref,
final int integerField,
final long longField,
final String stringField) throws Exception {
if (simpleExamplePref.getMyIntegerField() != integerField ||
simpleExamplePref.getMyLongField() != longField ||
!simpleExamplePref.getMyStringField().equals(stringField)) {
throw new Exception("Bad data");
}
}
private static void checkFile(final File file,
final int integerField,
final long longField,
final String stringField) throws Exception {
final SimpleExamplePref simpleExamplePref = config.getGson()
.fromJson(Files.lines(file.toPath())
.collect(Collectors.joining(System.lineSeparator())), SimpleExamplePref.class);
if (simpleExamplePref.getMyIntegerField() != integerField ||
simpleExamplePref.getMyLongField() != longField ||
!simpleExamplePref.getMyStringField().equals(stringField)) {
throw new Exception("Bad data");
}
}
}
}