What you are looking for cannot be done. The type of the value to be retrieved cannot be determined as long as you store it all in a Map<String, Object>.
Imagine you want to validate compile time that reads and writes use the correct type.
The writing could be easily done; you can simply wrap Map and provide 3 methods for put: put(String key, float value), put(String key, String value), put(String key, int value).
The reading could also be easily done, you just create a float getFloat(String key), int getInt(String key) and String getString(String key).
What you CANNOT validate is, compile time, that put("blah", 10) should not be combined with getFloat("blah").
Similarily, it is also impossible to specify that for a CERTAIN RANGE of input values, these are floats, and these are strings, and these are...
It cannot be done in a clean way.
...
Ah.
It can be done in a nasty way, though.
By, by creating our own enumsEnums to serve as keys.
First, create an interface for the key...
interface class StorageKey {
String getKey();
}
Make subclassesenums implementing this interface...
public classenum StorageKeyForString implements StorageKey {
private final String key;
private StorageKeyForString(String key){
this.key = Objects.requireNotNull(key, "null not allowed for key");
}
public String getKey(){
return key;
}
}
And add static instances.
public static final StorageKeyForString STR1 = new StorageKeyForString("str1");
With a similar classenum for Float values and Integer values, you could make everything compile-time safe... at the cost of so much flexibility that I personally think you'd be better off just... using a regular class.
I mean.... you have a bounded list of fields. You can literally store everything in a class, add getters and setters, and then just use the appropriate get and set call.
If you can choose runtime, you cannot validate compile time.
Failing all that, if you just want to improve your current setup, at least use overloading for that. Rename setProperty to _setProperty, ditch the type checks in _setProperty, and make 3 overloaded versions of setProperty which take a Integer value or a String value or a Float value. That'd at least get you type-safety for value writes.