Re: Enum Idiom Question
"Jeff Higgins" <oohiggins@yahoo.com> wrote in message
news:htpioj$rau$1@news.eternal-september.org...
On 5/28/2010 7:02 PM, Rhino wrote:
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?
<http://www.informit.com/articles/article.aspx?p=433387>
Interesting article; I wonder if we might call it a "poor man's version" of
the Robust Java book you mentioned earlier :-)
The first page is the most interesting for the situation I am trying to
code. He seems to be assuming (in the third example of page 1) that
chargeCustomerCard(variable1, variable2) is returning a boolean and nothing
else. That's fine and I understand the example for that case. But what about
the case where the method is returning something else? For instance, my
pad() method's main output is the string in the first input parameter,
padded out according to instructions in the other parameters. The initial
version of pad() returned only the padded string or threw an exception if
there were problems with the input parameters. But if I'm not going to throw
an exception and _do_ want to return an error message (or even a message
number that could be looked up), I tend to need something like this
PadResult class that I've concocted for this thread so that I can pass back
either an "it worked" and a padded String or an "it failed" and an error
message.
What do you think? Is PadResult or something like it my best alternative
here, assuming we agree that an exception is overkill for a case where an
input parameter is just miscoded?
--
Rhino