package org.jdbi.v3.core.mapper;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Optional;
import org.jdbi.v3.core.config.ConfigRegistry;
import org.jdbi.v3.core.statement.StatementContext;
import static org.jdbi.v3.core.generic.GenericTypes.getErasedType;
class InternetMapperFactory implements ColumnMapperFactory {
private final Map<Class<?>, ColumnMapper<?>> mappers = new IdentityHashMap<>();
InternetMapperFactory() {
mappers.put(InetAddress.class, InternetMapperFactory::getInetAddress);
mappers.put(URL.class, new GetterMapper<>(ResultSet::getURL));
mappers.put(URI.class, new GetterMapper<>(InternetMapperFactory::getURI));
}
@Override
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
Class<?> rawType = getErasedType(type);
return Optional.ofNullable(mappers.get(rawType));
}
private static URI getURI(ResultSet r, int i) throws SQLException {
String s = r.getString(i);
try {
return s == null ? null : new URI(s);
} catch (URISyntaxException e) {
throw new SQLException("Failed to convert data to URI", e);
}
}
private static InetAddress getInetAddress(ResultSet r, int i, StatementContext ctx) throws SQLException {
String hostname = r.getString(i);
try {
return hostname == null ? null : InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
throw new MappingException("Could not map InetAddress", e);
}
}
}