Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G2-1607 Parse enums #131

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.file.Paths;
import java.util.ListIterator;

import com.g2forge.alexandria.java.core.enums.HEnum;
import com.g2forge.alexandria.java.fluent.optional.IOptional;
import com.g2forge.alexandria.java.fluent.optional.NullableOptional;

Expand Down Expand Up @@ -43,5 +44,20 @@ public IOptional<Object> getDefault(IParameterInfo parameter) {
public Object parse(IParameterInfo parameter, ListIterator<String> argumentIterator) {
return argumentIterator.next();
}
},
ENUM {
@Override
public IOptional<Object> getDefault(IParameterInfo parameter) {
if (parameter.getSubject().bind(Parameter.class).isPresent()) return NullableOptional.of(null);
return NullableOptional.empty();
}

@SuppressWarnings("unchecked")
@Override
public Object parse(IParameterInfo parameter, ListIterator<String> argumentIterator) {
@SuppressWarnings("rawtypes")
final Class<? extends Enum> cast = (Class<? extends Enum>) parameter.getType();
return HEnum.valueOfInsensitive(cast, argumentIterator.next());
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private IParameterParser computeParser(final Class<?> type) {
if (Path.class.equals(type)) return BasicParameterParser.PATH;
if (String.class.equals(type)) return BasicParameterParser.STRING;
if (Boolean.TYPE.equals(type) || Boolean.class.equals(type)) return BasicParameterParser.BOOLEAN;
if (Enum.class.isAssignableFrom(type)) return BasicParameterParser.ENUM;
if (type.isArray()) {
final Class<?> componentType = type.getComponentType();
final IParameterParser componentParser = computeParser(componentType);
Expand Down
Loading