diff --git a/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java b/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java index afbd983..7a805e8 100644 --- a/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java +++ b/src/main/java/fr/u_paris/gla/project/idfm/CSVSchedulesProvider.java @@ -2,9 +2,24 @@ package fr.u_paris.gla.project.idfm; import fr.u_paris.gla.project.io.ScheduleFormat; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.time.Duration; +import java.time.LocalDateTime; +// import java.time.format.ResolverStyle; import java.text.NumberFormat; import java.util.*; + +import org.apache.commons.lang3.ObjectUtils.Null; public class CSVSchedulesProvider { + private static final DateTimeFormatter HOUR_MINUTE_FORMATTER = ScheduleFormat.getTimeFormatter(); + // private static final HashMap timings = new HashMap(){{ + // put("Bus", new int[]{45, 50}); + // put("Funicular", new int[]{120, 70}); + // put("Tram", new int[]{72, 60}); + // put("Rail", new int[]{120, 60,50}); + // put("Subway", new int[]{50, 45}); + // }}; private static final HashMap timings = new HashMap(){{ put("Bus", new int[]{11, 8, 7}); put("Funicular", new int[]{15, 25, 20}); @@ -12,6 +27,9 @@ public class CSVSchedulesProvider { put("Rail", new int[]{10, 11,15,12}); put("Subway", new int[]{4, 2, 6,3,3,4}); }}; + + private static int DEFAULT_TIMING = 6; + // private static int MINUTES_BEFORE_LAST_DEPARTURE = 6; private static final NumberFormat MINUTES_SECOND_FORMATTER = NumberFormat .getInstance(Locale.ENGLISH); @@ -23,12 +41,13 @@ public class CSVSchedulesProvider { private final Iterator currentTransport; private Iterator currentDescription = Collections.emptyIterator(); - // private Stop start = null; - // private Stop end = null; - // private boolean hasNext = false; - // private boolean onNext = false; - private int hours_count = 0; - private String last_hour = ""; + private String current_tansport_type = ""; + private LocalDateTime currentHour = null; + private LocalDateTime lastHour = null; + private int debut = 0; + + + /** * Create the stream provider @@ -42,12 +61,16 @@ public class CSVSchedulesProvider { } private void skipToNext() { - if (hours_count == 2){ + if(currentHour == null || lastHour == null){ + skipToNextTransport(); + }else if(currentHour.compareTo(lastHour) < 0){ + // System.out.println("**Skip: Le current hour est plus petit "+currentHour+"|||"+lastHour); + addRandomMinutes(); + }else if (currentHour.compareTo(lastHour) >= 0) { + // System.out.println("**Skip: Le current hour est plus grand "+currentHour+"|||"+lastHour); skipToNextDescription(); - hours_count = 0; - }else if(hours_count == 1){ - this.line[ScheduleFormat.TIME_INDEX] = last_hour; - }else if (!this.currentDescription.hasNext()) { + } + else if (!this.currentDescription.hasNext()) { skipToNextTransport(); } @@ -56,15 +79,21 @@ public class CSVSchedulesProvider { private void skipToNextDescription() { if (this.currentDescription.hasNext()) { TraceDescription description = this.currentDescription.next(); - hours_count = 0; - // this.start = new Stop(description.from); - // this.end = new Stop(description.to); + // hours_count = 0; + // current_time = description.first; + // last_time = description.last; + currentHour = convertIntoLocalDateTime(description.first); + lastHour = convertIntoLocalDateTime(description.last); + // System.out.println("** SkipTOnextD: last hour est "+lastHour+" first est "+currentHour+" desc.last est "+description.last+ " et desc.first est "+description.first); + + + if(lastHour.compareTo(currentHour) <= 0){ + lastHour = lastHour.plusDays(1); + } this.line[ScheduleFormat.TERMINUS_INDEX] = description.from; this.line[ScheduleFormat.TRIP_SEQUENCE_INDEX] = description.bifurcation.toString(); - this.line[ScheduleFormat.TIME_INDEX] = description.first; - last_hour = description.last; - // this.onNext = true; - // this.hasNext = true; + this.line[ScheduleFormat.TIME_INDEX] = currentHour.format(HOUR_MINUTE_FORMATTER); + }else{ skipToNextTransport(); } @@ -74,12 +103,14 @@ public class CSVSchedulesProvider { if (this.currentTransport.hasNext()) { Transport transport = this.currentTransport.next(); this.line[ScheduleFormat.LINE_INDEX] = transport.name; + // System.out.println("Changement de transport"+transport.name); + // transport.buildBifurcation(); + current_tansport_type = transport.type; this.currentDescription = transport.descriptions.iterator(); skipToNextDescription(); } else { - // this.onNext = true; - // this.hasNext = false; + } } @@ -88,35 +119,41 @@ public class CSVSchedulesProvider { return null; } skipToNext(); - hours_count ++; return new String[][]{Arrays.copyOf(this.line, this.line.length)}; - // if (!this.currentTransport.hasNext()) { - // return new String[][]{Arrays.copyOf(this.line, this.line.length)}; - // } - // Transport transport = this.currentTransport.next(); - - // // Store all lines for this iteration - // List allLines = new ArrayList<>(); - - // transport.descriptions.forEach(desc -> { - // Arrays.stream(findTimes(desc.first, desc.last)).forEach(time -> { - // String[] newLine = Arrays.copyOf(this.line, this.line.length); - // newLine[ScheduleFormat.LINE_INDEX] = transport.name; - // newLine[ScheduleFormat.TRIP_SEQUENCE_INDEX] = desc.bifurcation.toString(); - // newLine[ScheduleFormat.TERMINUS_INDEX] = desc.to; - // newLine[ScheduleFormat.TIME_INDEX] = time; - // allLines.add(Arrays.copyOf(newLine, newLine.length)); - // }); - // }); - - // return allLines.toArray(new String[0][]); } - private String[] findTimes(String start, String last) { - return new String[]{ - start, - last, - }; + private void addRandomMinutes() { + // System.out.println("** addM: AVANT: "+currentHour); + currentHour = currentHour.plusMinutes(pickMinute(current_tansport_type)); + this.line[ScheduleFormat.TIME_INDEX] = currentHour.format(HOUR_MINUTE_FORMATTER); + // System.out.println("** addM: APRES: "+currentHour); + // debut ++; + // if(debut == 7) throw new IllegalArgumentException(); + } + + private static long calculateDuration(String debut, String dernier) { + LocalTime debutTime = LocalTime.parse(debut, HOUR_MINUTE_FORMATTER); + LocalTime dernierTime = LocalTime.parse(dernier, HOUR_MINUTE_FORMATTER); + + Duration difference = Duration.between(debutTime, dernierTime); + return difference.toMinutes(); + } + + public static int pickMinute(String transportType) { + if (!timings.containsKey(transportType)) { + return DEFAULT_TIMING; + } + int[] temps = timings.get(transportType); + Random random = new Random(); + int indexAleatoire = random.nextInt(temps.length); + return temps[indexAleatoire]; + } + + public static LocalDateTime convertIntoLocalDateTime(String heureMinute) { + LocalDateTime aujourdHui = LocalDateTime.now(); + LocalTime time = LocalTime.parse(heureMinute, HOUR_MINUTE_FORMATTER); + + return aujourdHui.withHour(time.getHour()).withMinute(time.getMinute()).withSecond(0).withNano(0); } } \ No newline at end of file