1

I see this question

but I need to define a lot of enum type and do a lot of duplicate work, can every enum extends some thing? like this:

    public static enum Type {
        ;
        public final int value;

        Type(int v) {
            this.value = v;
        }

        public static Type fromInt(int v) {
            for (Type type : values()) {
                if (type.value == v) {
                    return type;
                }
            }
            return null;
        }
    }
    
    public static enum ENUM1 extends Type{
        A(1), B(5), C(10);
    }
    public static enum ENUM2 extends Type{
        D(1), E(20), F(30);
    }
ZerQAQ
  • 31
  • 1
  • 3

2 Answers2

0

As seen in this question, you cannot subclass enums in java. End of story. If you describe in detail what you want to achieve, people might suggest better solution than enums.

About parsing to enum from its' int value, what you have done will work, but it would be better to do something like this:

public enum MyEnum {

    A(1),
    B(2),
    C(3);

    private final int value;

    MyEnum(int value) {
        this.value = value;
    }

    private static Map<Integer, MyEnum> valueToEnumMap;

    public static MyEnum parseFromValue(int value) {
        if (valueToEnumMap == null) {
            valueToEnumMap = new HashMap<>();
            for (MyEnum myEnum : MyEnum.values()) {
                valueToEnumMap.put(myEnum.value, myEnum);
            }
        }
        return valueToEnumMap.get(value);
    }
}

Initialise a Map with keys the values of the enum, and values the enum itself, and get enums from this map. Depending on your use case, you might want to add check for duplicate keys, currently in case of duplication later pairs will overwrite previous ones.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
0

Alternatively, you could try this.

import java.util.Arrays;

/** Int <-> Enum. */
public class EnumToIntMap
{

   /**
    * 
    * Main method.
    * 
    * @param   args  commandline arguments, should they be needed.
    * 
    */
   public static void main(String[] args)
   {
   
      interface Type
      {
      
         int value();
         
         static <E extends Enum<E> & Type> E fromValue(Class<E> clazz, final int value)
         {
         
            return
               Arrays.stream(clazz.getEnumConstants())
                  .parallel()
                  .filter(each -> each.value() == value)
                  .findAny()
                  .orElseThrow()
                  ;
         
         }
      
      }
      
      
      enum Enum1 implements Type
      {
      
         A(1),
         B(5),
         C(10),
         ;
         
         private final int value;
         
         Enum1(int value)
         {
         
            this.value = value;
         
         }
         
         public int value()
         {
         
            return this.value;
         
         }
      
      }
      
      enum Enum2 implements Type
      {
      
         D(1),
         E(20),
         F(30),
         ;
      
         private final int value;
         
         Enum2(int value)
         {
         
            this.value = value;
         
         }
         
         public int value()
         {
         
            return this.value;
         
         }
      
      }
   
   }

}

davidalayachew
  • 1,279
  • 1
  • 11
  • 22