/* * Copyright 2015 Red Hat, Inc. and/or its affiliates * and other contributors as indicated by the @author tags. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hawkular.alerts.api.model.paging; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Spliterator; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.UnaryOperator; import java.util.stream.Stream; /** * A read-only list representing a single page of some results. * * <p>Contains a reference to the paging state object that describes the position of the page in some overall results. * * @author Lukas Krejci * @since 0.0.1 */ public final class Page<T> implements List<T> { private final List<T> wrapped; private final PageContext pageContext; private final long totalSize; public Page(List<T> wrapped, PageContext pageContext, long totalSize) { this.wrapped = wrapped; this.pageContext = pageContext; this.totalSize = totalSize; } /** * @return the information about the page of the results that this object represents */ public PageContext getPageContext() { return pageContext; } /** * @return the total number of results of which this page is a subset of */ public long getTotalSize() { return totalSize; } @Override public boolean add(T t) { throw new UnsupportedOperationException(); } @Override public void add(int index, T element) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection<? extends T> c) { throw new UnsupportedOperationException(); } @Override public boolean addAll(int index, Collection<? extends T> c) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object o) { return wrapped.contains(o); } @Override public boolean containsAll(Collection<?> c) { return wrapped.containsAll(c); } @Override public boolean equals(Object o) { return wrapped.equals(o); } @Override public T get(int index) { return wrapped.get(index); } @Override public int hashCode() { return wrapped.hashCode(); } @Override public int indexOf(Object o) { return wrapped.indexOf(o); } @Override public boolean isEmpty() { return wrapped.isEmpty(); } @Override public Iterator<T> iterator() { Iterator<T> it = wrapped.iterator(); return new Iterator<T>() { @Override public boolean hasNext() { return it.hasNext(); } @Override public T next() { return it.next(); } }; } @Override public int lastIndexOf(Object o) { return wrapped.lastIndexOf(o); } @Override public ListIterator<T> listIterator() { ListIterator<T> it = wrapped.listIterator(); return new ListIterator<T>() { @Override public boolean hasNext() { return it.hasNext(); } @Override public T next() { return it.next(); } @Override public boolean hasPrevious() { return it.hasPrevious(); } @Override public T previous() { return it.previous(); } @Override public int nextIndex() { return it.nextIndex(); } @Override public int previousIndex() { return it.previousIndex(); } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public void set(T t) { throw new UnsupportedOperationException(); } @Override public void add(T t) { throw new UnsupportedOperationException(); } }; } @Override public ListIterator<T> listIterator(int index) { return wrapped.listIterator(index); } @Override public T remove(int index) { throw new UnsupportedOperationException(); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); } @Override public void replaceAll(UnaryOperator<T> operator) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); } @Override public T set(int index, T element) { throw new UnsupportedOperationException(); } @Override public int size() { return wrapped.size(); } @Override public void sort(Comparator<? super T> c) { throw new UnsupportedOperationException(); } @Override public Spliterator<T> spliterator() { return wrapped.spliterator(); } @Override public List<T> subList(int fromIndex, int toIndex) { return new Page<>(wrapped.subList(fromIndex, toIndex), Pager.unlimited(pageContext.getOrder()), totalSize); } @Override public Object[] toArray() { return wrapped.toArray(); } @Override public <T1> T1[] toArray(T1[] a) { return wrapped.toArray(a); } @Override public Stream<T> parallelStream() { return wrapped.parallelStream(); } @Override public boolean removeIf(Predicate<? super T> filter) { throw new UnsupportedOperationException(); } @Override public Stream<T> stream() { return wrapped.stream(); } @Override public void forEach(Consumer<? super T> action) { wrapped.forEach(action); } }