Skip to content

Commit c1efadc

Browse files
Refac
1 parent be2c54b commit c1efadc

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

pacman-ui-lib/src/main/java/de/amr/pacmanfx/uilib/assets/UIPreferences.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public class UIPreferences {
2222

2323
public UIPreferences(Class<?> javaClass) {
2424
prefs = Preferences.userNodeForPackage(javaClass);
25+
defineDefaultValues();
2526
if (!isBackingStoreAccessible()) {
2627
Logger.error("User preferences could not be accessed, using default values!");
2728
} else {
2829
addMissingValues();
2930
}
3031
}
3132

33+
protected void defineDefaultValues() {
34+
}
35+
3236
public boolean isBackingStoreAccessible() {
3337
try {
3438
prefs.keys();
@@ -39,19 +43,19 @@ public boolean isBackingStoreAccessible() {
3943
}
4044
}
4145

42-
public void addMissingValues() {
46+
private void addMissingValues() {
4347
try {
4448
Set<String> prefKeys = new HashSet<>(Arrays.asList(prefs.keys()));
4549
List<String> allKeys = defaultValueMap.keySet().stream().sorted().toList();
4650
for (String key : allKeys) {
4751
if (!prefKeys.contains(key)) {
4852
Object defaultValue = defaultValueMap.get(key);
4953
storeValue(key, defaultValue);
50-
Logger.info("Added missing entry '{}'='{}'", key, defaultValue);
54+
Logger.info("Added missing preference '{}'='{}'", key, defaultValue);
5155
}
5256
}
5357
} catch (BackingStoreException x) {
54-
Logger.error("Could not access preferences to add missing keys");
58+
Logger.error("Could not access preferences store to add missing keys");
5559
Logger.error(x);
5660
}
5761
}
@@ -81,7 +85,7 @@ public void storeDefaultColor(String key, Color color) {
8185
}
8286

8387
public Color getColor(String colorKey) {
84-
String colorValue = prefs.get(colorKey, getDefaultValue(colorKey));
88+
String colorValue = prefs.get(colorKey, getDefaultValue(colorKey, String.class));
8589
return Color.web(colorValue);
8690
}
8791

@@ -112,21 +116,24 @@ public void storeDefaultFont(String key, Font font) {
112116

113117
public Font getFont(String key) {
114118
String familyKey = key + ".family";
115-
String family = prefs.get(familyKey, getDefaultValue(familyKey));
119+
String family = prefs.get(familyKey, getDefaultValue(familyKey, String.class));
116120

117121
String styleKey = key + ".style";
118-
String style = prefs.get(styleKey, getDefaultValue(styleKey));
122+
String style = prefs.get(styleKey, getDefaultValue(styleKey, String.class));
119123

120124
String sizeKey = key + ".size";
121-
Object defaultSize = getDefaultValue(sizeKey);
122-
float size = prefs.getFloat(sizeKey, defaultSize instanceof Float ? ((Float) defaultSize) : 8);
125+
float size = prefs.getFloat(sizeKey, getDefaultValue(sizeKey, Float.class));
123126

124127
return Font.font(family, FontWeight.findByName(style), size);
125128
}
126129

127-
@SuppressWarnings("unchecked")
128-
public <T> T getDefaultValue(String key) {
129-
return (T) defaultValueMap.get(key);
130+
public <T> T getDefaultValue(String key, Class<T> type) {
131+
Object defaultValue = defaultValueMap.get(key);
132+
if (type.isInstance(defaultValue)) {
133+
return type.cast(defaultValue);
134+
}
135+
throw new IllegalArgumentException("There is no default preference value of type %s for key '%s'"
136+
.formatted(type.getSimpleName(), defaultValue));
130137
}
131138

132139
private Object getValue(String key) {

pacman-ui/src/main/java/de/amr/pacmanfx/ui/PacManGames_Preferences.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class PacManGames_Preferences extends UIPreferences {
1515

1616
public PacManGames_Preferences() {
1717
super(PacManGames_Preferences.class);
18+
}
1819

20+
protected void defineDefaultValues() {
1921
storeDefaultValue("3d.bonus.symbol.width", 8.0f);
2022
storeDefaultValue("3d.bonus.points.width", 1.8f * 8.0f);
2123
storeDefaultValue("3d.energizer.radius", 3.5f);

0 commit comments

Comments
 (0)