Skip to content

Commit

Permalink
act: improve ParameterInput
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocnhan-tran1996 committed Jul 28, 2024
1 parent 0c1ff67 commit cdb6821
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import org.springframework.jdbc.core.SqlInOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnType;
Expand Down Expand Up @@ -46,8 +44,7 @@ public static <T> ParameterInput<T> withParameterName(
public final ParameterTypeValue withValues(T... values) {

this.values = Optional.ofNullable(values)
.map(Arrays::stream)
.map(Stream::toList)
.map(Arrays::asList)
.orElse(null);
return new ParameterTypeValue();
}
Expand All @@ -60,10 +57,7 @@ public ParameterTypeValue withValues(Collection<T> values) {

public ParameterTypeValue withValue(T value) {

this.values = value == null
? null
: List.of(value);
return new ParameterTypeValue();
return withValues(value);
}

public final class ParameterTypeValue {
Expand Down Expand Up @@ -129,18 +123,16 @@ public ParameterTypeValue withStructArray(String arrayTypeName, String structTyp
public Map<String, Object> toMap() {

var map = new HashMap<String, Object>();
map.put(getParameterName(), this.getTypeValue().orElse(null));
map.put(getParameterName(), this.getTypeValueOrNull());
return map;
}

public Object convert(Connection connection) {

try {

var value = this.getTypeValue();
if (value.isPresent()) {
if (this.getTypeValueOrNull() instanceof AbstractTypeValue sqlTypeValue) {

var sqlTypeValue = (AbstractTypeValue) value.get();
return sqlTypeValue.createTypeValue(connection, sqlTypeValue.getTypeName());
}

Expand Down Expand Up @@ -171,10 +163,11 @@ public SqlInOutParameter sqlInOutParameter() {
);
}

private Optional<Object> getTypeValue() {
private AbstractTypeValue getTypeValueOrNull() {

return Optional.ofNullable(values)
.map(v -> this.typeValue);
return values == null
? null
: this.typeValue;
}

private int getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public record LocalDateTimeToLocalDate() implements OracleConverter<LocalDateTim
@Override
public LocalDate convert(LocalDateTime source) {

return Optional.of(source)
return Optional.ofNullable(source)
.map(LocalDateTime::toLocalDate)
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public record BigDecimalToInteger() implements OracleConverter<BigDecimal, Integ
@Override
public Integer convert(BigDecimal source) {

return Optional.of(source)
return Optional.ofNullable(source)
.map(BigDecimal::intValue)
.orElse(null);
}
Expand Down

0 comments on commit cdb6821

Please sign in to comment.