Why does EnumSet have many overloaded “of” methods?

Varargs methods create an array. public static void foo(Object… args) { System.out.println(args.length); } This works, because of the implicit array creation. EnumSet is a class designed to be very, very fast, so by creating all the extra overloads they can skip the array creation step in the first few cases. This is especially true since … Read more

EnumSet from array, shortest variant?

This is two lines, but slightly less complex: EnumSet<Options> temp = EnumSet.noneOf(Options.class); // make an empty enumset temp.addAll(Arrays.asList(options)); // add varargs to it I don’t consider this worse than any other kind of variable declaration for a class which doesn’t have the constructor you want: SomeClass variable = new SomeClass(); // make an empty object … Read more

java.util.stream.Collectors with EnumSet Stream

You may use toCollection(Supplier): return ALL_OPTS.stream().filter(a -> (a.ameityId & bitFlag) > 0) .collect(Collectors.toCollection(() -> EnumSet.noneOf(Amenities.class))); The toCollection method receives a lambda which should create an empty collection to store the result. Here we create empty EnumSet using EnumSet.noneOf call. Note that for EnumSet you must always specify (implicitly or explicitly) which enum is this set … Read more

Implementing a bitfield using java enums

Instead of defining constructor parameters, you could simply use the internal ordinal() value to calculate this. public enum StatusFlag { DOCUMENT_STATUS_NOT_DEFINED, DOCUMENT_STATUS_OK, DOCUMENT_STATUS_MISSING_TID_DIR, DOCUMENT_STATUS_MISSING_TIF_FILE, DOCUMENT_STATUS_MISSING_PDF_FILE, DOCUMENT_STATUS_MISSING_OCR_FILE, DOCUMENT_STATUS_PAGE_COUNT_TIF, DOCUMENT_STATUS_PAGE_COUNT_PDF, DOCUMENT_STATUS_UNAVAILABLE; public long getStatusFlagValue(){ return 1 << this.ordinal(); } } Please note that now you should abstain from reordering, inserting (other than at the end) or deleting … Read more

What does EnumSet really mean?

As for any variable, its type is found in its declaration: EnumSet largeSize So yes, largeSize (which should be named largeSizes since it’s a collection) is of type EnumSet. It should also be generified, and thus be declared as EnumSet<Size> largeSizes What it means, is that largeSizes is of type EnumSet. An EnumSet is a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)