jmapper-core icon indicating copy to clipboard operation
jmapper-core copied to clipboard

How to do Index Mapping

Open arko2405 opened this issue 6 years ago • 1 comments

How to map 1st element of a list of String to a String I want something like :- @JMap("${stringgs[1]}") private String field;

arko2405 avatar Jan 23 '19 12:01 arko2405


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());
    }
}

ljluestc avatar Apr 06 '25 15:04 ljluestc