1

I basically want to have an enum with specific methods, e.g.

public interface PropertyTypeMethods {
    public int getTypeId();

    public void setTypeId(Integer typeId);
}

and something like

public enum BasePropertyTypes implements PropertyTypeMethods {

ONE, TWO;

private int typeId;

@Override
public int getTypeId() {
    return typeId;
}

@Override
public void setTypeId(Integer typeId) {
    this.typeId = typeId;
}

}

as well as an extended version of this enum, e.g.

public enum ExtendedPropertyTypes implements PropertyTypeMethods {
HUNDRED, THOUSEND; 
// same as above
}

which would result in ONE.setTypeId(1) ==> ONE.getTypeId()==1 //true. That is the basic concept. But now I want to call a generic method like e.g.

private <E extends Enum<E> & PropertyTypeMethods> void initEnum(Enum<E> curType) {
   // the below approach does not work :-/
   curType.setTypeId(1); // or any other reasonable value....

But somehow I cannot figure out what the correct method signature would be. Following this question I figured at least some part of the puzzle - but still don't get it for the method signature. Still it remains unclear how to specify curType properly in the signature to execute an appropriate call.

5
  • Why <E extends Enum<E> & PropertyTypeMethodes>? To set the type, <E extends PropertyTypeMethods> is enough. Commented Feb 16, 2018 at 12:36
  • 1
    Enums are supposed to be constant. If you want a mutable "enum", make it a plain old class. Commented Feb 16, 2018 at 12:37
  • Just create a class, give it a private or protected constructor and add constant instances. Voila, it's basically an mutable and extendable enum. You may also add a static method to provide all available instances of that class like values() on enums. Commented Feb 16, 2018 at 12:46
  • Like Andy said, enum values should be constants. I think what you really want is an EnumMap. Commented Feb 16, 2018 at 15:30
  • Well I need to review the other suggestion .... About the Why? The main problem is this enum is already cluttered all over the code. And now there is a breaking change to set the ids manually - instead of hard coded (as it was). Therefore the request... I would love to have it differently - from the start Commented Feb 17, 2018 at 13:07

2 Answers 2

2

This will work :

private <E extends Enum<E> & PropertyTypeMethods> void initEnum(E curType) {
  curType.setTypeId(1);
}

However, I don't think it's a good idea to make mutable Enums (they're meant to be labels over constant values, not state-carrying singletons). Furthermore, you shouldn' write methods that require an Enum parameter when all they should care about is its interface :

// This will be easier to work with down the road
private void initEnum(PropertyTypeMethods curType) {
  curType.setTypeId(1);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Upvote for pointing out the pointlessness of the whole thing ;-)
Thx the hint with the signature solved this issue. I don't see my situation as ideal - nevertheless it works with the existing code.
1

The correct signature would simply be

private void initEnum(PropertyTypeMethods onject) {
    object.setTypeId(1);
}

But as Andy Turner mentioned, enums are expected to be immutable, i.e. only final immutable field. Therefore, enums also have a constructor Enum Types.

If you have more complex enums, it is a common way to implement them as follows

public enum BasePropertyTypes implements PropertyTypeMethods {
   ONE (new PropertyTypeMethods() {
          @Override
          public int getTypeId() {
              return 1;
          }
     });

    private final PropertyTypeMethods m;

    BasePropertyTypes(PropertyTypeMethods m) {
       this.m = m;
    }

   @Override
   public int getTypeId()
   {
      return this.m.getTypeId();
   }
}

But from your example, I would suggest to review your actual issue. Propably enums are not the proper way at all.

1 Comment

Yeah, I need to review - but anyway this enum is used all over the code - and a breaking change reg. ID needs some other measures - which are not as intended at the very beginning.... See as well my other comment..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.