Created
April 17, 2021 17:15
-
-
Save EvgeniGordeev/b1fff655ad02edf26c3e3bb1c0dc7430 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.time.Duration; | |
import java.time.LocalDate; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
/** | |
* EmploymentGaps. | |
* | |
* @author evgeni.gordeev | |
* @since 4/17/21 | |
*/ | |
public class EmploymentGaps { | |
static class Employment { | |
String type; | |
String start_date; | |
String end_date; | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
public String getStart_date() { | |
return start_date; | |
} | |
public void setStart_date(String start_date) { | |
this.start_date = start_date; | |
} | |
public String getEnd_date() { | |
return end_date; | |
} | |
public void setEnd_date(String end_date) { | |
this.end_date = end_date; | |
} | |
} | |
public List<Employment> findGaps(List<Employment> employments) { | |
if (employments == null || employments.isEmpty()) { | |
return Collections.emptyList(); | |
} | |
employments.sort(Comparator.comparing(Employment::getStart_date)); | |
Employment prev = employments.get(0); | |
LocalDate maxEndDate = LocalDate.parse(prev.end_date); | |
for (int i = 1; i < employments.size(); i++) { | |
Employment curr = employments.get(i); | |
LocalDate date1 = LocalDate.parse(prev.end_date); | |
if (date1.isAfter(maxEndDate)) { | |
maxEndDate = date1; | |
} | |
LocalDate date2 = LocalDate.parse(curr.start_date); | |
if (date2.isAfter(maxEndDate) && Duration.between(date1.atStartOfDay(), date2.atStartOfDay()).toDays() > 30) { | |
Employment gap = new Employment(); | |
gap.setType("employment_gap"); | |
gap.setStart_date(prev.end_date); | |
gap.setEnd_date(curr.start_date); | |
employments.add(i, gap); | |
i++; | |
} | |
prev = curr; | |
} | |
return employments; | |
} | |
static void test(String input, String expected) throws JsonProcessingException { | |
// GIVEN | |
ObjectMapper objectMapper = new ObjectMapper(); | |
List<Employment> employments = objectMapper.readValue(input, new TypeReference<>() { | |
}); | |
// WHEN | |
EmploymentGaps eg = new EmploymentGaps(); | |
List<Employment> employmentsWithGaps = eg.findGaps(employments); | |
// THEN | |
String output = objectMapper.writeValueAsString(employmentsWithGaps); | |
if (!objectMapper.readTree(output).equals(objectMapper.readTree(expected))) { | |
throw new RuntimeException("Output doesn't equal expected"); | |
} | |
} | |
public static void main(String[] args) throws JsonProcessingException { | |
String input = "[ \n" + | |
" { \"type\": \"employment\", \"start_date\": \"2010-01-01\", \"end_date\": \"2012-02-13\" },\n" + | |
" { \"type\": \"employment\", \"start_date\": \"2013-05-01\", \"end_date\": \"2015-03-13\" },\n" + | |
" { \"type\": \"employment\", \"start_date\": \"2012-03-01\", \"end_date\": \"2013-03-13\" }\n" + | |
"]"; | |
String output = "[ \n" + | |
" { \"type\": \"employment\", \"start_date\": \"2010-01-01\", \"end_date\": \"2012-02-13\" },\n" + | |
" { \"type\": \"employment\", \"start_date\": \"2012-03-01\", \"end_date\": \"2013-03-13\" },\n" + | |
" { \"type\": \"employment_gap\", \"start_date\": \"2013-03-13\", \"end_date\": \"2013-05-01\" },\n" + | |
" { \"type\": \"employment\", \"start_date\": \"2013-05-01\", \"end_date\": \"2015-03-13\" }\n" + | |
"]"; | |
test(input, output); | |
// another test to verify if end date lasts more than 2 next employments | |
input = "[ \n" + | |
" { \"type\": \"employment\", \"start_date\": \"2011-01-01\", \"end_date\": \"2015-02-13\" },\n" + | |
" { \"type\": \"employment\", \"start_date\": \"2010-01-01\", \"end_date\": \"2012-02-13\" },\n" + | |
" { \"type\": \"employment\", \"start_date\": \"2013-05-01\", \"end_date\": \"2015-03-13\" },\n" + | |
" { \"type\": \"employment\", \"start_date\": \"2012-03-01\", \"end_date\": \"2013-03-13\" }\n" + | |
"]"; | |
output = "[" + | |
"{\"type\":\"employment\",\"start_date\":\"2010-01-01\",\"end_date\":\"2012-02-13\"}," + | |
"{\"type\":\"employment\",\"start_date\":\"2011-01-01\",\"end_date\":\"2015-02-13\"}," + | |
"{\"type\":\"employment\",\"start_date\":\"2012-03-01\",\"end_date\":\"2013-03-13\"}," + | |
"{\"type\":\"employment\",\"start_date\":\"2013-05-01\",\"end_date\":\"2015-03-13\"}" + | |
"]"; | |
test(input, output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment