英文:
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 <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 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 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论