--- /dev/null
+package net.pterodactylus.sone.template;
+
+import java.util.List;
+import java.util.Random;
+
+import net.pterodactylus.util.template.Accessor;
+import net.pterodactylus.util.template.TemplateContext;
+
+/**
+ * {@link Accessor} implementation for {@link List}s that can return a random value from the list.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ListAccessor extends net.pterodactylus.util.template.ListAccessor {
+
+ private Random random = new Random();
+
+ @Override
+ public Object get(TemplateContext templateContext, Object object, String member) {
+ if ("random".equals(member)) {
+ List<?> list = (List<?>) object;
+ if (!list.isEmpty()) {
+ return list.get(random.nextInt(list.size()));
+ }
+ return null;
+ }
+ return super.get(templateContext, object, member);
+ }
+
+}
import net.pterodactylus.sone.template.ImageAccessor;
import net.pterodactylus.sone.template.ImageLinkFilter;
import net.pterodactylus.sone.template.JavascriptFilter;
+import net.pterodactylus.sone.template.ListAccessor;
import net.pterodactylus.sone.template.ParserFilter;
import net.pterodactylus.sone.template.PostAccessor;
import net.pterodactylus.sone.template.ProfileAccessor;
templateContextFactory.addAccessor(Trust.class, new TrustAccessor());
templateContextFactory.addAccessor(HTTPRequest.class, new HttpRequestAccessor());
templateContextFactory.addAccessor(Profile.class, new ProfileAccessor(getCore()));
+ templateContextFactory.addAccessor(List.class, new ListAccessor());
templateContextFactory.addFilter("date", new DateFilter());
templateContextFactory.addFilter("html", new HtmlFilter());
templateContextFactory.addFilter("replace", new ReplaceFilter());
--- /dev/null
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.Matchers.sameInstance;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import net.pterodactylus.util.template.Accessor;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link ListAccessorTest}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ListAccessorTest {
+
+ private final Accessor accessor = new ListAccessor();
+
+ @Test
+ public void gettingARandomElementFromAnEmptyListReturnsNull() {
+ assertThat(accessor.get(null, Collections.emptyList(), "random"), nullValue());
+ }
+
+ @Test
+ public void gettingARandomElementFromAListOfOneWillReturnTheOneElement() {
+ Object object = new Object();
+ assertThat(accessor.get(null, Arrays.asList(object), "random"), sameInstance(object));
+ }
+
+ @Test
+ public void gettingRandomElementsFromAListTwoElementsWillReturnBothWithSomeProportion() {
+ Object first = new Object();
+ Object second = new Object();
+ List<?> objects = Arrays.asList(first, second);
+ int gotFirst = 0;
+ for (int i = 0; i < 10000; i++) {
+ if (accessor.get(null, objects, "random") == first) {
+ gotFirst++;
+ }
+ }
+ assertThat(gotFirst, greaterThanOrEqualTo(4000));
+ }
+
+}