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 {
public void searchByQuery() {
String query = searchBox.getText();
log.debug("User submitted search box");
RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes());
lastSearchResults = recipeSearch.searchByQuery(query);
//RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes()); //not needed when methods are static
lastSearchResults = RecipeSearch.searchByQuery(recipeManager.getRecipes(), query);
System.out.println("Results: ");
lastSearchResults.forEach(result -> System.out.println(" " + result.getName()));
}
......
......@@ -80,7 +80,7 @@ public class MainPageController extends BaseController {
.filter(CheckBox::isSelected)
.map(CategoryCheckBox::getAssociatedCategory)
.toList();
RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes());
return recipeSearch.searchByCategory(currentlySelectedCategories);
//RecipeSearch recipeSearch = new RecipeSearch(recipeManager.getRecipes()); //not needed when methods are static
return RecipeSearch.searchByCategory(recipeManager.getRecipes(), currentlySelectedCategories);
}
}
......@@ -7,13 +7,10 @@ import java.util.ArrayList;
import java.util.List;
public class RecipeSearch {
//TODO: lets make these methods static, no need to instantiate a new object every time
private final List<Recipe> recipesToSearch;
//private final List<Recipe> recipesToSearch; //not needed when methods are static
private static final Logger log = LogManager.getLogger(RecipeSearch.class);
public RecipeSearch (List<Recipe> recipesToSearch) {
this.recipesToSearch = recipesToSearch;
}
//public RecipeSearch (List<Recipe> recipesToSearch) { this.recipesToSearch = recipesToSearch; } //not needed when methods are static
/**
* A method to search a list of recipes for keywords.
......@@ -22,7 +19,7 @@ public class RecipeSearch {
* @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
*/
public List<Recipe> searchByQuery(String query) {
public static List<Recipe> searchByQuery(List<Recipe> recipesToSearch, String query) {
if (query == null) return recipesToSearch;
query = query.replaceAll("[.,!+?;-]", " ").trim();
if (query.isBlank()) return recipesToSearch;
......@@ -52,7 +49,7 @@ public class RecipeSearch {
* @param categoriesToSearch a list of categories to which the recipes shall belong
* @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;
List<Recipe> result = new ArrayList<>(recipesToSearch); //create List containing all the recipes to search
......
......@@ -18,19 +18,19 @@ class RecipeSearchTest {
private final static Category c2 = ValidObjectsPool.getValidCategoryTwo();
private List<Recipe> recipes;
private RecipeSearch underTest;
//private RecipeSearch underTest; //not needed when methods are static
@BeforeEach
public void setup() {
recipes = List.of(r1, r2);
underTest = new RecipeSearch(recipes);
//underTest = new RecipeSearch(recipes); //not needed when methods are static
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" \n", ".", "!", "..."})
public void testSearchByEmptyQuery(String input) {
assertEquals(recipes, underTest.searchByQuery(input));
assertEquals(recipes, RecipeSearch.searchByQuery(recipes, input));
}
@ParameterizedTest
......@@ -40,7 +40,7 @@ class RecipeSearchTest {
List<Recipe> expected = List.of(r1);
//when
List<Recipe> result = underTest.searchByQuery(query);
List<Recipe> result = RecipeSearch.searchByQuery(recipes, query);
//expected
assertEquals(expected, result);
......@@ -53,7 +53,7 @@ class RecipeSearchTest {
String query = "Apple ";
//when
List<Recipe> result = underTest.searchByQuery(query);
List<Recipe> result = RecipeSearch.searchByQuery(recipes, query);
//expected
assertEquals(expected, result);
......@@ -62,13 +62,13 @@ class RecipeSearchTest {
@ParameterizedTest
@NullAndEmptySource
public void testSearchByEmptyCategories(List<Category> input) {
assertEquals(recipes, underTest.searchByCategory(input));
assertEquals(recipes, RecipeSearch.searchByCategory(recipes, input));
}
@Test
public void testSearchByCategories() {
assertEquals(List.of(r1, r2), underTest.searchByCategory(List.of(c1)));
assertEquals(List.of(r2), underTest.searchByCategory(List.of(c1, c2)));
assertEquals(List.of(r2), underTest.searchByCategory(List.of(c2)));
assertEquals(List.of(r1, r2), RecipeSearch.searchByCategory(recipes, List.of(c1)));
assertEquals(List.of(r2), RecipeSearch.searchByCategory(recipes, List.of(c1, 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