What is the best practice for specifying flags in a Java method?
I saw SWT using int as bit fields, for example:
(example partially from Effective Java, 2nd ed. on page 159):
public class Text { public static final int STYLE_BOLD = 1 << 0;
and your customer request is as follows:
printText("hello", Text.STYLE_BOLD | Text.STYLE_ITALIC);
.. but this is discouraging since you can mix flags (int values) from different classes together without any compiler checks.
In the same book (Effective Java) I see the use of EnumSet, but then your custom call becomes:
printText("hello", EnumSet.of(Style.Bold, Style.ITALIC));
I find this a bit detailed, and I prefer the elegance of SWT.
Is there another alternative or are these basically two flavors you should choose?
java swt flags enumset
Roalt
source share