-2

I have a current state where an enum MyType represent Type table with columns as:

ID
Name

And it's used to identify type using ID parameter with byId method:

public enum MyType {

FIRST_TYPE("First Type", 10),
SECOND_TYPE("Second Type", 20);
public static class Holder {
    static Map<Integer, MyType > idMap = new HashMap<>();
    private Holder() { }
}

private MyType(String name, Integer id) {
    this.name = name;
    this.id = id;
    Holder.idMap.put(id, this);
}

public String getName() {
    return name;
}

public static MyType byId(Integer id) {
    return Holder.idMap.get(id);
}

My new requirement is to support also values exists in Type table, I found answers for dynamic enum, but accept answer is not to do it

No. Enums are always fixed at compile-time. The only way you could do this would be to dyamically generate the relevant bytecode.

What will be a better solution for finding also values (mainly IDs) from database (for example ID 30)

 select ID from TYPE

Can I extends existing state instead of change it? can I add extra IDS from database using method?

EDIT

Even if I update as @StefanFischer suggested an interface which populate map with enum class and new database class, I still expect in code an enum return by byId method,

public interface MyType {
    public static class Holder {
        static Map<Integer, MyType> idMap = new HashMap<>();
        private Holder() { }
    }

    public default void add(MyType myType, Integer id) {
        Holder.idMap.put(id, myType);
    }


    public static MyType byId(Integer id) {
        return Holder.idMap.get(id);
    }
}
4

3 回答 3

7

如果我理解正确,要求是:

  • 具有提供一些预定义值的 MyType.byId(Integer id) 方法
  • 它还应该Type从数据库中的表动态扩展

所以一个枚举不能动态扩展,但我们可以切换到一个类。

因此,靠近您的代码可以编写如下内容:

import java.util.HashMap;
import java.util.Map;

public class MyType {

    static Map<Integer, MyType> idMap = new HashMap<>();
    static {
        idMap.put(10, new MyType("First Type"));
        idMap.put(20, new MyType("Second Type"));
    }

    private final String name;

    private MyType(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public static MyType byId(Integer id) {
        return idMap.get(id);
    }

    public static void addType(String name, Integer id) {
        MyType lookup = byId(id);
        if(lookup != null) {
            if(!lookup.getName().equals(name)) {
                System.out.println("conflicting redefinition for id " + id + ": '" + name + "' vs '" + lookup.name + "'");
                //handle...
            }
        }
        idMap.put(id, new MyType(name));
    }
}

测试数据

假设我们在数据库中有以下内容:

stephan=# select * from Type;
 id |    name     
----+-------------
 30 | Third Type
 10 | First Type
 20 | Second Type
(3 rows)

因此,在数据库中,我们有 id=10 和 id=20 的预定义类型,还有一个 id=30 的类型,默认情况下应用程序不知道。但是我们可以从数据库中填充类型。

测试用例

public static void main(String[] args) {
    try {
        Connection connection = createConnection();
        try (connection) {
            populateTypes(connection);
        }

        MyType type;

        type = MyType.byId(10);
        System.out.println(type.getName());

        type = MyType.byId(20);
        System.out.println(type.getName());

        type = MyType.byId(30);
        System.out.println(type.getName());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

JDBC 示例

使用什么实际的数据库技术来检索这些值并不重要。这是 JDBC 的示例:

private static void populateTypes(Connection connection)
        throws SQLException {

    String sql = "SELECT * FROM type";
    try (Statement st = connection.createStatement()) {
        try (ResultSet rs = st.executeQuery(sql)) {
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                MyType.addType(name, id);
            }
        }
    }
}

演示输出

First Type
Second Type
Third Type

那是你要找的吗?

于 2019-06-17T16:18:18.337 回答
6

一个明显的非答案:你试图强迫自己进入错误的兔子洞。

枚举的全部意义在于在编译时为您提供某些优势。final static Whatever在运行时,如果你有一个包含一些字段的类,或者一个具有不同常量的 Enum,这对 JVM 来说真的无关紧要。或者,如果您使用 anEnumSet与普通Set.

您使用枚举是因为它们允许您以更优雅的方式写下您的代码。

因此,在运行时生成枚举的方法没有意义。

枚举的想法是您使用它们编写源代码。但是,当您的枚举为您生成时,您将如何编写利用它们的源代码?!如前所述,枚举类默认是最终的。您不能单独扩展或增强它们。无论您想拥有什么,它都需要为您生成。这再次提出了一个问题:您将如何在编译时利用在运行时生成的东西?

因此,从概念的角度来看,另一个答案(使用 Map)中概述的方法比试图将枚举变成它们不应该成为的东西要好得多。

于 2019-06-21T10:53:56.457 回答
1

enum 表示一组常量(不可更改的变量,如最终变量)。你不能在运行时定义它。

于 2019-06-19T15:05:37.947 回答