R
Rhino
I'm trying something Eric Sosman suggested on another thread but I have
doubts about one of the idioms so I thought I'd check here and see if there
was a better way.
A method name pad() returns a special class called PadResult. PadResult is
defined as follows:
import CompletionStatus;
/**
* Simple holder class contains the result of invoking the pad() method.
*
* <p>If the pad() method encounters no errors along the way, it returns a
CompletionStatus set to NO_ERROR and a String containing the
* result of the padding of the input value. If the pad() method encounters
trouble along the way, it returns a CompletionStatus of ERROR
* and an error message.</p>
*/
public class PadResult {
CompletionStatus status;
String result;
public PadResult(CompletionStatus status, String result) {
this.status = status;
this.result = result;
}
public CompletionStatus getStatus() {
return status;
}
public String getResult() {
return result;
}
}
And here is the enum CompletionStatus:
/**
* This enum contains a list of possible completion statuses for methods.
*/
public enum CompletionStatus {
/** Constant for ERROR. */
ERROR,
/** Constant for NO_ERROR */
NO_ERROR;
}
The method is then invoked as follows:
PadResult padResult = PadUtils.pad("Footsy", ' ', 't', 3); //Pad Footsy with
trailing blanks and make the final result 3 characters long
Since this should fail because the requested final length would truncate the
input string, pad() will return CompletionStatus.ERROR and an error message
via PadResult.
I'm checking the value of the CompletionStatus as follows:
if (padResult.getStatus()==CompletionStatus.ERROR) {
System.out.println(padResult.getResult());
}
It's the if statement that concerns me here. This code works fine (I can
test for CompletionStatus.NO_ERROR by simply changlng the right side of the
if) but it doesn't look right. It reminds me of mistakes I made when I was
new to Java comparing Strings to each other via the == operator to see if
they had the same VALUE but discovering that == doesn't determine equality
of value.
I've tried using a switch() statement -
switch(padResult.getStatus()) {
case NO_ERROR:
System.out.println("The padded value is " + padResult.getResult());
break;
default:
System.err.println("Error!! --> " + padResult.getResult());
break;
}
That also works but is a little long for my tastes.
Is there a better way to do this if statement? If so, what is it?
doubts about one of the idioms so I thought I'd check here and see if there
was a better way.
A method name pad() returns a special class called PadResult. PadResult is
defined as follows:
import CompletionStatus;
/**
* Simple holder class contains the result of invoking the pad() method.
*
* <p>If the pad() method encounters no errors along the way, it returns a
CompletionStatus set to NO_ERROR and a String containing the
* result of the padding of the input value. If the pad() method encounters
trouble along the way, it returns a CompletionStatus of ERROR
* and an error message.</p>
*/
public class PadResult {
CompletionStatus status;
String result;
public PadResult(CompletionStatus status, String result) {
this.status = status;
this.result = result;
}
public CompletionStatus getStatus() {
return status;
}
public String getResult() {
return result;
}
}
And here is the enum CompletionStatus:
/**
* This enum contains a list of possible completion statuses for methods.
*/
public enum CompletionStatus {
/** Constant for ERROR. */
ERROR,
/** Constant for NO_ERROR */
NO_ERROR;
}
The method is then invoked as follows:
PadResult padResult = PadUtils.pad("Footsy", ' ', 't', 3); //Pad Footsy with
trailing blanks and make the final result 3 characters long
Since this should fail because the requested final length would truncate the
input string, pad() will return CompletionStatus.ERROR and an error message
via PadResult.
I'm checking the value of the CompletionStatus as follows:
if (padResult.getStatus()==CompletionStatus.ERROR) {
System.out.println(padResult.getResult());
}
It's the if statement that concerns me here. This code works fine (I can
test for CompletionStatus.NO_ERROR by simply changlng the right side of the
if) but it doesn't look right. It reminds me of mistakes I made when I was
new to Java comparing Strings to each other via the == operator to see if
they had the same VALUE but discovering that == doesn't determine equality
of value.
I've tried using a switch() statement -
switch(padResult.getStatus()) {
case NO_ERROR:
System.out.println("The padded value is " + padResult.getResult());
break;
default:
System.err.println("Error!! --> " + padResult.getResult());
break;
}
That also works but is a little long for my tastes.
Is there a better way to do this if statement? If so, what is it?