Skip to content
Snippets Groups Projects
Commit 866bbc8d authored by Blersch Lara's avatar Blersch Lara
Browse files

changed methods in RecipeSearch to static

parent 910ee3ad
No related branches found
No related tags found
No related merge requests found
...@@ -44,8 +44,8 @@ public class HeaderController extends BaseController { ...@@ -44,8 +44,8 @@ public class HeaderController extends BaseController {
public void searchByQuery() { public void searchByQuery() {
String query = searchBox.getText(); String query = searchBox.getText();
log.debug("User submitted search box"); log.debug("User submitted search box");
RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes()); //RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes()); //not needed when methods are static
lastSearchResults = recipeSearch.searchByQuery(query); lastSearchResults = RecipeSearch.searchByQuery(recipeManager.getRecipes(), query);
System.out.println("Results: "); System.out.println("Results: ");
lastSearchResults.forEach(result -> System.out.println(" " + result.getName())); lastSearchResults.forEach(result -> System.out.println(" " + result.getName()));
} }
......
...@@ -80,7 +80,7 @@ public class MainPageController extends BaseController { ...@@ -80,7 +80,7 @@ public class MainPageController extends BaseController {
.filter(CheckBox::isSelected) .filter(CheckBox::isSelected)
.map(CategoryCheckBox::getAssociatedCategory) .map(CategoryCheckBox::getAssociatedCategory)
.toList(); .toList();
RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes()); //RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes()); //not needed when methods are static
return recipeSearch.searchByCategory(currentlySelectedCategories); return RecipeSearch.searchByCategory(recipeManager.getRecipes(), currentlySelectedCategories);
} }
} }
...@@ -7,13 +7,10 @@ import java.util.ArrayList; ...@@ -7,13 +7,10 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class RecipeSearch { public class RecipeSearch {
//TODO: lets make these methods static, no need to instantiate a new object every time //private final List<Recipe> recipesToSearch; //not needed when methods are static
private final List<Recipe> recipesToSearch;
private static final Logger log = LogManager.getLogger(RecipeSearch.class); private static final Logger log = LogManager.getLogger(RecipeSearch.class);
public RecipeSearch (List<Recipe> recipesToSearch) { //public RecipeSearch (List<Recipe> recipesToSearch) { this.recipesToSearch = recipesToSearch; } //not needed when methods are static
this.recipesToSearch = recipesToSearch;
}
/** /**
* A method to search a list of recipes for keywords. * A method to search a list of recipes for keywords.
...@@ -22,7 +19,7 @@ public class RecipeSearch { ...@@ -22,7 +19,7 @@ public class RecipeSearch {
* @param query a String with keywords to search for * @param query a String with keywords to search for
* @return a list of Recipes which contain one or several of the keywords in its name or its ingredients * @return a list of Recipes which contain one or several of the keywords in its name or its ingredients
*/ */
public List<Recipe> searchByQuery(String query) { public static List<Recipe> searchByQuery(List<Recipe> recipesToSearch, String query) {
if (query == null) return recipesToSearch; if (query == null) return recipesToSearch;
query = query.replaceAll("[.,!+?;-]", " ").trim(); query = query.replaceAll("[.,!+?;-]", " ").trim();
if (query.isBlank()) return recipesToSearch; if (query.isBlank()) return recipesToSearch;
...@@ -52,7 +49,7 @@ public class RecipeSearch { ...@@ -52,7 +49,7 @@ public class RecipeSearch {
* @param categoriesToSearch a list of categories to which the recipes shall belong * @param categoriesToSearch a list of categories to which the recipes shall belong
* @return a list of recipes which belong to all categories of categoriesToSearch * @return a list of recipes which belong to all categories of categoriesToSearch
*/ */
public List<Recipe> searchByCategory(List<Category> categoriesToSearch) { public static List<Recipe> searchByCategory(List<Recipe> recipesToSearch, List<Category> categoriesToSearch) {
if (categoriesToSearch == null || categoriesToSearch.isEmpty()) return recipesToSearch; if (categoriesToSearch == null || categoriesToSearch.isEmpty()) return recipesToSearch;
List<Recipe> result = new ArrayList<>(recipesToSearch); //create List containing all the recipes to search List<Recipe> result = new ArrayList<>(recipesToSearch); //create List containing all the recipes to search
......
...@@ -18,19 +18,19 @@ class RecipeSearchTest { ...@@ -18,19 +18,19 @@ class RecipeSearchTest {
private final static Category c2 = ValidObjectsPool.getValidCategoryTwo(); private final static Category c2 = ValidObjectsPool.getValidCategoryTwo();
private List<Recipe> recipes; private List<Recipe> recipes;
private RecipeSearch underTest; //private RecipeSearch underTest; //not needed when methods are static
@BeforeEach @BeforeEach
public void setup() { public void setup() {
recipes = List.of(r1, r2); recipes = List.of(r1, r2);
underTest = new RecipeSearch(recipes); //underTest = new RecipeSearch(recipes); //not needed when methods are static
} }
@ParameterizedTest @ParameterizedTest
@NullAndEmptySource @NullAndEmptySource
@ValueSource(strings = {" \n", ".", "!", "..."}) @ValueSource(strings = {" \n", ".", "!", "..."})
public void testSearchByEmptyQuery(String input) { public void testSearchByEmptyQuery(String input) {
assertEquals(recipes, underTest.searchByQuery(input)); assertEquals(recipes, RecipeSearch.searchByQuery(recipes, input));
} }
@ParameterizedTest @ParameterizedTest
...@@ -40,7 +40,7 @@ class RecipeSearchTest { ...@@ -40,7 +40,7 @@ class RecipeSearchTest {
List<Recipe> expected = List.of(r1); List<Recipe> expected = List.of(r1);
//when //when
List<Recipe> result = underTest.searchByQuery(query); List<Recipe> result = RecipeSearch.searchByQuery(recipes, query);
//expected //expected
assertEquals(expected, result); assertEquals(expected, result);
...@@ -53,7 +53,7 @@ class RecipeSearchTest { ...@@ -53,7 +53,7 @@ class RecipeSearchTest {
String query = "Apple "; String query = "Apple ";
//when //when
List<Recipe> result = underTest.searchByQuery(query); List<Recipe> result = RecipeSearch.searchByQuery(recipes, query);
//expected //expected
assertEquals(expected, result); assertEquals(expected, result);
...@@ -62,13 +62,13 @@ class RecipeSearchTest { ...@@ -62,13 +62,13 @@ class RecipeSearchTest {
@ParameterizedTest @ParameterizedTest
@NullAndEmptySource @NullAndEmptySource
public void testSearchByEmptyCategories(List<Category> input) { public void testSearchByEmptyCategories(List<Category> input) {
assertEquals(recipes, underTest.searchByCategory(input)); assertEquals(recipes, RecipeSearch.searchByCategory(recipes, input));
} }
@Test @Test
public void testSearchByCategories() { public void testSearchByCategories() {
assertEquals(List.of(r1, r2), underTest.searchByCategory(List.of(c1))); assertEquals(List.of(r1, r2), RecipeSearch.searchByCategory(recipes, List.of(c1)));
assertEquals(List.of(r2), underTest.searchByCategory(List.of(c1, c2))); assertEquals(List.of(r2), RecipeSearch.searchByCategory(recipes, List.of(c1, c2)));
assertEquals(List.of(r2), underTest.searchByCategory(List.of(c2))); assertEquals(List.of(r2), RecipeSearch.searchByCategory(recipes, List.of(c2)));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment