在Java记录中强制执行不可变集合?
Java 记录用于实现浅不可变的数据载体类型。如果构造函数接受可变类型,那么我们应该实现显式防御性复制以强制不变性。例如
record Data(Set<String> set) {
public Data(Set<Thing> set) {
this.set = Set.copyOf(set);
}
}
这有点烦人 - 我们必须
- 实现一个老式的 POJO 构造函数(复制字段)而不是使用规范的构造函数和
- 显式初始化每个字段只是为了处理可变字段的防御性副本。
理想情况下,我们想要表达的是以下内容:
record SomeRecord(ImmutableSet<Thing> set) {
}
或者
record SomeRecord(Set<Thing> set) {
public SomeRecord {
if(set.isMutable()) throw new IllegalArgumentException(...);
}
}
这里我们使用一个虚构的ImmutableSet类型和Set::isMutable方法,在任何一种情况下,记录都是使用规范构造函数创建的 - 很好。不幸的是它不存在!
据我所知,内置集合类型(在 Java 10 中引入)是隐藏的,即无法确定集合是否不可变(除了尝试修改它)。
我们可以使用 Guava,但是当 99% 的功能已经在核心库中时,这似乎有点过分了。或者,有 Maven 插件可以测试注释为不可变的类,但这又是一个创可贴而不是解决方案。
是否有任何纯 Java 机制来强制执行不可变集合?
回答
你已经可以做到了,构造函数的参数是可变的:
record SomeRecord(Set<Thing> set) {
public SomeRecord {
set = Set.copyOf(set);
}
}
一个相关的讨论提到,为了允许这种防御性复制,这个论点不是最终的。确保equals()在进行此类复制时遵守规则仍然是开发人员的责任。
- That's neat, I hadn't known that that's possible in Records. It's also interesting to point out that `Set.copyOf()` doesn't actually do any copying if the input is already an immutable set created via `Set.of()` (or related methods). Just like the Guava immutable collections did.
- You can think of the compact constructor as an N-to-N transform on the parameters followed up by a call to the canonical constructor with the transformed values. This permits validation (fail on bad inputs), normalization (e.g. reduce fractions to lowest terms), and defensive copies. While it feels a little weird at first, this is exactly why it is there.
- @stridecolossus If your IDE is warning you when modifying a parameter of a compact record constructor, that's an IDE bug. While parameter modification is usually discouraged, compact constructors are an exception to this. You should file an RFE with your IDE.
- Interesting, it's a bit nasty essentially re-writing the arguments, but it does work! Have to switch off parameter modification warnings.
- @stridecolossus I agree it felt a bit weird when I found about it 🙂