package com.mssngvwls;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

@Service
public class GamePhraseSelector {

    private final PhraseRepository phraseRepository;
    private final PhraseFormatter phraseFormatter;

    public GamePhraseSelector(final PhraseRepository phraseRepository, final PhraseFormatter phraseFormatter) {
        this.phraseRepository = phraseRepository;
        this.phraseFormatter = phraseFormatter;
    }

    public Queue<GamePhrase> generateCategories() {
        final List<Phrase> phrases = phraseRepository.getPhrases();
        Collections.shuffle(phrases);
        return phrases.stream()
                .limit(10)
                .map(phrase -> new GamePhrase(phrase.getFullPhrase(), phraseFormatter.format(phrase.getFullPhrase()), phrase.getCategory().getName()))
                .collect(Collectors.toCollection(LinkedList::new));
    }

}

@@ -21,38 +21,8 @@
 s;%0A%0A
-import java.util.Collections;%0A
 impo
@@ -236,30 +236,32 @@
 ivate final 
-Phrase
+Category
 Repository p
@@ -251,38 +251,40 @@
 egoryRepository 
-phrase
+category
 Repository;%0A    
@@ -367,22 +367,24 @@
 r(final 
-Phrase
+Category
 Reposito
@@ -386,22 +386,24 @@
 ository 
-phrase
+category
 Reposito
@@ -460,22 +460,24 @@
    this.
-phrase
+category
 Reposito
@@ -481,22 +481,24 @@
 itory = 
-phrase
+category
 Reposito
@@ -608,55 +608,128 @@
 ies(
-) %7B%0A        final List%3CPhrase%3E phrases = phrase
+final int numberOfCategories, final int numberOfPhrasesPerCategory) %7B%0A        final List%3CCategory%3E categories = category
 Repo
@@ -742,27 +742,34 @@
 .get
-Phras
+AllCategori
 es();%0A
+%0A
         
 Coll
@@ -768,95 +768,305 @@
     
-Collections.shuffle(phrases);%0A        return phrases.stream()%0A                .limit(10
+return categories.stream()%0A                .filter(category -%3E category.getPhrases().size() %3E= numberOfPhrasesPerCategory)%0A                .limit(numberOfCategories)%0A                .map(Category::getPhrases)%0A                .limit(numberOfPhrasesPerCategory)%0A                .flatMap(List::stream
 )%0A  

Change how game phrases are generated - more streams!