jmapper-core
jmapper-core copied to clipboard
How to do Index Mapping
How to map 1st element of a list of String to a String I want something like :- @JMap("${stringgs[1]}") private String field;
import com.googlecode.jmapper.JMapper;
import com.googlecode.jmapper.annotations.JMap;
import com.googlecode.jmapper.annotations.JMapConversion;
import java.util.List;
// Source class
class Source {
private List<String> stringgs;
public Source(List<String> stringgs) {
this.stringgs = stringgs;
}
public List<String> getStringgs() {
return stringgs;
}
public void setStringgs(List<String> stringgs) {
this.stringgs = stringgs;
}
}
// Destination class
class Destination {
@JMap("stringgs") // Reference the source list
private String field;
// Custom conversion method
@JMapConversion(from = "stringgs", to = "field")
public String conversion(List<String> sourceList) {
if (sourceList != null && sourceList.size() > 1) {
return sourceList.get(1); // Get 2nd element (index 1)
}
return null;
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
// Main class to test
public class Main {
public static void main(String[] args) {
// Create source data
List<String> strings = List.of("First", "Second", "Third");
Source source = new Source(strings);
// Create JMapper instance
JMapper<Destination, Source> mapper =
new JMapper<>(Destination.class, Source.class);
// Perform mapping
Destination destination = mapper.getDestination(source);
// Print result
System.out.println("Mapped field: " + destination.getField());
}
}