package be.raildelays.batch.processor;

import be.raildelays.domain.xls.ExcelRow;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

/**
 * We filter items based on a threshold date.
 * You have the choice to keep what is before or after the threshold date.
 *
 * @author Almex
 * @since 1.2
*/
public class FilterWithThresholdDateProcessor implements ItemProcessor<ExcelRow, ExcelRow>, InitializingBean {

    private LocalDate thresholdDate;

    private Mode mode;

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(thresholdDate, "The 'thresholdDate' property must be provided");
    }

    public enum Mode {
        BEFORE, AFTER_OR_EQUALS
    }

    @Override
    public ExcelRow process(ExcelRow item) throws Exception {
        ExcelRow result = null;

        if (item.getDate() != null) {
            LocalDate itemDate = item.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            switch (mode) {
                case BEFORE:
                    if (itemDate.isBefore(thresholdDate)) {
                        result = item;
                    }
                    break;
                case AFTER_OR_EQUALS:
                    if (itemDate.isAfter(thresholdDate) || itemDate.isEqual(thresholdDate)) {
                        result = item;
                    }
                    break;
            }
        }

        return result;
    }

    public void setThresholdDate(Date thresholdDate) {
        this.thresholdDate = thresholdDate != null ? thresholdDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() : null;
    }

    public void setMode(Mode mode) {
        this.mode = mode;
    }
}

@@ -847,25 +847,497 @@
 FORE
-, AFTER_OR_EQUALS
+ %7B%0A            @Override%0A            boolean filter(LocalDate itemDate, LocalDate thresholdDate) %7B%0A                return itemDate.isAfter(thresholdDate) %7C%7C itemDate.isEqual(thresholdDate);%0A            %7D%0A        %7D, AFTER_OR_EQUALS %7B%0A            @Override%0A            boolean filter(LocalDate itemDate, LocalDate thresholdDate) %7B%0A                return itemDate.isBefore(thresholdDate);%0A            %7D%0A        %7D;%0A%0A        abstract boolean filter(LocalDate itemDate, LocalDate thresholdDate);
 %0A   
@@ -1447,16 +1447,49 @@
  = null;
+ // By default we filter the item
 %0A%0A      
@@ -1643,315 +1643,35 @@
     
-switch
+if
  (
+!
 mode
-) %7B%0A                case BEFORE:%0A                    if (itemDate.isBefore(thresholdDate)) %7B%0A                        result = item;%0A                    %7D%0A                    break;%0A                case AFTER_OR_EQUALS:%0A                    if (itemDate.isAfter(thresholdDate) %7C%7C itemDate.isEqual(
+.filter(itemDate, 
 thre
@@ -1700,24 +1700,16 @@
         
-    
-    
 result =
@@ -1719,57 +1719,8 @@
 em;%0A
-                    %7D%0A                    break;%0A
     

Apply Enum polymorphic pattern