问题描述
有可能以某种方式将Java中的某些枚举
值标记为package-private,即给予它们默认修饰符?
背景(仅仅是为了抢占另外的第一个评论What for?;))
我有一个任务
- 具有不同执行方式的对象和执行状态,决定下一个调用哪个方法。每个执行方法返回下一个被调用方法的执行状态(基本上是一个用于执行状态机的框架)。
我有一个 enum
包含所有可能的执行状态,但还包含一些待处理或失败的程序包内部状态,不能由执行方法返回
我知道我可以在一个单独的变量中使用自己的枚举来管理这些状态,但是这会使代码干净,因为它变为单个 switch
-statement into(至少)两个(可能是周围的 if
)。此外,我当然可以检查返回值,但是我宁愿甚至不会错误地提供。
但是,考虑到不同的意见和答案(特别是Marcelo,BlackVegetable和OldCurmudgeon),我已经提出了以下解决方法:
package-private 枚举包含所有值:
枚举PackagePrivateEnum {
PUBLIC_VALUE_1,
PUBLIC_VALUE_2,
PUBLIC_VALUE_3,
PACKAGE_PRIVATE_VALUE_1,
PACKAGE_PRIVATE_VALUE_2;
}
第二个 public 枚举只包含公共值,并直接映射到包私钥:
public enum PublicEnum {
PUBLIC_VALUE_1(PackagePrivateEnum.PUBLIC_VALUE_1) ,
PUBLIC_VALUE_2(PackagePrivateEnum.PUBLIC_VALUE_2),
PUBLIC_VALUE_3(PackagePrivateEnum.PUBLIC_VALUE_3);
final PackagePrivateEnum value;
private PublicEnum(PackagePrivateEnum value){
this.value = value;
}
}
现在,如果我有一个只允许的功能要返回一个公共值,我将其定义为:
public abstract PublicEnum returnSomething();
然后可以通过以下方式在包中使用它:
PackagePrivateEnum value = returnSomething()。value;
这隐藏了公众不需要的值,我相信同时最大限度地减少编码和性能 - 开销在包中(例如,没有开关或if语句,没有地图查找等,只需要一个 .value
)。实际上,使用像GWT这样的智能编译器,返回值应该可以得到内联,即使 .value
-lookup也被完全删除,即没有性能另外,为此,可以为不同的上下文定义一个大的集合枚举的不同允许子集的任意数量:我可以很容易地定义另一个 PublicEnum2
显示了与$ code> PackagePrivateEnum 完全不同的值。
Is it possible to somehow mark certain enum
values in Java as package-private, i.e. give them the default modifier?
Background (only to preempt the otherwise immediate first comment "What for?" ;) )
I have a Task
-object with different execution-methods and an execution-state that decides which method to call next. Each one of the execution-methods returns the execution-state of the next method to be called (basically a framework for executing a state-machine).
I have an enum
that contains all possible execution-states, but also contains a few "package-internal" states like "pending" or "failed" that should not be returnable by the execution-methods.
I know I could manage these states in a separate variable with its own enum, but that would make the code a lot less clean as it turns a single switch
-statement into (at least) two (and possibly a surrounding if
). Also, I could, of course, just check the return value, but I'd rather not even make wrong ones available in the first place.
Sounds like the simple answer is "No."
But, thinking about the different comments and answers (particularly by Marcelo, BlackVegetable and OldCurmudgeon), I have come up with the following workaround:
A package-private enum contains all values:
enum PackagePrivateEnum {
PUBLIC_VALUE_1,
PUBLIC_VALUE_2,
PUBLIC_VALUE_3,
PACKAGE_PRIVATE_VALUE_1,
PACKAGE_PRIVATE_VALUE_2;
}
A second public enum contains only the public values, and directly maps these to the package-private ones:
public enum PublicEnum {
PUBLIC_VALUE_1 (PackagePrivateEnum.PUBLIC_VALUE_1),
PUBLIC_VALUE_2 (PackagePrivateEnum.PUBLIC_VALUE_2),
PUBLIC_VALUE_3 (PackagePrivateEnum.PUBLIC_VALUE_3);
final PackagePrivateEnum value;
private PublicEnum(PackagePrivateEnum value) {
this.value = value;
}
}
Now, if I have a function that is only allowed to return one of the public values, I define it as:
public abstract PublicEnum returnSomething();
and can then use it in the package via:
PackagePrivateEnum value = returnSomething().value;
This hides the unwanted values from the public and, I believe, simultaneously minimizes coding- and performance-overhead inside the package (e.g. no switch- or if-statements, no Map-lookups, etc., just a .value
required). In fact, with a smart compiler like GWT, the return-value should probably get "inlined" to the point that even the .value
-lookup is removed completely, i.e. no performance-overhead at all.
Also, with this, it is possible to define an arbitrary number of different allowed subsets of a big collective enum for different contexts: I could easily define another PublicEnum2
that exposes an entirely different set of values from the PackagePrivateEnum
.
这篇关于枚举值在Java中的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!