package io.generators.core;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.copyOf;

/**
 * Generates randomly selected element from collection/array
 *
 * @param <T> type of the collection's elements
 *
 * @author Tomas Klubal
 */
public class RandomFromCollectionGenerator<T> implements Generator<T> {
    private final List<T> items;
    private final Random random = new Random();

    /**
     * Creates generator that selects values from <code>items</code> passed in
     *
     * @param items to select from
     * @throws NullPointerException when collection passed in is null
     */
    public RandomFromCollectionGenerator(@Nonnull Collection<T> items) {
        this.items = copyOf(checkNotNull(items, "Collection for generation can't be null"));
    }

    /**
     * Creates generator that selects values from <code>items</code> passed in
     *
     * @param items to select from
     * @throws NullPointerException when array passed in is null
     */
    @SafeVarargs
    public RandomFromCollectionGenerator(T... items) {
        this.items = copyOf(checkNotNull(items, "Collection for generation can't be null"));
    }

    @Override
    public T next() {
        int maximumIndex = items.size() - 1;
        return maximumIndex > 0
                ? items.get(random.nextInt(maximumIndex))
                : maximumIndex == 0 ? items.get(0) : null;
    }
}

@@ -377,19 +377,16 @@
 lements%0A
- *%0A
  * @auth
@@ -1406,15 +1406,12 @@
     
-return 
+if (
 maxi
@@ -1422,16 +1422,19 @@
 ndex %3E 0
+) %7B
 %0A       
@@ -1442,13 +1442,14 @@
     
-    ?
+return
  ite
@@ -1484,28 +1484,26 @@
 mIndex))
+;
 %0A
-   
         
      : m
@@ -1498,15 +1498,19 @@
     
-     : 
+%7D else if (
 maxi
@@ -1526,31 +1526,96 @@
 == 0
- ? items.get(0) : null;
+) %7B%0A            return items.get(0);%0A        %7D else %7B%0A            return null;%0A        %7D
 %0A   

Remove insane double ternary operator