io.dropwizard.jersey.optional.OptionalIntParamConverterProvider Maven / Gradle / Ivy
package io.dropwizard.jersey.optional;
import javax.inject.Singleton;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.OptionalInt;
import static com.google.common.base.Preconditions.checkArgument;
@Singleton
public class OptionalIntParamConverterProvider implements ParamConverterProvider {
private final OptionalIntParamConverter paramConverter = new OptionalIntParamConverter();
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked")
public ParamConverter getConverter(final Class rawType, final Type genericType,
final Annotation[] annotations) {
return OptionalInt.class.equals(rawType) ? (ParamConverter) paramConverter : null;
}
public static class OptionalIntParamConverter implements ParamConverter {
@Override
public OptionalInt fromString(final String value) {
if (value == null) {
return OptionalInt.empty();
}
try {
return OptionalInt.of(Integer.parseInt(value));
} catch (NumberFormatException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public String toString(final OptionalInt value) {
checkArgument(value != null);
return value.isPresent() ? Integer.toString(value.getAsInt()) : "";
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy