This is an example of what is sometimes called fluid interface,
where you can chain .method() calls after each other, because
they do return this. I had to remove this in LabWork to get
the tests to pass.
A similar pattern is the with-er pattern, where you have
methods .withX(newX) that create a new copy of the object but
with the property X set to the new value. (Related keywords
if you want to google around to find out more: immutable objects,
copy on write.)
new Track("Washing Machine Heart")
.setPerformer(artists.get("Mitski"))
.setWriter(artists.get("Mitski"))
.setDuration(128)
.setYear(2019),
new Track("26")
.setPerformer(artists.get("Paramore"))
.setWriter(artists.get("Paramore"))
.setDuration(222)
.setYear(2012),
new Track("Shoulda")
.setPerformer(artists.get("Jamie Woon"))
.setWriter(artists.get("Jamie Woon"))
.setDuration(229)
.setYear(2011),
};
MyTrackContainer container = new MyTrackContainer();
for (Track t : ts) {
container.add(t);
}
container.reset();
printTracks(container, "All tracks");
container.sort(new DurationComparator(), false);
printTracks(container, "All tracks sorted by duration (desc)");
container.filter(new DurationMatcher("180"));
printTracks(container, "Tracks longer than 3 minutes");
container.reset();
container.filter(new TitleMatcher(".*[aA]"));
printTracks(container, "Tracks with an 'a' in the title");
container.filter(new DurationMatcher("0 180"));
container.remove();
container.reset();
printTracks(container, "Tracks longer than 3 minutes (others removed)");
}
public static void printTracks(MyTrackContainer container, String heading) {
System.out.println("# " + heading);
System.out.println();
MyFormatter<Track> f = new ShortTrackFormatter();
System.out.println(f.header());
System.out.println(f.topSeparator());
for (Track t : container.selection()) {
System.out.println(f.format(t));
}
System.out.println();
}
}