Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: Preferences API vs. Apache Commons Configuration

I need to allow the user to store/load an arbitrary number of lists of objects (assume they are Serializable). Conceptually I want a data model like

class FooBean { /* bean stuff here */ }

class FooList {
    final private Set<FooBean> items = new HashSet<FooBean>();

    public boolean add(FooBean item) { return items.add(item); }
    public boolean remove(FooBean item) { return items.remove(item); }
    public Collection<FooBean> getItems() { 
       return Collections.unmodifiableSet(items); 
    }
}

class FooStore {
    public FooStore() {
       /* something... uses Preferences or Commons Configuration */ 
    }
    public FooList load(String key) {
       /* something... retrieves a FooList associated with the key */ 
    }
    public void store(String key, FooList items) { 
       /* something... saves a FooList under the given key */
    }
}

Should I use the Preferences API or Commons Config? What's the advantages of each?

like image 255
Jason S Avatar asked Feb 23 '10 20:02

Jason S


2 Answers

Well, commons-configuration is, like many of apache project, an abstraction layer allowing one to seemlessly use preferences, an ldap store, properties files, and so on. So, your question could be rewritten as is : Will you need to change the format you use to store your preferences ? If no, java preferences are the way to go. Elsewhere, consider the portability of commons configuration.

like image 198
Riduidel Avatar answered Oct 06 '22 20:10

Riduidel


Given your example of storing a set of values associated with a key you would seem to have the following options when using each library

  • Preferences - store as a byte array associated with the key
  • Commons Configuration - store as a list of strings associated with a key

So the choice could come down to whether it is easier to convert a FooBean into a byte array or a String.

The other advantage of Commons Configuration is the different backends. I have used it to store properties in a database. If you want to store the objects somewhere other than the users local machine it would be the better choice.

like image 24
Mark Avatar answered Oct 06 '22 20:10

Mark