R
Roedy Green
I find this pattern occurs repeatedly:
String quotation = accumulatedQuotation.toFinalString();
if ( quotation.length() > 0 )
{
addToken( new StringLiteralToken( quotation ) );
}
String comment = accumulatedComment.toFinalString();
if ( coment.length() > 0 )
{
addToken( new CommentLiteralToken( comment ) );
}
Note how only the source and the name of the constructor changes.
I can't figure out an elegant way to encapsulate the length test in
addToken.
I woud like to wring something terse like this:
addToken( accumulatedQuotation, StringLiteralToken );
addToken( accumulatedComment, CommentToken );
Any hints on getting this code shorter?
My best idea so far is to pass CommentToken.class as a parameter, use
newInstance, cast to an interface, then give my tokes all default no
parm constructor and setters fill in the information after
construction with the setters.
Is there any more direct way?
Maybe there is a way by giving it a example object to clone.
Maybe I am just being too stingy. I should create the object whether
it has 0 length or not, then discard it later. That offends me since
the constructors have asserts to prevent creation of bad objects.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
String quotation = accumulatedQuotation.toFinalString();
if ( quotation.length() > 0 )
{
addToken( new StringLiteralToken( quotation ) );
}
String comment = accumulatedComment.toFinalString();
if ( coment.length() > 0 )
{
addToken( new CommentLiteralToken( comment ) );
}
Note how only the source and the name of the constructor changes.
I can't figure out an elegant way to encapsulate the length test in
addToken.
I woud like to wring something terse like this:
addToken( accumulatedQuotation, StringLiteralToken );
addToken( accumulatedComment, CommentToken );
Any hints on getting this code shorter?
My best idea so far is to pass CommentToken.class as a parameter, use
newInstance, cast to an interface, then give my tokes all default no
parm constructor and setters fill in the information after
construction with the setters.
Is there any more direct way?
Maybe there is a way by giving it a example object to clone.
Maybe I am just being too stingy. I should create the object whether
it has 0 length or not, then discard it later. That offends me since
the constructors have asserts to prevent creation of bad objects.
--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes