All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.container.data.table.IDataTableRowMethods Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.data.table;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * Extension interface for {@link IDataTable} providing default implementations
 * for many row related methods.
 * 

* The only methods that really need to be implemented are * {@link #iterator(int, int)} and {@link #count()}. * * @author Oliver Richers */ public interface IDataTableRowMethods extends Iterable { /** * Returns an {@link Iterator} over all table rows. * * @return an {@link Iterator} over all filtered and sorted table rows */ @Override default Iterator iterator() { return iterable().iterator(); } /** * Returns an {@link Iterator} over all table rows in the given range. * * @param offset * the 0-based index of the first row to return or zero or * negative for no offset * @param limit * the maximum number of rows to return or zero or * negative for no limit * @return an {@link Iterator} over all filtered and sorted table rows */ Iterator iterator(int offset, int limit); /** * Returns all table rows as a {@link Stream}. * * @return a {@link Stream} of all filtered and sorted table rows */ default Stream stream() { return stream(0, 0); } /** * Returns at most limit table rows as a {@link Stream}. * * @return a {@link Stream} of filtered and sorted table rows */ default Stream stream(int limit) { return stream(0, limit); } /** * Returns all table rows in the given range as a {@link Stream}. * * @param offset * the 0-based index of the first row to return or zero or * negative for no offset * @param limit * the maximum number of rows to return or zero or * negative for no limit * @return a {@link Stream} of all filtered and sorted table rows */ default Stream stream(int offset, int limit) { return StreamSupport.stream(iterable(offset, limit).spliterator(), false); } /** * Returns all table rows as an {@link Iterable}. * * @return an {@link Iterable} of all filtered and sorted table rows */ default Iterable iterable() { return iterable(0, 0); } /** * Returns at most limit table rows as an {@link Iterable}. * * @return an {@link Iterable} of filtered and sorted table rows */ default Iterable iterable(int limit) { return iterable(0, limit); } /** * Returns all table rows in the given range as an {@link Iterable}. * * @param offset * the 0-based index of the first row to return or zero or * negative for no offset * @param limit * the maximum number of rows to return or zero or * negative for no limit * @return an {@link Iterable} of filtered and sorted table rows */ default Iterable iterable(int offset, int limit) { return () -> iterator(offset, limit); } /** * Returns all table rows as a {@link List}. * * @return a {@link List} of all filtered and sorted table rows */ default List list() { return list(0, 0); } /** * Returns at most limit table rows as a {@link List}. * * @return a {@link List} of filtered and sorted table rows */ default List list(int limit) { return list(0, limit); } /** * Returns all table rows in the given range as a {@link List}. * * @param offset * the 0-based index of the first row to return or zero or * negative for no offset * @param limit * the maximum number of rows to return or zero or * negative for no limit * @return a {@link List} of filtered and sorted table rows */ default List list(int offset, int limit) { return stream(offset, limit).collect(Collectors.toList()); } /** * Returns the total number of table rows after filtering. * * @return the total row count */ int count(); /** * Checks if at least one table row exists after filtering. * * @return true if one or more rows exist; false otherwise */ default boolean exists() { return count() > 0; } /** * Asserts that this table contains at most one row and returns it as an * {@link Optional}. * * @param exceptionFactory * the supplier to use to create the exception if multiple rows * were found * @return the optional row */ default Optional getOneAsOptional(Supplier exceptionFactory) { Iterator iterator = iterator(0, 2); Optional element = iterator.hasNext()? Optional.ofNullable(iterator.next()) : Optional.empty(); if (iterator.hasNext()) { throw exceptionFactory.get(); } return element; } /** * Asserts that this table contains at most one row and returns it as an * {@link Optional}. * * @return the optional row * @throws RuntimeException * if more than one row was found */ default Optional getOneAsOptional() { return getOneAsOptional(() -> new RuntimeException("Multiple rows found while only one was expected.")); } /** * Returns the first row of this table as an {@link Optional}. * * @return the optional first row */ default Optional getFirstAsOptional() { Iterator iterator = iterator(0, 1); return iterator.hasNext()? Optional.ofNullable(iterator.next()) : Optional.empty(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy