Skip to content

Commit 4fa6312

Browse files
authored
AppSettings: add printPreferences() method
1 parent c110698 commit 4fa6312

File tree

1 file changed

+109
-28
lines changed

1 file changed

+109
-28
lines changed

jme3-core/src/main/java/com/jme3/system/AppSettings.java

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2022 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -39,6 +39,8 @@
3939
import java.util.HashMap;
4040
import java.util.Map;
4141
import java.util.Properties;
42+
import java.util.logging.Level;
43+
import java.util.logging.Logger;
4244
import java.util.prefs.BackingStoreException;
4345
import java.util.prefs.Preferences;
4446

@@ -58,6 +60,8 @@ public final class AppSettings extends HashMap<String, Object> {
5860

5961
private static final long serialVersionUID = 1L;
6062

63+
private static final Logger logger = Logger.getLogger(AppSettings.class.getName());
64+
6165
private static final AppSettings defaults = new AppSettings(false);
6266

6367
/**
@@ -507,12 +511,25 @@ public void save(String preferencesKey) throws BackingStoreException {
507511
* @return the corresponding value, or 0 if not set
508512
*/
509513
public int getInteger(String key) {
510-
Integer i = (Integer) get(key);
511-
if (i == null) {
512-
return 0;
513-
}
514+
return getInteger(key, 0);
515+
}
514516

515-
return i.intValue();
517+
/**
518+
* Get an integer from the settings.
519+
* <p>
520+
* If the key is not set, or the stored value is not an Integer, then the
521+
* provided default value is returned.
522+
*
523+
* @param key the key of an integer setting
524+
* @param defaultValue the value to return if the key is not found or the
525+
* value is not an integer
526+
*/
527+
public int getInteger(String key, int defaultValue) {
528+
Object val = get(key);
529+
if (val == null) {
530+
return defaultValue;
531+
}
532+
return (Integer) val;
516533
}
517534

518535
/**
@@ -524,12 +541,25 @@ public int getInteger(String key) {
524541
* @return the corresponding value, or false if not set
525542
*/
526543
public boolean getBoolean(String key) {
527-
Boolean b = (Boolean) get(key);
528-
if (b == null) {
529-
return false;
530-
}
544+
return getBoolean(key, false);
545+
}
531546

532-
return b.booleanValue();
547+
/**
548+
* Get a boolean from the settings.
549+
* <p>
550+
* If the key is not set, or the stored value is not a Boolean, then the
551+
* provided default value is returned.
552+
*
553+
* @param key the key of a boolean setting
554+
* @param defaultValue the value to return if the key is not found or the
555+
* value is not a boolean
556+
*/
557+
public boolean getBoolean(String key, boolean defaultValue) {
558+
Object val = get(key);
559+
if (val == null) {
560+
return defaultValue;
561+
}
562+
return (Boolean) val;
533563
}
534564

535565
/**
@@ -541,12 +571,25 @@ public boolean getBoolean(String key) {
541571
* @return the corresponding value, or null if not set
542572
*/
543573
public String getString(String key) {
544-
String s = (String) get(key);
545-
if (s == null) {
546-
return null;
547-
}
574+
return getString(key, null);
575+
}
548576

549-
return s;
577+
/**
578+
* Get a string from the settings.
579+
* <p>
580+
* If the key is not set, or the stored value is not a String, then the
581+
* provided default value is returned.
582+
*
583+
* @param key the key of a string setting
584+
* @param defaultValue the value to return if the key is not found or the
585+
* value is not a string
586+
*/
587+
public String getString(String key, String defaultValue) {
588+
Object val = get(key);
589+
if (val == null) {
590+
return defaultValue;
591+
}
592+
return (String) val;
550593
}
551594

552595
/**
@@ -558,12 +601,25 @@ public String getString(String key) {
558601
* @return the corresponding value, or 0 if not set
559602
*/
560603
public float getFloat(String key) {
561-
Float f = (Float) get(key);
562-
if (f == null) {
563-
return 0f;
564-
}
604+
return getFloat(key, 0f);
605+
}
565606

566-
return f.floatValue();
607+
/**
608+
* Get a float from the settings.
609+
* <p>
610+
* If the key is not set, or the stored value is not a Float, then the
611+
* provided default value is returned.
612+
*
613+
* @param key the key of a float setting
614+
* @param defaultValue the value to return if the key is not found or the
615+
* value is not a float
616+
*/
617+
public float getFloat(String key, float defaultValue) {
618+
Object val = get(key);
619+
if (val == null) {
620+
return defaultValue;
621+
}
622+
return (Float) val;
567623
}
568624

569625
/**
@@ -573,7 +629,7 @@ public float getFloat(String key) {
573629
* @param value the desired integer value
574630
*/
575631
public void putInteger(String key, int value) {
576-
put(key, Integer.valueOf(value));
632+
put(key, value);
577633
}
578634

579635
/**
@@ -583,7 +639,7 @@ public void putInteger(String key, int value) {
583639
* @param value the desired boolean value
584640
*/
585641
public void putBoolean(String key, boolean value) {
586-
put(key, Boolean.valueOf(value));
642+
put(key, value);
587643
}
588644

589645
/**
@@ -603,7 +659,7 @@ public void putString(String key, String value) {
603659
* @param value the desired float value
604660
*/
605661
public void putFloat(String key, float value) {
606-
put(key, Float.valueOf(value));
662+
put(key, value);
607663
}
608664

609665
/**
@@ -960,7 +1016,7 @@ public void setSettingsDialogImage(String path) {
9601016
/**
9611017
* Enable or disable gamma correction. If enabled, the main framebuffer will
9621018
* be configured for sRGB colors, and sRGB images will be linearized.
963-
*
1019+
* <p>
9641020
* Gamma correction requires a GPU that supports GL_ARB_framebuffer_sRGB;
9651021
* otherwise this setting will be ignored.
9661022
*
@@ -1215,7 +1271,7 @@ public boolean isGammaCorrection() {
12151271

12161272
/**
12171273
* Allows the display window to be resized by dragging its edges.
1218-
*
1274+
* <p>
12191275
* Only supported for {@link JmeContext.Type#Display} contexts which
12201276
* are in windowed mode, ignored for other types.
12211277
* The default value is <code>false</code>.
@@ -1240,7 +1296,7 @@ public boolean isResizable() {
12401296

12411297
/**
12421298
* When enabled the display context will swap buffers every frame.
1243-
*
1299+
* <p>
12441300
* This may need to be disabled when integrating with an external
12451301
* library that handles buffer swapping on its own, e.g. Oculus Rift.
12461302
* When disabled, the engine will process window messages
@@ -1282,7 +1338,7 @@ public boolean isOpenCLSupport() {
12821338
/**
12831339
* Sets a custom platform chooser. This chooser specifies which platform and
12841340
* which devices are used for the OpenCL context.
1285-
*
1341+
* <p>
12861342
* Default: an implementation defined one.
12871343
*
12881344
* @param chooser the class of the chooser, must have a default constructor
@@ -1507,4 +1563,29 @@ public int getDisplay() {
15071563
public void setDisplay(int mon) {
15081564
putInteger("Display", mon);
15091565
}
1566+
1567+
/**
1568+
* Prints all key-value pairs stored under a given preferences key
1569+
* in the Java Preferences API to standard output.
1570+
*
1571+
* @param preferencesKey The preferences key (node path) to inspect.
1572+
* @throws BackingStoreException If an exception occurs while accessing the preferences.
1573+
*/
1574+
public static void printPreferences(String preferencesKey) throws BackingStoreException {
1575+
Preferences prefs = Preferences.userRoot().node(preferencesKey);
1576+
String[] keys = prefs.keys();
1577+
1578+
if (keys == null || keys.length == 0) {
1579+
logger.log(Level.WARNING, "No Preferences found under key: {0}", preferencesKey);
1580+
} else {
1581+
StringBuilder sb = new StringBuilder();
1582+
sb.append("Preferences for key: ").append(preferencesKey).append("\n");
1583+
for (String key : keys) {
1584+
// Retrieve the value as a String (default fallback for Preferences API)
1585+
String value = prefs.get(key, "[Value Not Found]");
1586+
sb.append(key).append(" = ").append(value).append("\n");
1587+
}
1588+
logger.log(Level.INFO, sb.toString());
1589+
}
1590+
}
15101591
}

0 commit comments

Comments
 (0)