package org.jdbi.v3.core.statement;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
public class OutParameters {
private final Map<Object, Object> map = new HashMap<>();
public <T> T getObject(String name, Class<T> type) {
return type.cast(getObject(name));
}
public Object getObject(String name) {
return map.get(name);
}
public Object getObject(int position) {
return map.get(position);
}
public <T> T getObject(int pos, Class<T> type) {
return type.cast(getObject(pos));
}
public String getString(String name) {
Object obj = map.get(name);
if (obj == null) {
if (!map.containsKey(name)) {
throw new IllegalArgumentException(String.format("Parameter %s does not exist", name));
}
return null;
}
return obj.toString();
}
public String getString(int pos) {
Object obj = map.get(pos);
if (obj == null) {
if (!map.containsKey(pos)) {
throw new IllegalArgumentException(String.format("Parameter at %d does not exist", pos));
}
return null;
}
return obj.toString();
}
public byte[] getBytes(String name) {
Object obj = map.get(name);
if (obj == null) {
if (!map.containsKey(name)) {
throw new IllegalArgumentException(String.format("Parameter %s does not exist", name));
}
return null;
}
if (obj instanceof byte[]) {
return (byte[]) obj;
} else {
throw new IllegalArgumentException(String.format("Parameter %s is not byte[] but %s", name, obj.getClass()));
}
}
public byte[] getBytes(int pos) {
Object obj = map.get(pos);
if (obj == null) {
if (!map.containsKey(pos)) {
throw new IllegalArgumentException(String.format("Parameter at %d does not exist", pos));
}
return null;
}
if (obj instanceof byte[]) {
return (byte[]) obj;
} else {
throw new IllegalArgumentException(String.format("Parameter at %d is not byte[] but %s", pos, obj.getClass()));
}
}
public Integer getInt(String name) {
return getNumber(name).intValue();
}
public Integer getInt(int pos) {
return getNumber(pos).intValue();
}
public Long getLong(String name) {
return getNumber(name).longValue();
}
public Long getLong(int pos) {
return getNumber(pos).longValue();
}
public Short getShort(String name) {
return getNumber(name).shortValue();
}
public Short getShort(int pos) {
return getNumber(pos).shortValue();
}
public Date getDate(String name) {
Long epoch = getEpoch(name);
if (epoch == null) {
return null;
}
return new Date(epoch);
}
public Date getDate(int pos) {
Long epoch = getEpoch(pos);
if (epoch == null) {
return null;
}
return new Date(epoch);
}
public Timestamp getTimestamp(String name) {
Long epoch = getEpoch(name);
if (epoch == null) {
return null;
}
return new Timestamp(epoch);
}
public Timestamp getTimestamp(int pos) {
Long epoch = getEpoch(pos);
if (epoch == null) {
return null;
}
return new Timestamp(epoch);
}
public Double getDouble(String name) {
return getNumber(name).doubleValue();
}
public Double getDouble(int pos) {
return getNumber(pos).doubleValue();
}
public Float getFloat(String name) {
return getNumber(name).floatValue();
}
public Float getFloat(int pos) {
return getNumber(pos).floatValue();
}
private Number getNumber(String name) {
Object obj = map.get(name);
if (obj == null) {
if (!map.containsKey(name)) {
throw new IllegalArgumentException(String.format("Parameter %s does not exist", name));
}
return null;
}
if (obj instanceof Number) {
return (Number) obj;
} else {
throw new IllegalArgumentException(String.format("Parameter %s is not a number but %s", name, obj.getClass()));
}
}
private Number getNumber(int pos) {
Object obj = map.get(pos);
if (obj == null) {
if (!map.containsKey(pos)) {
throw new IllegalArgumentException(String.format("Parameter at %d does not exist", pos));
}
return null;
}
if (obj instanceof Number) {
return (Number) obj;
} else {
throw new IllegalArgumentException(String.format("Parameter at %d is not a number but %s", pos, obj.getClass()));
}
}
private Long getEpoch(String name) {
Object obj = map.get(name);
if (obj == null) {
if (!map.containsKey(name)) {
throw new IllegalArgumentException(String.format("Parameter %s does not exist", name));
}
return null;
}
if (obj instanceof java.util.Date) {
return ((java.util.Date) obj).getTime();
} else {
throw new IllegalArgumentException(String.format("Parameter %s is not Date but %s", name, obj.getClass()));
}
}
private Long getEpoch(int pos) {
Object obj = map.get(pos);
if (obj == null) {
if (!map.containsKey(pos)) {
throw new IllegalArgumentException(String.format("Parameter at %d does not exist", pos));
}
return null;
}
if (obj instanceof java.util.Date) {
return ((java.util.Date) obj).getTime();
} else {
throw new IllegalArgumentException(String.format("Parameter at %d is not Date but %s", pos, obj.getClass()));
}
}
Map<Object, Object> getMap() {
return map;
}
}