Java: Enum parameter in method
Solution 1
This should do it:
private enum Alignment { LEFT, RIGHT };
String drawCellValue (int maxCellLength, String cellValue, Alignment align){
if (align == Alignment.LEFT)
{
//Process it...
}
}
Solution 2
Even cooler with enums you can use switch:
switch (align) {
case LEFT: {
// do stuff
break;
}
case RIGHT: {
// do stuff
break;
}
default: { //added TOP_RIGHT but forgot about it?
throw new IllegalArgumentException("Can't yet handle " + align);
}
}
Enums are cool because the output of the exception will be the name of the enum value, rather than some arbitrary int value.
Solution 3
I like this a lot better. reduces the if/switch, just do.
private enum Alignment { LEFT, RIGHT;
void process() {
//Process it...
}
};
String drawCellValue (int maxCellLength, String cellValue, Alignment align){
align.process();
}
of course, it can be:
String process(...) {
//Process it...
}
Solution 4
You could also reuse SwingConstants.{LEFT,RIGHT}. They are not enums, but they do already exist and are used in many places.
Solution 5
I am not too sure I would go and use an enum as a full fledged class - this is an object oriented language, and one of the most basic tenets of object orientation is that a class should do one thing and do it well.
An enum is doing a pretty good job at being an enum, and a class is doing a good job as a class. Mixing the two I have a feeling will get you into trouble - for example, you can't pass an instance of an enum as a parameter to a method, primarily because you can't create an instance of an enum.
So, even though you might be able to enum.process() does not mean that you should.
avinashse
Updated on January 18, 2020Comments
-
avinashse over 4 years
I have a method lets say:
private static String drawCellValue( int maxCellLength, String cellValue, String align) { }
and as you can notice, I have a parameter called align. Inside this method I'm going to have some if condition on whether the value is a 'left' or 'right'.. setting the parameter as String, obviously I can pass any string value.. I would like to know if it's possible to have an Enum value as a method parameter, and if so, how?
Just in case someone thinks about this; I thought about using a Boolean value but I don't really fancy it. First, how to associate true/false with left/right ? (Ok, I can use comments but I still find it dirty) and secondly, I might decide to add a new value, like 'justify', so if I have more than 2 possible values, Boolean type is definitely not possible to use.
Any ideas?
-
C. K. Young almost 15 years+1 The idea is on the right track. However, the process method should be abstract, and LEFT and RIGHT should each provide an implementation of it.
-
glglgl about 9 years@ChrisJester-Young That depends on their similiarity. If they are quite similiar, the possible values might only provide a private method which does a part of the implementation, and
process()
does the main work and calls these if required. -
C. K. Young about 9 years@glglgl Right, that works too. I just don't want to see code like
if (this == LEFT)
, that's all. :-)