1
public enum TagEnum {
   MALE,FEMALE,//GENDER
   YOUNG,MIDDLE_AGED,OLD//AGE
}

I wanna use TagEnum to describe a Person. MALE and FEMALE are mutually exclusive, so are those enums for age.

For example, once you put MALE into the EnumSet, FEMALE will not be accepted.

Is there such a EnumSet?

==update==

Note that both of gender and age are used to describe a person. What's going on if I have more then 5 dimensions? To make 5 enums and 5 separate references?

Since I need a test method to know if someone own such an enum, it's redundant to make 5 overloaded methods like this:

boolean test(Age age);
boolean test(Gender gender);
boolean test(OtherDimension other);
//What a pity Enum can't be inherited

Is there a way to make it easier? Thanks!

Anderson
  • 2,496
  • 1
  • 27
  • 41
  • So are all the elements of this enum exclusive? Are you looking for an `EnumSet` which can't contain more than one element? – Simon MᶜKenzie Mar 20 '15 at 03:55
  • 1
    That's called "a single instance of an enum" :-) – Erwin Bolwidt Mar 20 '15 at 03:57
  • Does every `TagEnum` have one mutually exclusive `TagEnum`? – fps Mar 20 '15 at 04:20
  • No, Not all of them are exclusive. I've updated the question. @SimonMᶜKenzie – Anderson Mar 20 '15 at 05:47
  • Sorry, that's not what I want. @ErwinBolwidt – Anderson Mar 20 '15 at 05:57
  • @Anderson You have a problem in your data modeling. You should make two enums, one called `Gender` and one called `Age`. That's why you have a strange requirement for the Set. You just need two attributes in wherever you are refering to gender and age, one reference to an instance of `Gender` and one reference to an instance of `Age`. – Erwin Bolwidt Mar 20 '15 at 08:47
  • 1
    This sounds like an architectural problem - you really need 2 enums: One for age, and one for gender. This will naturally enforce your need for exclusivity. – Simon MᶜKenzie Mar 20 '15 at 08:50
  • @ErwinBolwidt Note that both of them are used to describe a person. What's going on if I have more than 5 dimensions? To make 5 enums and 5 separate references? Since I need a test method to know if someone own such an enum, it's redundant to make 5 methods again. – Anderson Mar 21 '15 at 07:39
  • @SimonMᶜKenzie What about more then 5 enums? I've updated the question. Thanks! – Anderson Mar 21 '15 at 07:40
  • 1
    You'll just be shifting the complexity into your `EnumSet`. Multiple overloads will be a lot simpler to maintain than a special set of rules around enum combinations. I'd go with the overloads. – Simon MᶜKenzie Mar 21 '15 at 08:33

2 Answers2

2

No, there is not. EnumSet, and Set more generally, is not the correct data structure for this.

Why do you think you need a Set in the first place? If all you want is mutual exclusivity, a single reference to a TagEnum will suffice.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

EnumSet is not an immutable Set, and it DO have methods like add and remove. It has numbers of Enum values after initialized. And you can add or remove other Enum values of same type with the initialization. Besides, the way of initializing an EnumSet is like this:

EnumSet<TagEnum> all = EnumSet.allOf(TagEnum.class);
EnumSet<TagEnum> none = EnumSet.noneOf(TagEnum.class);
EnumSet<TagEnum> range = EnumSet.range(TagEnum.MALE, TagEnum.FEMALE);
EnumSet<TagEnum> some = EnumSet.of(TagEnum.YOUNG,TagEnum.OLD);

Meanwhile, you can handle EnumSet like this:

for(TagEnum a : all){
    System.out.println(a);
}        
if(all.contains(TagEnum.MALE)){
    System.out.println("sure");
}

Hope these will help you.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • 1
    `EnumSet` is not an immutable `Set`, and it DO have methods like `add` and `remove`. – Anderson Mar 20 '15 at 06:19
  • @MattBall@Anderson check [here](http://man.lupaworld.com/content/develop/JDK_6.0_API_html_zh_CN/html/zh_CN/api/java/util/class-use/EnumSet.html). – SilentKnight Mar 20 '15 at 06:47
  • @SilentKnight We can't read mandarin. Here's the [Javadoc in English](http://docs.oracle.com/javase/7/docs/api/java/util/EnumSet.html). EnumSet implements `Set` so of course it has methods `add` and `remove`. They are required for any implementation of `Set`. – Erwin Bolwidt Mar 20 '15 at 08:45
  • @Anderson @ErwinBolwidt Hahaha, Feel sorry when realizeing my providing a Url linking to a Chinese webpage. And you are right, I think I missed `Methods inherited from interface java.util.Set`. – SilentKnight Mar 20 '15 at 09:37