Is there an \"official\" recommendation of how to name Java enums?
enum Protocol { HTTP, HTTPS, FTP }
or
enum Protocols { HTTP,
In the same way that when you are defining a table name in a database or a class in Java you use singular for enums it's also the best option. Just see how you are going to use it.
Let's write an example:
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
class Appointment {
private Day day;
public void setDay(Day day) {
this.day = day;
}
}
In singular form you see clearly the intention of the day attribute. "day" its the day of the week this appointment is going to be held. Otherwise the signature of the method would have been setDay(Days day) and for sure a lot of people will be wondering if the appointment could happen in more than one day.
If you work in a company the has 48 hour shifts you could try to define something like:
public enum Days {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}
That way you could set the days you are going to work. But still it will look weird and there is a better option and is to use the singular form:
public enum Shift {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}
Now you really are showing the meaning of the enum. Usually in any domain you are going to find that using singular for an enum is the best option as each constant in the enum is just one element.
You also mention .NET. A "flags" enum in .NET just means that when you are expecting that enum as a parameter what you really get is a list of elements of that enum (stored as a integer).
// Define an Enum with FlagsAttribute.
[FlagsAttribute]
enum MultiHue : short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};
public void setMultiHue(MultiHue hues);
You could do the same in Java, but the enum still will be singular:
public enum Hue {
BLACK, RED, GREEN, BLUE;
private final Integer hue;
Hue() {
this.hue = 1 << this.ordinal();
}
public Integer toFlag() {
return this.hue;
}
}
public class MultiHue {
private Integer hues = 0;
public void addHue(Hue hue) {
this.hues |= hue.toFlag();
}
public boolean hasHue(Hue hue) {
return (this.hues & hue.toFlag()) != 0;
}
}
An easier and clearer (although it uses a more memory) to do this with Java is just to use a List.