/*
* Created at 20:17 on 11/02/2017
*/
package com.example.news.feed;

import com.example.news.domain.News;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.function.Supplier;

/**
 * @author zzhao
 */
public class NewsSupplier implements Supplier<News> {

    public static final String[] HEADLINES = new String[]{
            "up", "down", "rise", "fall", "good", "bad", "success", "failure", "high", "low", "über", "unter"
    };

    private final SecureRandom random;

    NewsSupplier() {
        this.random = new SecureRandom(String.valueOf(System.nanoTime()).getBytes());
    }

    @Override
    public News get() {
        final int oneFrom100 = this.random.nextInt(100);
        final int priority = oneFrom100 < 7 // 7% chance to be high prio
                ? oneFrom100 % 5 + 5
                : oneFrom100 % 5;

        final int headlineCount = this.random.nextInt(3) + 3; // 3 to 5
        final ArrayList<String> headlines = new ArrayList<>(headlineCount);
        for (int i = 0; i < headlineCount; i++) {
            headlines.add(HEADLINES[this.random.nextInt(HEADLINES.length)]); // could have duplicate headlines
        }

        return new News(priority, headlines);
    }
}

@@ -154,21 +154,46 @@
 va.util.
-Array
+Collections;%0Aimport java.util.
 List;%0Aim
@@ -225,16 +225,87 @@
 upplier;
+%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.IntStream;
 %0A%0A/**%0A *
@@ -595,16 +595,66 @@
 andom;%0A%0A
+    private final List%3CInteger%3E headlineIndices;%0A%0A
     News
@@ -752,16 +752,192 @@
 tes());%0A
+        this.headlineIndices = IntStream%0A                .range(0, HEADLINES.length)%0A                .mapToObj(Integer::valueOf)%0A                .collect(Collectors.toList());%0A
     %7D%0A%0A 
@@ -1187,317 +1187,281 @@
     
-final int headlineCount = this.random.nextInt(3) + 3; // 3 to 5%0A        final ArrayList%3CString%3E headlines = new ArrayList%3C%3E(headlineCount);%0A        for (int i = 0; i %3C headlineCount; i++) %7B%0A            headlines.add(HEADLINES%5Bthis.random.nextInt(HEADLINES.length)%5D); // could have duplicate headlines%0A
+Collections.shuffle(this.headlineIndices);%0A        return new News(priority,%0A                this.headlineIndices%0A                        .subList(0, this.random.nextInt(3) + 3)%0A                        .stream()%0A                        .map(i -%3E HEADLINES%5Bi%5D)%0A     
         
 %7D%0A%0A 
@@ -1460,19 +1460,16 @@
     
-%7D%0A%0A
         
 retu
@@ -1468,43 +1468,40 @@
     
-return new News(priority, headlines
+   .collect(Collectors.toList())
 );%0A 

fix headline creation of news