S
Stefan Ram
I have made up a little exercise.
I have a straightforward solution that I will post
later, but not before 2008-01-19T20:00:00+01:00.
There is a class »Util« implementing a verb »format«
that can format a sequence of texts in a certain way.
The requirements are that an object »util« of this
class can be used like
util.format( sequence )
to get the formatted text for the sequence »sequence«,
where the sequence »sequence« is either
- an array of texts or
- a list of texts,
where a »list« is a »java.util.List« object and a
text is a java.lang.CharSequence object.
The implementation of »format« contains two methods,
one for arrays and one for lists.
The body of both method declarations is exactly the same, so
the source code of the class »Util« contains much redundancy.
The exercise is to reduce this redundancy (repetition of code)
in a readable and simple way, but still allow for the same
call pattern »util.format( sequence )« for both arrays and lists.
class Util
{
public static java.lang.String indent( final java.lang.String text )
{ final java.lang.String text1 = text.replaceAll( "\n", "\n " );
return text1; }
public static java.lang.CharSequence format
( final java.lang.CharSequence[] source )
{ final java.lang.StringBuilder result;
final java.lang.StringBuilder text;
boolean first;
result = new java.lang.StringBuilder();
result.append( "Listing:\n" );
text = new java.lang.StringBuilder();
text.append( "[ " );
first = true;
for( final java.lang.CharSequence component : source )
{ if( first )first = false;
else text.append( "\n" );
text.append( component ); }
final char last = text.charAt( text.length() - 1 );
boolean space = true;
switch( last ){ case ' ': case ']': space = false; }
text.append( space ? " ]" : "]" );
result.append( indent( text.toString() ));
return result; }
public static java.lang.CharSequence format
( final java.util.List<java.lang.CharSequence> source )
{ final java.lang.StringBuilder result;
final java.lang.StringBuilder text;
boolean first;
result = new java.lang.StringBuilder();
result.append( "Listing:\n" );
text = new java.lang.StringBuilder();
text.append( "[ " );
first = true;
for( final java.lang.CharSequence component : source )
{ if( first )first = false;
else text.append( "\n" );
text.append( component ); }
final char last = text.charAt( text.length() - 1 );
boolean space = true;
switch( last ){ case ' ': case ']': space = false; }
text.append( space ? " ]" : "]" );
result.append( indent( text.toString() ));
return result; }}
I have a straightforward solution that I will post
later, but not before 2008-01-19T20:00:00+01:00.
There is a class »Util« implementing a verb »format«
that can format a sequence of texts in a certain way.
The requirements are that an object »util« of this
class can be used like
util.format( sequence )
to get the formatted text for the sequence »sequence«,
where the sequence »sequence« is either
- an array of texts or
- a list of texts,
where a »list« is a »java.util.List« object and a
text is a java.lang.CharSequence object.
The implementation of »format« contains two methods,
one for arrays and one for lists.
The body of both method declarations is exactly the same, so
the source code of the class »Util« contains much redundancy.
The exercise is to reduce this redundancy (repetition of code)
in a readable and simple way, but still allow for the same
call pattern »util.format( sequence )« for both arrays and lists.
class Util
{
public static java.lang.String indent( final java.lang.String text )
{ final java.lang.String text1 = text.replaceAll( "\n", "\n " );
return text1; }
public static java.lang.CharSequence format
( final java.lang.CharSequence[] source )
{ final java.lang.StringBuilder result;
final java.lang.StringBuilder text;
boolean first;
result = new java.lang.StringBuilder();
result.append( "Listing:\n" );
text = new java.lang.StringBuilder();
text.append( "[ " );
first = true;
for( final java.lang.CharSequence component : source )
{ if( first )first = false;
else text.append( "\n" );
text.append( component ); }
final char last = text.charAt( text.length() - 1 );
boolean space = true;
switch( last ){ case ' ': case ']': space = false; }
text.append( space ? " ]" : "]" );
result.append( indent( text.toString() ));
return result; }
public static java.lang.CharSequence format
( final java.util.List<java.lang.CharSequence> source )
{ final java.lang.StringBuilder result;
final java.lang.StringBuilder text;
boolean first;
result = new java.lang.StringBuilder();
result.append( "Listing:\n" );
text = new java.lang.StringBuilder();
text.append( "[ " );
first = true;
for( final java.lang.CharSequence component : source )
{ if( first )first = false;
else text.append( "\n" );
text.append( component ); }
final char last = text.charAt( text.length() - 1 );
boolean space = true;
switch( last ){ case ' ': case ']': space = false; }
text.append( space ? " ]" : "]" );
result.append( indent( text.toString() ));
return result; }}