aspose file tools*
The moose likes Java in General and the fly likes Generic and serializable to String - possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic and serializable to String - possible?" Watch "Generic and serializable to String - possible?" New topic
Author

Generic and serializable to String - possible?

Maciej Wegorkiewicz
Greenhorn

Joined: Jun 30, 2003
Posts: 11
Hi I want to create class that is generic-customized and can be serialized to String. And you know what? Java does not allow this!
Or maybe does - please help me if someone knows how to do it.

Ok, I begin with creating interface that defines "serializable to string" classes:

interface StringSerializable
{
public StringSerializable fromString(String s);
public String toString();
}

Then, I want to define the class that is StringSerializable but can contain other classes that are StringSerializable:

class A<E extends StringSerializable> implements StringSerializable
{
E innerData=null;

public String toString()
{
return innerData.toString();
}

public A<E extends StringSerializable> fromString(String s)
{
E eObj=somehow create object of class E (how?)
innerData=eObj.fromString(s);
}
}

Sad surprise nr 1:
You cannot create eObj. It is normal because compiler does not know what constructors class E has. If we want to tell him using interface we meet

Sad surprise nr 2:
Interfaces do not allow defining constructors... The solution would be making fromString as static method to invoke just E.fromString but here we meet

Sad surprise nr 3:
Interfaces do not allow defining static methods... If we are so desperate that we are ready to create eObj by reflections we got

Sad surprise nr 4:
E.class expression does not work... you cannot then get reference to class of object eObj.

That is all I tried. I really see many interface constraints as not really intelligent. Maybe someone has a clue how to solve my problem?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
First, you code does not compile. You could declare the class in the following way:



Second, why do you think you need a generic type to implement this? What exactly are you trying to do?

Many solutions can be implemented with simple inheritance and composition. Just make sure you are not complicating your life in vain. Just make sure you are not trying to use a bazooka to kill a fly.
[ March 17, 2007: Message edited by: Edwin Dalorzo ]
Maciej Wegorkiewicz
Greenhorn

Joined: Jun 30, 2003
Posts: 11
Why generic type? Because I have to.

What I need is to serialize to String the class that contains objects of other classes that have to be serialized also. How can I deserialize them inside "fromString" method without generics? I cannot use inheritance because one of these inner objects have to be BigDecimals so I must use interface to make BigDecimals StringSerializable.

To be concrete, I have such class:

public class DiscreetTimeFunction<E extends StringSerializable>
implements StringSerializable
{
List<PolishDate> timepointStarts=new ArrayList<PolishDate>();
List<PolishDate> timepointEnds=new ArrayList<PolishDate>();
List<E> values=new ArrayList<E>();
(....)
}

I have to write to string whole object of this class, with lists of PolishDates (which inherit from GregorianCalendar) and E objects that can be BigDecimals or arrays of BigDecimals or other objects maybe in the future.
[ March 17, 2007: Message edited by: Maciej Wegorkiewicz ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You can provide your own external _factories_ that can serialize to and from a given class.


This way, instead of making classes that are responsible for creating instances of themselves from a String, you are defining a class that is responsible for the serialization of a particular class to and from a String. This means you would have to supply a StringSerializer upon construction of the class in the example you gave:



Could you find a way to make something like that work?
[ March 17, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Maciej Wegorkiewicz
Greenhorn

Joined: Jun 30, 2003
Posts: 11
Well Garrett, this is interesting idea. It is not the same exactly as I wanted but I think I will do it your way.

Factories would contain nothing more but invocations of toString/fromString methods from their objects as they have no access to private attributes, so the code would look a bit silly.

So thanks for answer.

Anyway I think the best situation would be to be allowed to define constructors or static methods in interfaces.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic and serializable to String - possible?