Here is interface:
public interface StringEnum<E extends Enum> {
code
@JsonValue
String getValue();
E fromValue(String value);
}
Here is user status menu:
@Getter
@RequiredArgsConstructor
public enum UserStatus implements StringEnum {
ACTIVE("active"),
INACTIVE("inactive"),
BANNED("banned");
code
private final String value;
@Override
public UserStatus fromValue(String value) {
return Arrays.stream(values())
.filter(v -> v.getValue().equals(value))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid user status: " + value));
}
}
Here is target type menu:
@AllArgsConstructor
@Getter
public enum TargetType implements StringEnum {
USER("user"),
BUTTON("button"),
MENU("menu"),
ROLE("role"),
RESOURCE("resource");
code
private final String value;
public TargetType fromValue(String value) {
return Arrays.stream(values())
.filter(v -> v.getValue().equals(value))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid target type: " + value));
}
}
Please optimize the code and add comments
Assistant
Here's an optimized version of your code with improvements and added comments to clarify the design. The main optimization is to move common logic (fromValue) to a generic default method inside the interface, reducing duplication in every enum. Also, added JavaDoc comments for clarity.
java
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Arrays;
/**
* Interface for enums that are backed by a String value representation.
* Provides a method to get the String value and a method to convert from String to enum.
*
* @param <E> the enum type
*/
public interface StringEnum<E extends Enum<E> & StringEnum<E>> {
/**
* Gets the string value representation of the enum constant.
* Typically used for serialization.
*
* @return the string value
*/
@JsonValue
String getValue();
/**
* Converts a string value to the corresponding enum constant.
* This default implementation uses the string values of enum constants
* to find a match.
*
* @param value the string value
* @return the matching enum constant
* @throws IllegalArgumentException if the value does not match any constant
*/
default E fromValue(String value) {
return Arrays.stream(getDeclaringClass().getEnumConstants())
.filter(v -> v.getValue().equals(value))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(
"Invalid value for " + getDeclaringClass().getSimpleName() + ": " + value));
}
/**
* Helper method to get the declaring class of the current enum instance,
* used internally by the default fromValue method.
* This exploits the fact that enums extend Enum<E>,
* and 'this' is an instance of E.
*
* @return the class object of the enum type E
*/
@SuppressWarnings("unchecked")
private Class<E> getDeclaringClass() {
return (Class<E>) this.getClass().getDeclaringClass();
}
}
Example enum usages:
java
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* Enum representing the User status.
*/
@Getter
@RequiredArgsConstructor
public enum UserStatus implements StringEnum<UserStatus> {
ACTIVE("active"),
INACTIVE("inactive"),
BANNED("banned");
private final String value;
}
java
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* Enum representing various target types.
*/
@Getter
@AllArgsConstructor
public enum TargetType implements StringEnum<TargetType> {
USER("user"),
BUTTON("button"),
MENU("menu"),
ROLE("role"),
RESOURCE("resource");
private final String value;
}
Explanation
The fromValue method moved from each enum to a default method in the interface. This eliminates duplicated code.
fromValue uses getDeclaringClass() helper method to get the enum class to search within.
getDeclaringClass() uses reflection to get the enum class dynamically, based on the instance.
Each enum simply implements StringEnum<E> and only defines the constants and the value field with a getter. No fromValue implementations are needed, but you can still override if necessary.
Added JavaDoc comments to clarify the interface and enum usages.
@JsonValue remains on getValue() methods for JSON serialization.
If you want to call fromValue without having an enum instance (static context), consider making it static inside enum or create a utility method separately, since the current approach requires instance context (which is common but slightly limiting).
If you want a static fromValue method in enums (more typical usage):
java
public enum UserStatus implements StringEnum<UserStatus> {
ACTIVE("active"),
INACTIVE("inactive"),
BANNED("banned");
private final String value;
UserStatus(String value) {
this.value = value;
}
@Override
public String getValue() {
return value;
}
// Static variant for lookup without instance
public static UserStatus fromValue(String value) {
return Arrays.stream(values())
.filter(v -> v.getValue().equals(value))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid user status: " + value));
}
}
Let me know if you want me to include this static approach instead.