简体   繁体   English

具有不同数量参数的Java枚举实例

[英]Java enum instances with different number of arguments

Is there any way by which I can have an Enum in Java to accept different number of arguments so as to achieve this without using varargs. 我有没有办法让Java中的Enum接受不同数量的参数,以便在不使用varargs的情况下实现这一点。

public enum ValueType {
    LITERAL() {
        @Override
        String getRepresentation(String... args) {
            return MvelStringUtil.representAsLiteral(args[0]);
        }
    },

    NUMBER() {
        @Override
        String getRepresentation(String... args) {
            return args[0];
        }
    },

    //converts a string representation of a date to a java.util.Date representation, because this is what is required
    DATE() {
        @Override
        // should have two params, 1- date in string, 2- format of the passed string
        String getRepresentation(String... args) {
            // DateUtil.parse accepts dateInString and dateFormat.
            return DateUtil.parse(args[0], args[1]).toString();
        }
    };

    abstract String getRepresentation(String... args);
}

Here, LITERAL and NUMBER accept only one argument, that is, the target value, whereas the DATE instance accepts two. 这里,LITERAL和NUMBER只接受一个参数,即目标值,而DATE实例接受两个。 I went through several questions only to find out that this cannot be achieved using Generics since enums do not really support Generics upto that extent. 我经历了几个问题才发现使用Generics无法实现这一点,因为枚举并不能真正支持泛型。

Also, from a design perspective could I just not have all the types in an Enum rather have them in a class with some workarounds, keeping in mind that this ValueType instance needed to be unmarshalled from a json and getRepresentation method would be called on the unmarshalled enum instance to get the actual representation of the target value. 此外,从设计的角度来看,我可能只是没有Enum中的所有类型而是将它们放在具有一些变通方法的类中,请记住,需要从json解组的ValueType实例和getRepresentation方法将在unmarshalled上调用枚举实例以获取目标值的实际表示。

This is not possible because LITERAL, NUMBER and DATE conform to the interface of the enum ValueType. 这是不可能的,因为LITERAL,NUMBER和DATE符合枚举ValueType的接口。 If it were possible, type safety would be violated here: 如果可能的话,这里会违反类型安全:

LITERAL()
{
    @Override
    String getRepresentation(String args) {
        return MvelStringUtil.representAsLiteral(arg);
    }
}    
...
ValueType x = ValueType.LITERAL;
x.getRepresentation("a","b"); // the compiler could not know whether this is syntactically correct

As other answers say, it is not possible. 正如其他答案所说,这是不可能的。 Goal is to make sure you won't get wrong number of arguments for given type, so that bugs in calling code will be apperent soon, right? 目标是确保给定类型的参数数量不会错误,这样调用代码中的错误很快就会出现,对吧?

I'd go with simple validation in each getRepresentation method implementation. 我将在每个getRepresentation方法实现中进行简单验证 Check the array length and throw exception if incorrect . 检查数组长度, 如果不正确则抛出异常

That way validity rules are kept close to the subject, which is readable. 这样,有效性规则就会与主题保持一致,这是可读的。 You won't know mistakes at compile time, but at least you'll know them early in runtime. 你不会在编译时知道错误,但至少你会在运行时早期就知道错误。

Ad design perspective - I'd keep enum. 广告设计视角 - 我会保持枚举。 Ammount of types is limited, known at compile time and from quick inspection of the enum everyone will know what possibilities are there... 大量的类型是有限的,在编译时已知,并且通过快速检查枚举,每个人都会知道有什么可能性......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM