Import values ​​from one Enum to another?

Now I have two Enum, which should only have synchronized values, and they are present in separate projects. I need to find a way to capture values ​​from one enum ( Enum1) and import them into ( Enum2).

What does it look like Enum1.java:

package com.example.hello;

import com.example.foo.SomeClass;

public enum Enum1 {
     A(1), B(2), C(3), D(4);

     integer i;

     Enum1(int ii) {
         this.i = ii;
     }

     public static int getValue(Enum1 e) {
         return e.i;
     }

     public int doSomething() {
         return SomeClass.doThings(i);
     }
}

Meanwhile Enum2.java(in another project) it looks like this:

package com.example.world;

import com.example.bar.SomeOtherClass;

import static com.example.hello.Enum1.*;

public enum Enum2 {
    // Enums *should* be imported from Enum1

    integer i;

    Enum2(int ii) {
        this.i = ii;
    }

    public int doSomethingElse() {
        return SomeOtherClass.doSomethingElse(i);
    }
}

Of course, this does not work, because the values ​​are not hyphenated.

The same could be used Enum1in my code, but the functions in Enums between the two projects should be completely different.

Am I trying to do what I'm trying to do? If so, how can I accomplish this in a (relatively) reasonable way?


. , , - Enum1.B. , Enum1 , , Enum2, Java Enum2, .

, , , - , , ( ) - , .

+4
1

. - , Enum2, Enum1 Enum2 . IDE .

3 :

  • , , ​​ Velocity Mustache
  • AST-based, , JCodeModel

, , JCodeModel Maven Groovy, .

+1

Source: https://habr.com/ru/post/1659671/


All Articles