enumset

convert a two Byte bit mask into a EnumSet

久未见 提交于 2020-01-31 16:52:28
问题 I am reading a binary file that has values stored in bit masks, both 1 Byte bit masks and 2 Byte bit masks . Each bit in the masks act as a switch that indicates where an Event has transpired. Example of 1 Byte mask: 00000101 Indicates that Event one and Event 3 has transpired. Example of Enum public enum MyEnum { EventOne, EventTwo, ....; } I have created a Enum MyEnum (as per Item 32 in Effective java, Second Edition) of the events. How can the binary bit masks be read into an EnumSet

What does EnumSet really mean?

血红的双手。 提交于 2019-12-28 04:58:57
问题 I have the following example: import java.util.EnumSet; import java.util.Iterator; public class SizeSet { public static void main(String[] args) { EnumSet largeSize = EnumSet.of(Size.XL,Size.XXL,Size.XXXL); for(Iterator it = largeSize.iterator();it.hasNext();){ Size size = (Size)it.next(); System.out.println(size); } } } enum Size { S, M, L, XL, XXL, XXXL; } In this code I can understand that the Enum creates an Enum type of Sizes. My question is: is largeSize an object of EnumSet type? What

Convert an EnumSet to an array of integers

和自甴很熟 提交于 2019-12-24 11:51:27
问题 I have an EnumSet and want to convert it to array of its ordinal values. For example: enum MyEnum { A, B, C; } EnumSet enumSet = EnumSet.of(MyEnum.A, MyEnum.C); and what I want to get: [0, 2] 回答1: You should not use the raw type EnumSet without the <MyEnum> part EnumSet<MyEnum> enumSet = EnumSet.of(MyEnum.A, MyEnum.C); A Java 8 solution: MyEnum[] values = MyEnum.values(); int[] ordinals = IntStream.range(0, values.length).filter(i -> enumSet.contains(values[i])).toArray(); A pre-Java 8

EnumSet from array, shortest variant?

老子叫甜甜 提交于 2019-12-23 06:47:18
问题 I need an EnumSet from an array (which is given through a varargs method parameter). First, I was surprised that there is no varargs constructor method in EnumSet (there is EnumSet#of(E first, E... rest) ). As a workaround, I used the following variant: EnumSet<Options> temp = EnumSet.copyOf(Arrays.asList(options)); However, this triggers a java.lang.IllegalArgumentException: Collection is empty . So, now I ended up with the following, which looks somewhat ridiculous: EnumSet<Options> temp =

Why does EnumSet have many overloaded “of” methods?

为君一笑 提交于 2019-12-18 13:53:19
问题 While going through the EnumSet<E> of method, I have seen multiple overloaded implementations of of method: public static <E extends Enum<E>> EnumSet<E> of(E e) public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) . . public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5) and then another overloaded method with varargs public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) { EnumSet<E> result = noneOf(first.getDeclaringClass()); result.add(first);

Java: EnumSet.copyOf — is there a room for improvement?

*爱你&永不变心* 提交于 2019-12-13 00:04:07
问题 I need to create an EnumSet from a Set. I decided to use the EnumSet#copyOf method. However, because of a restriction on this method: the specified collection must contain at least one element (in order to determine the new enum set's element type) I need to ensure that the collection is not empty. The code then becomes: enum Color {RED, GREEN, BLUE}; Set<Color> set = ... // get it from somewhere if (set.isEmpty()) { return EnumSet.noneOf(Color.class); else return EnumSet.copyOf(set); Perhaps

EL syntax to check if a set contains a specific Enum value

只愿长相守 提交于 2019-12-12 01:43:11
问题 I have an Item object, which has a field that is a Set of ItemTypes: public class Item { EnumSet<ItemType> itemTypeSet; ... public Set<ItemType> getItemTypeSet(){ return this.itemTypeSet; } } ItemType is of course a simple Enum. public Enum ItemType { BOLD, THIN, COOL, ROUND; } In my JSP I would like to use JSTL to see if an item has a specific ItemType, I tried to use the following three snippets but I get no errors and no results. I'm not sure why all 3 are failing. Could somebody explain,

Convert between EnumSet and array of boolean values

血红的双手。 提交于 2019-12-10 14:39:17
问题 I have an EnumSet and want to convert back-and-forth to/from an array of boolean primitives. If it works better, I could work with a List instead of an array, and/or Boolean objects rather than boolean primitives. enum MyEnum { DOG, CAT, BIRD; } EnumSet enumSet = EnumSet.of( MyEnum.DOG, MyEnum.CAT ); What I want to get on the other end is an array that looks like this: [TRUE, TRUE, FALSE] This Question here is similar to this one, Convert an EnumSet to an array of integers. Differences:

Mapping EnumSet in Hibernate

左心房为你撑大大i 提交于 2019-12-07 18:40:20
问题 How to store EnumSet in the DB (using Hibernate)? @Entity public class A { public static enum SOME_ENUM { A, B, C }; private EnumSet<SOME_ENUM> myEnumSet = EnumSet.of(SOME_ENUM.A, SOME_ENUM.B); ... ... } If I try to persist the above, I get exception of course. I wanted to use @CollectionOfElements, but it is deprecated. Is there any alternative of @CollectionOfElements? Is there a way to store EnumSet in a single column without writing UserType? Thanks! 回答1: hibernate does not have built in

Java: How to write generic function that accepts Enum constants that implement a given interface?

一曲冷凌霜 提交于 2019-12-07 07:27:28
问题 So i have a bunch of enum's that all extend an interface: public interface MyInterface {} I then have several enums that extend the interface: public enum A implements MyInterface {} public enum B implements MyInterface {} I want a function that will accept only enum's that extend this interface. I cannot do: public void MyFunction(MyInterface input) because, inside the function, I create an EnumSet using EnumSet.of(input). I cannod do public <T extends Enum<T>> void myFunction(T input)