在增强型交换机的情况下不执行任何操作

发布于 2025-01-19 17:33:12 字数 1049 浏览 0 评论 0 原文

我有以下增强的 switch 情况

@Override
public MultivaluedMap<String, String> update(MultivaluedMap<String, String> incomingHeaders,
        MultivaluedMap<String, String> clientOutgoingHeaders) {

    switch (authMethod) {
        case BASIC -> clientOutgoingHeaders.add("Authorization", basicAuth("admin", "admin"));
        case BEARER -> clientOutgoingHeaders.add("Authorization", "Bearer" + " " + getAccessTokenFromKeycloak());
        case SKIP -> System.out.println(); // How can I remove this?
    }
    return clientOutgoingHeaders;
}

,其中 authMethod

enum AuthMethod{
        BASIC,
        BEARER,
        SKIP
    }

If authMethodis SKIP 我只是希望代码不执行任何操作。我不想删除这个案例。

我知道,我可以通过其他不同的方式解决这个问题,但我很好奇这是否适用于增强型交换机。

我也知道,我可以删除 SKIP 案例。这根本不是我想要的,因为我想澄清的是,在这种情况下 SKIP 不会执行任何操作。

这是我尝试过的,

case SKIP -> {};
case SKIP -> ();

在增强的 switch 语句的情况下,我怎样才能不执行任何操作?

I have following enhanced switch case

@Override
public MultivaluedMap<String, String> update(MultivaluedMap<String, String> incomingHeaders,
        MultivaluedMap<String, String> clientOutgoingHeaders) {

    switch (authMethod) {
        case BASIC -> clientOutgoingHeaders.add("Authorization", basicAuth("admin", "admin"));
        case BEARER -> clientOutgoingHeaders.add("Authorization", "Bearer" + " " + getAccessTokenFromKeycloak());
        case SKIP -> System.out.println(); // How can I remove this?
    }
    return clientOutgoingHeaders;
}

where as authMethod is a

enum AuthMethod{
        BASIC,
        BEARER,
        SKIP
    }

If authMethodis SKIP I simply want the code to do nothing. I don't want to remove the case.

I am aware, that I could work around this problem in other different ways, but I am curious if this works with an enhanced switch.

Also I am aware, that I could just remove the SKIP case. That is simply not what I want, because I want to make clear, that SKIP does nothing in that case.

This is what I have tried

case SKIP -> {};
case SKIP -> ();

How can I do nothing in a case of an enhanced switch statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪梦之恋 2025-01-26 17:33:12

这太接近了!

case SKIP -> {};

你只是多了一个分号!删除它并编译!

case SKIP -> {}

请参阅 SwitchRule 的语法rel="noreferrer">Java 语言规范

SwitchStatement:
switch ( Expression ) SwitchBlock

SwitchBlock:
{ SwitchRule {SwitchRule} } 
{ {SwitchBlockStatementGroup} {SwitchLabel :} }

SwitchRule:
SwitchLabel -> Expression ; 
SwitchLabel -> Block 
SwitchLabel -> ThrowStatement

请注意,如果它是一个表达式(例如 add 调用),则在其后面需要一个分号。如果您使用块,例如 {},则不应添加分号。

This is so close!

case SKIP -> {};

You just had one extra semicolon! Remove it and it compiles!

case SKIP -> {}

See the syntax for a SwitchRule in the Java Language Specification:

SwitchStatement:
switch ( Expression ) SwitchBlock

SwitchBlock:
{ SwitchRule {SwitchRule} } 
{ {SwitchBlockStatementGroup} {SwitchLabel :} }

SwitchRule:
SwitchLabel -> Expression ; 
SwitchLabel -> Block 
SwitchLabel -> ThrowStatement

Notice how, if it is an expression, like your add calls, you need a semicolon after it. If you use a block, like {}, you should not add a semicolon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文