Custom properties

V

visionset

I'm using the Preferences class and have some objects to represent with
Strings for storage.
I have for example a rectangle class that may have a string representation
"0,0,50,100".

Now saving a property is not a problem I just have a CustomProperty
interface and call toPropertyString().
But loading a property is a case of documenting that CustomProperty classes
must provide a no arg public contstructor, and then I can call
CustomProperty.fromPropertyString(myPropStr) to populate the object.

Now I'd really like to have immutable properties, and I realise I can have a
toImmutableInstance() method but are there any better solutions to my mai
goal?
 
T

Thomas Fritsch

visionset said:
I'm using the Preferences class and have some objects to represent with
Strings for storage.
I have for example a rectangle class that may have a string representation
"0,0,50,100".

Now saving a property is not a problem I just have a CustomProperty
interface and call toPropertyString().
So far, so good.
But loading a property is a case of documenting that CustomProperty
classes must provide a no arg public contstructor, and then I can call
CustomProperty.fromPropertyString(myPropStr) to populate the object.
As you already noticed below the interface method
public void fromPropertyString(String)
breaks immutability.
Now I'd really like to have immutable properties, and I realise I can have
a toImmutableInstance() method but are there any better solutions to my
goal?
I would modify your requirement (interface CustomProperty) as follows:
(*) The class has to provide a public constructor taking a String argument.
(*) The class does *not* have to provide a fromPropertyString(String)
method. (hence you don't have to give up immutability)

Obviously the loading from a string gets more clumsy then.
But at least one can tuck away that clumsiness into a utility method:
public static Object createFromPropertyString(String s) {
try {
Constructor ctor = c.getDeclaredConstructor(new Class[]{String.class};
return ctor.newInstance(new Object[]{s});
} catch(Exception e) {
throw new RuntimeException(e);
}
}
 
T

Thomas Fritsch

Thomas Fritsch wrote:
[...]
public static Object createFromPropertyString(String s) {
Oops, I meant
public static Object createFromPropertyString(Class c, String s) {
try {
Constructor ctor = c.getDeclaredConstructor(new Class[ {String.class});
return ctor.newInstance(new Object[]{s});
} catch(Exception e) {
throw new RuntimeException(e);
}
}
Usage example:
MyRectangle rect =
(MyRectangle) createFromPropertyString(MyRectangle.class, "0,0,50,100");
 
V

visionset

I would modify your requirement (interface CustomProperty) as follows:
(*) The class has to provide a public constructor taking a String
argument.
(*) The class does *not* have to provide a fromPropertyString(String)
method. (hence you don't have to give up immutability)

Obviously the loading from a string gets more clumsy then.
But at least one can tuck away that clumsiness into a utility method:
public static Object createFromPropertyString(String s) {
try {
Constructor ctor = c.getDeclaredConstructor(new
Class[]{String.class};
return ctor.newInstance(new Object[]{s});
} catch(Exception e) {
throw new RuntimeException(e);
}
}

Thanks, that makes more sense.
I can also Generify it which I really wanted to do.

static <T> CompoundProperty

createFromPropertyString(String propStr, Class<T> clazz) {

try {

Constructor ctor = clazz.getDeclaredConstructor(

new Class[]{String.class});

Object instance = ctor.newInstance(new Object[]{propStr});

return (CompoundProperty)instance;

} catch(Exception ex) {

throw new RuntimeException(ex);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top