io.quarkus.qute.runtime.extensions.CollectionTemplateExtensions Maven / Gradle / Ivy
package io.quarkus.qute.runtime.extensions;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import javax.enterprise.inject.Vetoed;
import io.quarkus.qute.TemplateExtension;
@Vetoed // Make sure no bean is created from this class
@TemplateExtension
public class CollectionTemplateExtensions {
static T get(List list, int index) {
return list.get(index);
}
@TemplateExtension(matchRegex = "\\d{1,10}")
static T getByIndex(List list, String index) {
return list.get(Integer.parseInt(index));
}
static Iterator reversed(List list) {
ListIterator it = list.listIterator(list.size());
return new Iterator() {
@Override
public boolean hasNext() {
return it.hasPrevious();
}
@Override
public T next() {
return it.previous();
}
};
}
static List take(List list, int n) {
if (n < 1 || n > list.size()) {
throw new IndexOutOfBoundsException(n);
}
if (list.isEmpty()) {
return list;
}
return list.subList(0, n);
}
static List takeLast(List list, int n) {
if (n < 1 || n > list.size()) {
throw new IndexOutOfBoundsException(n);
}
if (list.isEmpty()) {
return list;
}
return list.subList(list.size() - n, list.size());
}
// This extension method has higher priority than ValueResolvers.orEmpty()
// and makes it possible to validate expressions derived from {list.orEmpty}
static Collection orEmpty(Collection iterable) {
return iterable != null ? iterable : Collections.emptyList();
}
static T first(List list) {
if (list.isEmpty()) {
throw new NoSuchElementException();
}
return list.get(0);
}
static T last(List list) {
if (list.isEmpty()) {
throw new NoSuchElementException();
}
return list.get(list.size() - 1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy