I'm writing a application that reads from a queue of messages from a legacy mainframe system.
Some characteristics of message in the queue:
- Message from the Q is always fixed length plain text : 64 char length
- Each index or group of index indicates some meaning full data
- The first 20 chars represent first name, next 20 surname, character following that represents gender, then next 8 chars denote date in yyyyMMdd format
I need to map these to Java objects. Here is the sample of what I'm doing.
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Slf4j
public class Solution {
public static void main(String[] args){
String input = "JOE BLOGG M19880101PX2018010199PNM";
log.info("Input length 64 = ",input.length());
log.info(empMapper(input).toString());
}
public static boolean boolMapper(char value){
return (value == 'Y') ? true:false;
}
public static LocalDate dateMapper(String value){
final String dateString = String.format("%s-%s-%s",value.substring(0,4),value.substring(4,6),value.substring(6));
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return LocalDate.parse(dateString, formatter);
}
public static Emp empMapper(String input){
final Emp emp = new Emp();
emp.setFirstName(input.substring(0,19).trim());
emp.setSurName(input.substring(19,39).trim());
emp.setGender(input.charAt(40));
emp.setDob(dateMapper(input.substring(41,49)));
emp.setEmpId(input.substring(49,61));
emp.setJobType(input.charAt(61));
emp.setShiftNeeded(boolMapper(input.charAt(62)));
emp.setEmpLevel(input.charAt(63));
return emp;
}
@Data
@NoArgsConstructor
public static class Emp{
private String firstName;
private String surName;
private char gender;
private LocalDate dob;
private String empId;
private char jobType;
private boolean shiftNeeded;
private char empLevel;
}
}
Output: Emp(firstName=JOE, surName=BLOGG, gender=M, dob=1988-01-01, empId=PX2018010199, jobType=P, shiftNeeded=false, empLevel=M)
My question is there a better solution for doing this from a performance point of view
String.format(...)
with a simple concatenation. Note that on compiling concatenated Strings in the form ofString s = string1 + string2;
will result in the compiler using aStringBuilder
anyway \$\endgroup\$dateMapper
. Why do you create another dash separated date wile you can parse the original one with ` DateTimeFormatter.ofPattern("yyyyMMdd ")` ? \$\endgroup\$