Encapsulating Java Preferences API

I used to have a custom preference class for my applications. For my next hobby project, I wanted to switch to the settings API. But put and get functions require default values, and I don't want to propagate default values ​​in all source files. Despite the small size of my project, I cannot imagine changing the default values ​​throughout the source code. How do you guys use api? I think about packing api preferences in another class, but then what is the point of using the API, because it removes the burden of saving the file to disk, which is not so difficult with serialization? Am I missing a point?

+6
java encapsulation preferences
source share
4 answers

You mix a few concepts here. The default value specified in the code should be specific to the local situation as "reasonable default value". If you want to have generally accepted default values, you need a preference provider that allows you to connect to both the default settings and user preferences. Something that can be a worthy project in itself.

Oh, and “reasonable defaults” is a great way to avoid configuration when it's not necessary, but allow the user or packer to provide better values ​​if necessary.

@comment, I think I understand.

In the "local situation" I mean in the context of the code. For your GUI, you need a display value that represents what the stream is using. So I would use something like Worker.DEFAULT_TIMEOUT. Your worker will use the same default value. In this way, you retrieve the configured value or worker default by setting the worker behavior.

+2
source share

then you may need full control over how Preferences follows your desired repository by implementing AbstractPreferences . You can see the Linux based implementation:

http://www.docjar.com/html/api/java/util/prefs/FilePreferencesImpl.java.html

Good luck

+2
source share

Would it be so hard to stick to all of your default values ​​in one class so that they don't clog your code?

I have used community configuration in recent projects. I looked at the Java Preferences API, but I like the flexibility of the Commons project. And you do not need to specify default values!

+1
source share

You can put the default values ​​in a .preferences file that you link to your .jar file (or in a specialized class or interface with constants).

I use it for things like window position / size, remembering the default folder to select files, last opened files and such trifles. I can come up with some interesting things that you get “for free” with the API settings:

  • do things on the recommended OS; OS may not allow you to write “settings files” in your application’s folder, and users don’t like to ask where they want to save the settings on the disk, and you don’t want to implement your custom logic for each platform.
  • your data is stored for each OS user separately
  • way to save user data even if your application is deleted (or during the update)
  • No database or file system access needed.

Also, I do not like serialization and do not recommend it for this. Serialization means you have to take care of changing classes in new versions of your application.

+1
source share

All Articles