review changes

This commit is contained in:
Abdelbaki BEN ELHAJ SLIMENE
2021-03-24 16:21:16 +01:00
parent c30b27b240
commit b558643405
15 changed files with 104 additions and 41 deletions

View File

@@ -9,9 +9,9 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>standard-library</artifactId>
<artifactId>classics-library</artifactId>
<name>standard-library</name>
<name>classics-library</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@@ -6,20 +6,29 @@ import java.util.TreeMap;
import org.library.spi.Book;
import org.library.spi.Library;
public class StandardLibrary implements Library {
public class ClassicsLibrary implements Library {
public static final String CLASSICS_LIBRARY = "CLASSICS";
private final Map<String, Book> books;
public StandardLibrary() {
public ClassicsLibrary() {
books = new TreeMap<>();
Book nineteenEightyFour = new Book("Nineteen Eighty-Four", "George Orwell",
"It was a bright cold day in April, and the clocks were striking thirteen.");
Book theLordOfTheRings = new Book("The Lord of the Rings", "J. R. R. Tolkien",
"When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating his " +
"eleventy-first birthday with a party of special magnificence, there was much talk and excitement in Hobbiton.");
Book cleanCode = new Book("Clean Code", "Robert C. Martin",
"ClassicsEven bad code can function. But if code isnt clean, it can bring a development organization to its knees");
books.put("Nineteen Eighty-Four", nineteenEightyFour);
books.put("The Lord of the Rings", theLordOfTheRings);
books.put("Clean Code", cleanCode);
}
@Override
public String getCategory() {
return CLASSICS_LIBRARY;
}
@Override

View File

@@ -5,10 +5,10 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.library</groupId>
<artifactId>custom-library</artifactId>
<artifactId>computer-science-library</artifactId>
<version>1.0-SNAPSHOT</version>
<name>custom-library</name>
<name>computer-science-library</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@@ -6,11 +6,13 @@ import java.util.TreeMap;
import org.library.spi.Book;
import org.library.spi.Library;
public class CustomLibrary implements Library {
public class ComputerScienceLibrary implements Library {
public static final String COMPUTER_SCIENCE_LIBRARY = "COMPUTER_SCIENCE";
private final Map<String, Book> books;
public CustomLibrary() {
public ComputerScienceLibrary() {
books = new TreeMap<>();
Book cleanCode = new Book("Clean Code", "Robert C. Martin",
@@ -22,6 +24,11 @@ public class CustomLibrary implements Library {
books.put(pragmaticProgrammer.getName(), pragmaticProgrammer);
}
@Override
public String getCategory() {
return COMPUTER_SCIENCE_LIBRARY;
}
@Override
public Book getBook(String name) {
return books.get(name);

View File

@@ -9,28 +9,47 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>library-demo</artifactId>
<artifactId>library-client</artifactId>
<name>library-demo</name>
<name>library-client</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.library</groupId>
<artifactId>standard-library</artifactId>
<artifactId>computer-science-library</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.library</groupId>
<artifactId>custom-library</artifactId>
<artifactId>classics-library</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.library</groupId>
<artifactId>library-service-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,22 @@
package org.library;
public class LibraryClient {
public static void main(String[] args) {
LibraryService libraryService = LibraryService.getInstance();
requestBook("Clean Code", libraryService);
requestBook("The Lord of the Rings", libraryService);
requestBook("The Lord of the Rings", "COMPUTER_SCIENCE", libraryService);
}
private static void requestBook(String bookName, LibraryService library) {
library.getBook(bookName)
.ifPresentOrElse(book -> System.out.println("The book '" + bookName + "' was found, here are the details:" + book),
() -> System.out.println("The library doesn't have the book '" + bookName + "' that you need."));
}
private static void requestBook(String bookName,String category, LibraryService library) {
library.getBook(bookName, category)
.ifPresentOrElse(book -> System.out.println("The book '" + bookName + "' was found in " + category + ", here are the details:" + book),
() -> System.out.println("The library " + category + " doesn't have the book '" + bookName + "' that you need."));
}
}

View File

@@ -1,20 +0,0 @@
package org.library;
import org.library.spi.Book;
public class LibraryDemo {
public static void main(String[] args) {
LibraryService libraryService = LibraryService.getInstance();
manageBookRequest("Clean Code", libraryService);
manageBookRequest("The Lord of the Rings", libraryService);
}
private static void manageBookRequest(String bookName, LibraryService library) {
Book book = library.getBook(bookName);
if (book == null) {
System.out.println("The library doesn't have the book '" + bookName + "' that you need.");
} else {
System.out.println("The book '" + bookName + "'was found, here are the details:" + book);
}
}
}

View File

@@ -16,5 +16,15 @@
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,6 +1,8 @@
package org.library;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import org.library.spi.Book;
@@ -22,13 +24,26 @@ public class LibraryService {
return libraryService;
}
public Book getBook(String name) {
public void refresh() {
loader.reload();
}
public Optional<Book> getBook(String name) {
Book book = null;
Iterator<Library> libraries = loader.iterator();
while (book == null && libraries.hasNext()) {
Library library = libraries.next();
book = library.getBook(name);
}
return book;
return Optional.ofNullable(book);
}
public Optional<Book> getBook(String name, String category) {
return loader.stream()
.map(ServiceLoader.Provider::get)
.filter(library -> library.getCategory().equals(category))
.map(library -> library.getBook(name))
.filter(Objects::nonNull)
.findFirst();
}
}

View File

@@ -1,5 +1,6 @@
package org.library.spi;
public interface Library {
String getCategory();
Book getBook(String name);
}

View File

@@ -9,10 +9,10 @@
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>library-demo</module>
<module>library-client</module>
<module>library-service-provider</module>
<module>standard-library</module>
<module>custom-library</module>
<module>classics-library</module>
<module>computer-science-library</module>
</modules>
<name>Library Service Provider Example</name>