org.redkale.util.Sheet Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.util;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
/**
* 页集合。 结构由一个total总数和一个List列表组合而成。
*
*
* 详情见: https://redkale.org
*
* @author zhangjx
* @param 集合元素的数据类型
*/
@SuppressWarnings("unchecked")
public class Sheet implements java.io.Serializable, Iterable {
private long total = -1;
private Collection rows;
public Sheet() {
super();
}
public Sheet(int total, Collection data) {
this((long) total, data);
}
public Sheet(long total, Collection data) {
this.total = total;
this.rows = (Collection) data;
}
public static Sheet asSheet(Collection data) {
return data == null ? new Sheet() : new Sheet(data.size(), data);
}
public static Sheet empty() {
return new Sheet<>();
}
public Sheet copyTo(Sheet copy) {
if (copy == null) return copy;
copy.total = this.total;
if (this.getRows() != null) {
copy.setRows(new ArrayList(this.getRows()));
} else {
copy.rows = null;
}
return copy;
}
/**
* 判断数据列表是否为空
*
* @return 是否为空
*/
public boolean isEmpty() {
return this.rows == null || this.rows.isEmpty();
}
@Override
public String toString() {
return "{\"total\":" + this.total + ", \"rows\":" + this.rows + "}";
}
public long getTotal() {
return this.total;
}
public void setTotal(long total) {
this.total = total;
}
public Collection getRows() {
return this.rows;
}
public List list() {
return list(false);
}
public List list(boolean created) {
if (this.rows == null) return created ? new ArrayList() : null;
return (this.rows instanceof List) ? (List) this.rows : new ArrayList(this.rows);
}
public void setRows(Collection data) {
this.rows = (Collection) data;
}
@Override
public Iterator iterator() {
return (this.rows == null) ? new ArrayList().iterator() : this.rows.iterator();
}
@Override
public void forEach(final Consumer consumer) {
if (consumer != null && this.rows != null && !this.rows.isEmpty()) {
this.rows.forEach(consumer);
}
}
public Sheet map(Function mapper) {
if (this.isEmpty()) return (Sheet) this;
final List list = new ArrayList<>();
for (T item : this.rows) {
list.add(mapper.apply(item));
}
return new Sheet<>(getTotal(), list);
}
public void forEachParallel(final Consumer consumer) {
if (consumer != null && this.rows != null && !this.rows.isEmpty()) {
this.rows.parallelStream().forEach(consumer);
}
}
@Override
public Spliterator spliterator() {
return (this.rows == null) ? new ArrayList().spliterator() : this.rows.spliterator();
}
public Stream stream() {
return (this.rows == null) ? new ArrayList().stream() : this.rows.stream();
}
public Stream parallelStream() {
return (this.rows == null) ? new ArrayList().parallelStream() : this.rows.parallelStream();
}
public Object[] toArray() {
return (this.rows == null) ? new ArrayList().toArray() : this.rows.toArray();
}
public T[] toArray(T[] a) {
return (this.rows == null) ? new ArrayList().toArray(a) : this.rows.toArray(a);
}
}