Done ticket GEPAFINBE-3
This commit is contained in:
49
src/main/java/net/gepafin/tendermanagement/util/Utils.java
Normal file
49
src/main/java/net/gepafin/tendermanagement/util/Utils.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static final Logger log = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper()
|
||||
.registerModule(new JavaTimeModule())
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
public static <T, U> U convertObject(T source, Class<U> destinationClass) {
|
||||
try {
|
||||
return mapper.convertValue(source, destinationClass);
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting object: " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T, U> List<U> convertSourceListToDestinationList(List<T> sourceList, Class<U> destinationClass) {
|
||||
try {
|
||||
return sourceList.stream()
|
||||
.map(source -> mapper.convertValue(source, destinationClass))
|
||||
.collect(Collectors.toList());
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting list: " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T, U> List<U> convertSourceToList(T source, Class<U> destinationClass) {
|
||||
try {
|
||||
// Convert single source object to a single-element list of destination type
|
||||
return List.of(mapper.convertValue(source, destinationClass));
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting single object to list: " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user