|
| 1 | +import java.time.Duration; |
| 2 | +import java.time.LocalDate; |
| 3 | +import java.time.Month; |
| 4 | +import java.time.Period; |
| 5 | +import java.time.temporal.ChronoUnit; |
| 6 | +import java.time.temporal.TemporalField; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +/** |
| 11 | + * StreamsImprovements |
| 12 | + */ |
| 13 | + |
| 14 | +public class StreamsImprovements { |
| 15 | + public static void main(String[] args) { |
| 16 | + getBooks(). |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * New recommendation from Joshua Bloch -- If you know that the callers of your method are happy using Stream as a return, use it!!!! |
| 23 | + */ |
| 24 | + private static Stream<Book> getBooks() { |
| 25 | + Book ef3e = new Book("Effective Java 3rd Edition", "Joshua Bloch", "978-0-13-468599-1", LocalDate.of(2017, Month.DECEMBER, 20)); |
| 26 | + Book cjs9fi = new Book("Core Java SE 9 for the Impatient, 2nd Edition", "Cay S. Horstmann", "978-0-13-469472-6", LocalDate.of(2017, Month.SEPTEMBER, 15)); |
| 27 | + Book j9fp = new Book("Java 9 for Programmers, 4th Edition", "Paul J. Deitel, Harvey Deitel", "978-0-13-477756-6", LocalDate.of(2017, Month.MAY, 11)); |
| 28 | + Book ej2ne = new Book("Effective Java 2nd Edition", "Joshua Bloch", "ISBN-13: 978-0-321-35668-0", LocalDate.of(2008, Month.MAY, 8)); |
| 29 | + Book rwjeep = new Book("Real World Java EE Patterns--Rethinking Best Practices", "Adam Bien", "978-1-300-14931-6", LocalDate.of(2012, Month.SEPTEMBER, 5)); |
| 30 | + Book rwjeenh = new Book("Real World Java EE Night Hacks - Dissecting the Business Tier", "Adam Bien", "978-0-557-07832-5", LocalDate.of(2008, Month.MAY, 8)); |
| 31 | + return Stream.of(ef3e, cjs9fi, j9fp, ej2ne, rwjeep, rwjeenh); |
| 32 | + } |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +class Book { |
| 37 | + private final String name; |
| 38 | + private final String author; |
| 39 | + private final String isbn; |
| 40 | + private final LocalDate publishDate; |
| 41 | + |
| 42 | + public Book(String name, String author, String isbn, LocalDate publishDate) { |
| 43 | + this.name = name; |
| 44 | + this.author = author; |
| 45 | + this.isbn = isbn; |
| 46 | + this.publishDate = publishDate; |
| 47 | + } |
| 48 | + |
| 49 | + public Period getBookAgeinMonths() { |
| 50 | + return Period.between(publishDate, LocalDate.now()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return the name |
| 55 | + */ |
| 56 | + public String getName() { |
| 57 | + return name; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return the isbn |
| 62 | + */ |
| 63 | + public String getIsbn() { |
| 64 | + return isbn; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return the publishDate |
| 69 | + */ |
| 70 | + public LocalDate getPublishDate() { |
| 71 | + return publishDate; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return the author |
| 76 | + */ |
| 77 | + public String getAuthor() { |
| 78 | + return author; |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments