Skip to content

Commit

Permalink
Add EnumMapperProvider (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyJayJay committed Apr 5, 2020
1 parent eb38e0f commit fc235f3
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.mela.command.provided.mappers.providers;

import io.github.mela.command.bind.CommandBindings;
import io.github.mela.command.bind.TargetType;
import io.github.mela.command.bind.map.ArgumentMapper;
import io.github.mela.command.bind.map.ArgumentMapperProvider;
import io.github.mela.command.provided.mappers.EnumMapper;
import java.lang.reflect.Type;
import javax.annotation.Nonnull;

/**
* @author Johnny_JayJay (https://www.github.com/JohnnyJayJay)
*/
public class EnumMapperProvider implements ArgumentMapperProvider {


@SuppressWarnings({"unchecked", "rawtypes"})
@Nonnull
@Override
public ArgumentMapper<?> provideFor(@Nonnull TargetType type, @Nonnull CommandBindings bindings) {
Class enumType = (Class) type.getType();
return new EnumMapper(enumType);
}

@Override
public boolean canProvideFor(@Nonnull TargetType targetType) {
Type type = targetType.getType();
if (!(type instanceof Class<?>) || type == Enum.class) {
return false;
}
Class<?> clazz = (Class<?>) type;
return Enum.class.isAssignableFrom(clazz);
}
}

0 comments on commit fc235f3

Please sign in to comment.