-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
112 lines (100 loc) · 4.1 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.gridnine.testing;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Factory class to get sample list of flights.
*/
class FlightBuilder {
static List<Flight> createFlights() {
LocalDateTime threeDaysFromNow = LocalDateTime.now().plusDays(3);
return Arrays.asList(
//A normal filter with two hour duration
createFlight(threeDaysFromNow, threeDaysFromNow.plusHours(2)),
//A normal multi segment filter
createFlight(threeDaysFromNow, threeDaysFromNow.plusHours(2),
threeDaysFromNow.plusHours(3), threeDaysFromNow.plusHours(5)),
//A filter departing in the past
createFlight(threeDaysFromNow.minusDays(6), threeDaysFromNow),
//A filter that departs before it arrives
createFlight(threeDaysFromNow, threeDaysFromNow.minusHours(6)),
//A filter with more than two hours ground time
createFlight(threeDaysFromNow, threeDaysFromNow.plusHours(2),
threeDaysFromNow.plusHours(5), threeDaysFromNow.plusHours(6)),
//Another filter with more than two hours ground time
createFlight(threeDaysFromNow, threeDaysFromNow.plusHours(2),
threeDaysFromNow.plusHours(3), threeDaysFromNow.plusHours(4),
threeDaysFromNow.plusHours(6), threeDaysFromNow.plusHours(7)));
}
private static Flight createFlight(final LocalDateTime... dates) {
if ((dates.length % 2) != 0) {
throw new IllegalArgumentException(
"you must pass an even number of dates");
}
List<Segment> segments = new ArrayList<>(dates.length / 2);
for (int i = 0; i < (dates.length - 1); i += 2) {
segments.add(new Segment(dates[i], dates[i + 1]));
}
return new Flight(segments);
}
}
/**
* Bean that represents a filter.
*/
class Flight {
private final List<Segment> segments;
private static int counter = 0;
private final int id = counter++;
Flight(final List<Segment> segs) {
segments = segs;
}
List<Segment> getSegments() {
return segments;
}
@Override
public String toString() {
return "Flight " + id + " " + segments.stream().map(Object::toString)
.collect(Collectors.joining(" "));
}
}
/**
* Bean that represents a filter segment.
*/
class Segment {
private final LocalDateTime departureDate;
private final LocalDateTime arrivalDate;
Segment(final LocalDateTime dep, final LocalDateTime arr) {
departureDate = Objects.requireNonNull(dep);
arrivalDate = Objects.requireNonNull(arr);
}
LocalDateTime getDepartureDate() {
return departureDate;
}
LocalDateTime getArrivalDate() {
return arrivalDate;
}
@Override
public String toString() {
DateTimeFormatter fmt =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
return '[' + departureDate.format(fmt) + '|' + arrivalDate.format(fmt)
+ ']';
}
}
public class Main {
public static void main(String[] args) {
List<Flight> flights = FlightBuilder.createFlights();
System.out.println("-------TestList--------");
System.out.println(flights);
System.out.println("-------DepartureUntilNow----------");
System.out.println(FilterFlight.filter(new FilterFlightBeforeNow(),flights));
System.out.println("-------ArrivalDate Before DepartureDate-----------");
System.out.println(FilterFlight.filter(new FilterFlightArrivalBeforeDeparture(), flights));
System.out.println("-------GroundTime more than two hours----------");
System.out.println(FilterFlight.filter(new FilterFlightGroundTimeTwoHours(), flights));
}
}