Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Custom properties

Thread view: 
visionset - 03 May 2007 13:55 GMT
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?

Signature

Mike W

Thomas Fritsch - 03 May 2007 15:06 GMT
> I'm using the Preferences class and have some objects to represent with
> Strings for storage.
[quoted text clipped - 3 lines]
> 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);
   }
 }

Signature

Thomas

Thomas Fritsch - 03 May 2007 15:29 GMT
[...]
>   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");

Signature

Thomas

visionset - 03 May 2007 17:39 GMT
> I would modify your requirement (interface CustomProperty) as follows:
> (*) The class has to provide a public constructor taking a String
[quoted text clipped - 13 lines]
>    }
>  }

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);

   }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.