mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-24 00:24:42 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -26,25 +26,20 @@ package com.iluwatar.combinator;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
/**
|
||||
* The functional pattern representing a style of organizing libraries
|
||||
* centered around the idea of combining functions.
|
||||
* Putting it simply, there is some type T, some functions
|
||||
* for constructing "primitive" values of type T,
|
||||
* and some "combinators" which can combine values of type T
|
||||
* in various ways to build up more complex values of type T.
|
||||
* The class {@link Finder} defines a simple function {@link Finder#find(String)}
|
||||
* and connected functions
|
||||
* {@link Finder#or(Finder)},
|
||||
* {@link Finder#not(Finder)},
|
||||
* {@link Finder#and(Finder)}
|
||||
* Using them the became possible to get more complex functions {@link Finders}
|
||||
* The functional pattern representing a style of organizing libraries centered around the idea of
|
||||
* combining functions. Putting it simply, there is some type T, some functions for constructing
|
||||
* "primitive" values of type T, and some "combinators" which can combine values of type T in
|
||||
* various ways to build up more complex values of type T. The class {@link Finder} defines a simple
|
||||
* function {@link Finder#find(String)} and connected functions {@link Finder#or(Finder)}, {@link
|
||||
* Finder#not(Finder)}, {@link Finder#and(Finder)} Using them the became possible to get more
|
||||
* complex functions {@link Finders}
|
||||
*/
|
||||
@Slf4j
|
||||
public class CombinatorApp {
|
||||
|
||||
private static final String TEXT = """
|
||||
private static final String TEXT =
|
||||
"""
|
||||
It was many and many a year ago,
|
||||
In a kingdom by the sea,
|
||||
That a maiden there lived whom you may know
|
||||
@@ -60,15 +55,16 @@ public class CombinatorApp {
|
||||
|
||||
/**
|
||||
* main.
|
||||
*
|
||||
* @param args args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var queriesOr = new String[]{"many", "Annabel"};
|
||||
var queriesOr = new String[] {"many", "Annabel"};
|
||||
var finder = Finders.expandedFinder(queriesOr);
|
||||
var res = finder.find(text());
|
||||
LOGGER.info("the result of expanded(or) query[{}] is {}", queriesOr, res);
|
||||
|
||||
var queriesAnd = new String[]{"Annabel", "my"};
|
||||
var queriesAnd = new String[] {"Annabel", "my"};
|
||||
finder = Finders.specializedFinder(queriesAnd);
|
||||
res = finder.find(text());
|
||||
LOGGER.info("the result of specialized(and) query[{}] is {}", queriesAnd, res);
|
||||
@@ -79,12 +75,10 @@ public class CombinatorApp {
|
||||
|
||||
res = Finders.filteredFinder(" was ", "many", "child").find(text());
|
||||
LOGGER.info("the result of filtered query is {}", res);
|
||||
|
||||
}
|
||||
|
||||
private static String text() {
|
||||
|
||||
return TEXT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,13 +28,12 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Functional interface to find lines in text.
|
||||
*/
|
||||
/** Functional interface to find lines in text. */
|
||||
public interface Finder {
|
||||
|
||||
/**
|
||||
* The function to find lines in text.
|
||||
*
|
||||
* @param text full tet
|
||||
* @return result of searching
|
||||
*/
|
||||
@@ -42,17 +41,20 @@ public interface Finder {
|
||||
|
||||
/**
|
||||
* Simple implementation of function {@link #find(String)}.
|
||||
*
|
||||
* @param word for searching
|
||||
* @return this
|
||||
*/
|
||||
static Finder contains(String word) {
|
||||
return txt -> Stream.of(txt.split("\n"))
|
||||
.filter(line -> line.toLowerCase().contains(word.toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
return txt ->
|
||||
Stream.of(txt.split("\n"))
|
||||
.filter(line -> line.toLowerCase().contains(word.toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* combinator not.
|
||||
*
|
||||
* @param notFinder finder to combine
|
||||
* @return new finder including previous finders
|
||||
*/
|
||||
@@ -66,6 +68,7 @@ public interface Finder {
|
||||
|
||||
/**
|
||||
* combinator or.
|
||||
*
|
||||
* @param orFinder finder to combine
|
||||
* @return new finder including previous finders
|
||||
*/
|
||||
@@ -79,16 +82,14 @@ public interface Finder {
|
||||
|
||||
/**
|
||||
* combinator and.
|
||||
*
|
||||
* @param andFinder finder to combine
|
||||
* @return new finder including previous finders
|
||||
*/
|
||||
default Finder and(Finder andFinder) {
|
||||
return
|
||||
txt -> this
|
||||
.find(txt)
|
||||
.stream()
|
||||
return txt ->
|
||||
this.find(txt).stream()
|
||||
.flatMap(line -> andFinder.find(line).stream())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,30 +28,25 @@ import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Complex finders consisting of simple finder.
|
||||
*/
|
||||
/** Complex finders consisting of simple finder. */
|
||||
public class Finders {
|
||||
private Finders() {
|
||||
}
|
||||
|
||||
private Finders() {}
|
||||
|
||||
/**
|
||||
* Finder to find a complex query.
|
||||
*
|
||||
* @param query to find
|
||||
* @param orQuery alternative to find
|
||||
* @param notQuery exclude from search
|
||||
* @return new finder
|
||||
*/
|
||||
public static Finder advancedFinder(String query, String orQuery, String notQuery) {
|
||||
return
|
||||
Finder.contains(query)
|
||||
.or(Finder.contains(orQuery))
|
||||
.not(Finder.contains(notQuery));
|
||||
return Finder.contains(query).or(Finder.contains(orQuery)).not(Finder.contains(notQuery));
|
||||
}
|
||||
|
||||
/**
|
||||
* Filtered finder looking a query with excluded queries as well.
|
||||
*
|
||||
* @param query to find
|
||||
* @param excludeQueries to exclude
|
||||
* @return new finder
|
||||
@@ -63,11 +58,11 @@ public class Finders {
|
||||
finder = finder.not(Finder.contains(q));
|
||||
}
|
||||
return finder;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialized query. Every next query is looked in previous result.
|
||||
*
|
||||
* @param queries array with queries
|
||||
* @return new finder
|
||||
*/
|
||||
@@ -82,6 +77,7 @@ public class Finders {
|
||||
|
||||
/**
|
||||
* Expanded query. Looking for alternatives.
|
||||
*
|
||||
* @param queries array with queries.
|
||||
* @return new finder
|
||||
*/
|
||||
|
||||
@@ -32,12 +32,12 @@ class CombinatorAppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
* <p>
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link CombinatorApp#main(String[])}
|
||||
* throws an exception.
|
||||
*
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* CombinatorApp#main(String[])} throws an exception.
|
||||
*/
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> CombinatorApp.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> CombinatorApp.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,6 @@ class FindersTest {
|
||||
assertEquals("In this kingdom by the sea;", res.get(2));
|
||||
}
|
||||
|
||||
|
||||
private String text() {
|
||||
return """
|
||||
It was many and many a year ago,
|
||||
@@ -81,5 +80,4 @@ class FindersTest {
|
||||
With a love that the winged seraphs of heaven
|
||||
Coveted her and me.""";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user