HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java Properties Wrapper

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
propertieswrapperjava

Problem

I've decided to make a wrapper class for the java.util.Properties class since, in its current state, it only allows for storing and reading String values.

Is there any way I can improve the following code?

```
public final class Prefs {

private static final Properties properties = new Properties();
private static OutputStream out;
public static final File test = new File("settings.properties");

public static void main(String[] args) throws IOException {
try {
set("hello", "5");

int hello = get("hello", 1337);
System.out.println(hello == 5);

set("hello", "34");
hello = get("hello", 1337);
System.out.println(hello == 34);

remove("hello");
hello = get("hello", 1337);
System.out.println(hello == 1337);
} finally {
save();
}
}

static {
try {
if (!test.exists()) {
test.createNewFile();
}
properties.load(new FileInputStream(test));
out = new FileOutputStream(test);
} catch (IOException e) {
e.printStackTrace();
}
}

public static T get(String key, T defaultValue) {
if (!properties.containsKey(key)) {
return defaultValue;
}
String value = properties.getProperty(key);
if (defaultValue instanceof Long) {
return (T) new Long(value);
} else if (defaultValue instanceof Integer) {
return (T) new Integer(value);
} else if (defaultValue instanceof Short) {
return (T) new Short(value);
} else if (defaultValue instanceof Byte) {
return (T) new Byte(value);
} else if (defaultValue instanceof Double) {
return (T) new Double(value);
} else if (defaultValue instanceof Float) {
return (T) new Float(value);
} else if (defaultValue instanceof Boo

Solution

-
Is the try-finally code block really required in your main() method?

-
Your get() fails if the default value is not any of those primitive wrapper classes. What if I want a BigDecimal instance? "1.0" can't be casted to that.

-
On a related note, you are relying on auto-boxing, which may lead to unexpected consequences:

// code compiles, but throws NullPointerException here
int npe = getValue("invalid-key", (Integer) null);


-
Maybe this is just a quick prototyping, but if you do decide to make this a full-fledged library class, you should not be having the main() method, the static initialization block, or your static fields. The fields should become class fields where Prefs needs to be instantiated. Testing should go into its own class.

-
On another related note, you don't necessarily need to create temporary files for testing. There is a Properties.load(Reader) method that will work with a StringReader, which can be used in turn to wrap your test Strings/lines.

-
I don't think there's a benefit to instantiating a FileOutputStream object immediately after you have load()-ed the file. Things may happen between the loading and storing of properties, so regardless of whether you have this object or not, the storing process may still fail afterwards. Also, it seems like you do not need to explicitly flush() after calling store() as that is already done according to the Javadoc:


After the entries have been written, the output stream is flushed. The output stream remains open after this method returns.

Code Snippets

// code compiles, but throws NullPointerException here
int npe = getValue("invalid-key", (Integer) null);

Context

StackExchange Code Review Q#105576, answer score: 3

Revisions (0)

No revisions yet.