问题描述
正如我们所知,Java 16 带来了一些新功能,例如记录、密封接口和类,以及模式匹配。
今天我想在我的培训项目中使用它们。但是,我遇到了一个问题,也许我不明白什么。
所以情况看起来像在代表我的 Intellij Idea 项目的给定代码中: 我有包裹:客户和订单 客户端类是一个密封接口,具有三个实现:Regular、Vip 和 SuperVip:
package com.example.records.domain.client;
public sealed interface Client permits Client.Regular,Client.SuperVip,Client.Vip {
Limit currentLimit();
record Regular() implements Client {
@Override
public Client.Limit currentLimit() {
return Client.Limit.of((short) 10);
}
}
record Vip() implements Client {
@Override
public Client.Limit currentLimit() {
return Client.Limit.of((short) 20);
}
}
record SuperVip() implements Client {
@Override
public Client.Limit currentLimit() {
return Client.Limit.of((short) 100);
}
}
record Limit(short value) {
public Limit {
if (value < 0) throw new IllegalArgumentException("Values below 0 not allowed!");
}
static Limit of(short value) {
return new Limit(value);
}
}
}
Order 类是一个从 DDD 知道的简单聚合: 但是switch语句有问题:
package com.example.records.domain.order;
import com.example.records.domain.OrderId;
import com.example.records.domain.client.Client;
import com.example.records.domain.client.Client.*;
import com.example.records.shared.Money;
import com.example.records.shared.Result;
import java.util.LinkedList;
import java.util.List;
public class Order() {
private final OrderId orderId;
private final Client client;
private final LinesItems linesItems = LinesItems.empty();
Order(OrderId orderId,Client client) {
this.orderId = orderId;
this.client = client;
}
public Result add(LineItem lineItem) {
return switch (client) {
case Regular r -> addAnItemIfTheLimitIsNotExceeded(r.);
case Vip v -> addAnItemIfTheLimitIsNotExceeded(v.);
case SuperVip sv -> addAnItemIfTheLimitIsNotExceeded(sv.);
};
}
private Result addAnItemIfTheLimitIsNotExceeded(Limit limit) {
// Todo need impl
return Result.OK;
}
private static class LinesItems {
private final List<LineItem> lines = new LinkedList<>();
private LinesItems() {}
void add(LineItem lineItem) {
this.lines.add(lineItem);
}
Money cost() {
return lines.stream().map(LineItem::cost).reduce(Money.ZERO,Money::add);
}
static LinesItems empty() {
return new LinesItems();
}
}
}
record LineItem(Money cost) {}
其他类:
package com.example.records.domain;
public record OrderId(String value) {
public OrderId {
validate();
}
private void validate() {
if (value == null)
throw new IllegalArgumentException("Null value not allowed!");
if (value.isBlank())
throw new IllegalArgumentException("Blank value not allowed!");
}
}
package com.example.records.shared;
import java.math.BigDecimal;
public record Money(BigDecimal value) {
public static final Money ZERO = new Money(new BigDecimal("0.00"));
public Money add(Money money) {
return new Money(this.value.add(money.value));
}
}
package com.example.records.shared;
public sealed interface Result {
Success OK = new Success();
record Success() implements Result{}
record Failure(String message) implements Result {}
}
我收到“(预期的表达)” 我哪里做错了? (实验性功能开启,我已经安装了Java 16 open jdk)
解决方法
instanceof
中的类型模式是 Java 16 中的最终(非预览)特性。然而,switch
中的类型模式尚未在 Java 16 中出现;他们预计很快就会到达。
jeps 394 Gavin Bierman 的所有者已经提交了该功能的 JEP。
Pattern Matching for switch (Preview)
它还没有发布。
编辑:这里只是快速更新。此功能已被提议包含在 JDK 17 中。这是JEP 406 here