views:

605

answers:

4

I used to have a custom preferences class for my applications. For my next hobby project i wanted to switch to the Preferences API. But the put and get functions require a default value and i do not want to spread default values all over the source files. Even though my project is small i can not imagine changing default values all over the source code. How do you guys use the api? I am thinking of wrapping the preferences api in another class but then what is the point of using the API because it only takes away the burden of saving the file to disk, which isn't that hard using serialization? Am i missing the point?

A: 

Would it be so hard to stick all your defaults in a single class so that they weren't littering your code?

I've used commons configuration in recent projects. I've looked into the Java Preferences API but I like the flexibility of the Commons project. And you don't have to specify default values!

Phill Sacre
+2  A: 

You're mixing a few concepts here. The default given in the code should be specific to the local situation as a 'reasonable default'. If you want to have application-wide defaults, then you need a preference-provider that allows you to hook in both the default preferences and an overlaid user-preferences. Something that may be a worthwhile project in itself.

Oh, and "reasonable defaults" is a great way to avoid configuration when it's not necessary but allow the user or packager to provide better values when needed.

@comment, I think I understand.

By 'local situation' I mean in the context of the code. For your GUI, you need a value for display that represents whatever the thread is using. Therefore I'd use something like Worker.DEFAULT_TIMEOUT. Your worker would use the same value internally as the default. That way you are retrieving the configured value or the worker's default when you are setting the worker's behavior.

caskey
A: 

what do you mean by local situation? my exact problem is say that i have a timer that goes off at intervals and i want user to set it through the GUI. my gui package needs it to set and show to the user and my thread package also needs it to configure itself even by now for one variable i have to set 3 default values.

Hamza Yerlikaya
+1  A: 

You can put default values in .preferences file which you bundle in your .jar file (or in specialized class or interface with constants).

I use it for things like window positions/sizes, remembering default folder for choosing files, last opened files and such trivia. I can think of some interesting things you get "for free" with preferences API:

  • doing things in OS-recommended way; OS might not allow you to write "settings files" in your app folder, and users don't like to be asked where on disk they want to save settings, and you don't want to implement your custom logic for every platform
  • your data is saved for each OS user separately
  • a way to keep user data even if your application is uninstalled (or during an upgrade)
  • don't need database or access to file system

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

Domchi