• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic and serializable to String - possible?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Maciej Wegorkiewicz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic