在Java中,如果字符串匹配,我们如何获取枚举的值或获取默认值?

huangapple go评论75阅读模式
英文:

How can we get enum's value if string matches or get default in Java?

问题

I have an enum class looking like below

@Slf4j
public enum PGPaymentStatus {

    ACTIVE(true) {
        @Override
        public <T> T accept(PaymentStatusVisitor<T> visitor) {
            return visitor.visitActive();
        }
    },
    PAID(false) {
        @Override
        public <T> T accept(PaymentStatusVisitor<T> visitor) {
            return visitor.visitPaid();
        }
    },
    EXPIRED(false) {
        @Override
        public <T> T accept(PaymentStatusVisitor<T> visitor) {
            return visitor.visitExpired();
        }
    },

    UNKNOWN(true) {
        @Override
        public <T> T accept(PaymentStatusVisitor<T> visitor) {
            return visitor.visitUnknown();
        }
    };

    public final boolean active;

    PGPaymentStatus(boolean active) {
        this.active = active;
    }

    public abstract <T> T accept(PaymentStatusVisitor<T> visitor);

    public interface PaymentStatusVisitor<T> {
        T visitActive();

        T visitPaid();

        T visitExpired();

        default T visitUnknown() {
            log.error("Unknown state received from the payment gateway");
            return null;
        }
    }
}

I am trying to translate java.lang.String to PGPaymentStatus enum using valueOf method as shown below.

PGPaymentStatus.valueOf(createOrderResponse.getOrderStatus())

Is there a way to get the default UNKNOWN value from the valueOf method or any other alternate when the order status string doesn't match any of the enums present?

英文:

I have an enum class looking like below

@Slf4j
public enum PGPaymentStatus {

    ACTIVE(true) {
        @Override
        public &lt;T&gt; T accept(PaymentStatusVisitor&lt;T&gt; visitor) {
            return visitor.visitActive();
        }
    },
    PAID(false) {
        @Override
        public &lt;T&gt; T accept(PaymentStatusVisitor&lt;T&gt; visitor) {
            return visitor.visitPaid();
        }
    },
    EXPIRED(false) {
        @Override
        public &lt;T&gt; T accept(PaymentStatusVisitor&lt;T&gt; visitor) {
            return visitor.visitExpired();
        }
    },

    UNKNOWN(true) {
        @Override
        public &lt;T&gt; T accept(PaymentStatusVisitor&lt;T&gt; visitor) {
            return visitor.visitUnknown();
        }
    };

    public final boolean active;

    PGPaymentStatus(boolean active) {
        this.active = active;
    }

    public abstract &lt;T&gt; T accept(PaymentStatusVisitor&lt;T&gt; visitor);

    public interface PaymentStatusVisitor&lt;T&gt; {
        T visitActive();

        T visitPaid();

        T visitExpired();

        default T visitUnknown() {
            log.error(&quot;Unknown state received from payment gateway&quot;);
            return null;
        }
    }
}

I am trying to translate java.lang.String to PGPaymentStatus enum using valueOf method as shown below.

PGPaymentStatus.valueOf(createOrderResponse.getOrderStatus())

Is there a way to get default UNKNOWN value from valueOf method or any other alternate when order status string doesn't match any of the enums present?

答案1

得分: 1

以下是翻译好的代码部分:

第一种选项是准备一个方法,该方法将迭代枚举值并在未找到时返回默认值:

public static PGPaymentStatus iterationFindByName(String name) {
    for (PGPaymentStatus status : PGPaymentStatus.values()) {
        if (name.equals(status.name())) {
            return status;
        }
    }
    return PGPaymentStatus.UNKNOWN;
}

更加优雅的方式是使用Guava库的Enums.getIfPresent方法:

Enums.getIfPresent(PGPaymentStatus.class, name).orElse(PGPaymentStatus.UNKNOWN);
英文:

The first option is to prepare a method that will iterate enum values and return default if not present:

public static PGPaymentStatus iterationFindByName(String name) {
for (PGPaymentStatus status : PGPaymentStatus.values()) {
if (name.equals(status.name())) {
return status;
}
}
return PGPaymentStatus.UNKNOWN;
}

The more elegant way is using the Guava library Enums.getIfPresent method.

Enums.getIfPresent(PGPaymentStatus.class, name).orElse(PGPaymentStatus.UNKNOWN);

huangapple
  • 本文由 发表于 2023年4月13日 20:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定