|
1 | 1 | package org.teachingextensions.logo; |
2 | 2 |
|
| 3 | +import org.teachingextensions.approvals.lite.util.NumberUtils; |
| 4 | + |
3 | 5 | import java.util.ArrayList; |
4 | 6 | import java.util.List; |
5 | 7 |
|
6 | | -import org.teachingextensions.approvals.lite.util.NumberUtils; |
7 | | - |
8 | 8 | /** |
9 | | - * A Wheel is a List with no ending on beginning <br> |
10 | | - * |
11 | | - * <b>Example:</b> {@code |
12 | | - * Wheel<String> names = new Wheel<String>(); |
13 | | - * names.add("Chocolate"); |
14 | | - * names.add("Peanut Butter"); |
15 | | - * for(int i = 0; i < 6; i++) > { |
16 | | - * String name = names.next(); |
17 | | - * System.out.print(name + " "); |
18 | | - * } |
19 | | - * } |
20 | | - * Would result in : <br> |
| 9 | + * A Wheel is a List with no ending on beginning <p> |
| 10 | + * <b>Example:</b><pre>{@code |
| 11 | + * Wheel<String> names = new Wheel<String>(); |
| 12 | + * names.add("Chocolate"); |
| 13 | + * names.add("Peanut Butter"); |
| 14 | + * for(int i = 0; i < 6; i++){ |
| 15 | + * String name = names.next(); |
| 16 | + * System.out.print(name + " "); |
| 17 | + * } |
| 18 | + * }</pre> |
| 19 | + * Would result in:<p> |
21 | 20 | * Chocolate Peanut Butter Chocolate Peanut Butter Chocolate Peanut Butter |
22 | | - * |
23 | | - * @param <T> |
| 21 | + * |
| 22 | + * @param <T> The kind of things that are in the wheel |
24 | 23 | */ |
25 | | -public class Wheel<T> |
26 | | -{ |
27 | | - private List<T> list = new ArrayList<T>(); |
28 | | - private int index = 0; |
29 | | - @SafeVarargs |
30 | | -public Wheel(T... loadWith) |
31 | | - { |
32 | | - for (T t : loadWith) |
33 | | - { |
34 | | - add(t); |
| 24 | +public class Wheel<T> { |
| 25 | + private List<T> list = new ArrayList<>(); |
| 26 | + private int index = 0; |
| 27 | + |
| 28 | + @SafeVarargs |
| 29 | + public Wheel(T... loadWith) { |
| 30 | + for (T t : loadWith) { |
| 31 | + add(t); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public Wheel() { |
35 | 36 | } |
36 | | - } |
37 | | - public Wheel() |
38 | | - { |
39 | | - } |
40 | | - public void add(T i) |
41 | | - { |
42 | | - list.add(i); |
43 | | - } |
44 | | - public T next() |
45 | | - { |
46 | | - assertNonEmpty(); |
47 | | - if (index >= list.size()) |
48 | | - { |
49 | | - index = 0; |
| 37 | + |
| 38 | + public void add(T i) { |
| 39 | + list.add(i); |
50 | 40 | } |
51 | | - T t = list.get(index++); |
52 | | - return t; |
53 | | - } |
54 | | - private void assertNonEmpty() |
55 | | - { |
56 | | - if (list.isEmpty()) |
57 | | - { |
58 | | - String message = "I call shenanigans!!!\nThis Wheel is empty\nYou can NOT get something from the Wheel before you've added anything to it."; |
59 | | - throw new RuntimeException(message); |
| 41 | + |
| 42 | + public T next() { |
| 43 | + assertNonEmpty(); |
| 44 | + if (index >= list.size()) { |
| 45 | + index = 0; |
| 46 | + } |
| 47 | + return list.get(index++); |
| 48 | + } |
| 49 | + |
| 50 | + private void assertNonEmpty() { |
| 51 | + if (list.isEmpty()) { |
| 52 | + String message = "I call shenanigans!!!\nThis Wheel is empty\nYou can NOT get something from the Wheel before you've added anything to it."; |
| 53 | + throw new RuntimeException(message); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public T getRandomFrom() { |
| 58 | + assertNonEmpty(); |
| 59 | + int index = NumberUtils.getRandomInt(0, list.size()); |
| 60 | + return list.get(index); |
| 61 | + } |
| 62 | + |
| 63 | + public void empty() { |
| 64 | + list.clear(); |
| 65 | + index = 0; |
60 | 66 | } |
61 | | - } |
62 | | - public T getRandomFrom() |
63 | | - { |
64 | | - assertNonEmpty(); |
65 | | - int index = NumberUtils.getRandomInt(0, list.size()); |
66 | | - return list.get(index); |
67 | | - } |
68 | | - public void empty() |
69 | | - { |
70 | | - list.clear(); |
71 | | - index = 0; |
72 | | - } |
73 | 67 | } |
0 commit comments