From 20a545825c9a7ba4659d2abb7871280d4559bc43 Mon Sep 17 00:00:00 2001 From: Redkale Date: Tue, 27 Sep 2022 13:44:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Sheet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/org/redkale/util/Sheet.java | 49 ++++++++++++++--------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/redkale/util/Sheet.java b/src/main/java/org/redkale/util/Sheet.java index 484a6bff0..6e7fd71a8 100644 --- a/src/main/java/org/redkale/util/Sheet.java +++ b/src/main/java/org/redkale/util/Sheet.java @@ -52,8 +52,9 @@ public class Sheet implements java.io.Serializable, Iterable { public Sheet copyTo(Sheet copy) { if (copy == null) return copy; copy.total = this.total; - if (this.getRows() != null) { - copy.setRows(new ArrayList(this.getRows())); + Collection data = this.getRows(); + if (data != null) { + copy.setRows(new ArrayList(data)); } else { copy.rows = null; } @@ -67,7 +68,8 @@ public class Sheet implements java.io.Serializable, Iterable { */ @ConvertColumn(index = 3) public boolean isEmpty() { - return this.rows == null || this.rows.isEmpty(); + Collection data = this.rows; + return data == null || data.isEmpty(); } @Override @@ -92,8 +94,9 @@ public class Sheet implements java.io.Serializable, Iterable { } 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); + Collection data = this.rows; + if (data == null) return created ? new ArrayList() : null; + return (data instanceof List) ? (List) data : new ArrayList(data); } public void setRows(Collection data) { @@ -102,53 +105,63 @@ public class Sheet implements java.io.Serializable, Iterable { @Override public Iterator iterator() { - return (this.rows == null) ? new ArrayList().iterator() : this.rows.iterator(); + Collection data = this.rows; + return (data == null) ? new ArrayList().iterator() : data.iterator(); } @Override public void forEach(final Consumer consumer) { - if (consumer != null && this.rows != null && !this.rows.isEmpty()) { - this.rows.forEach(consumer); + Collection data = this.rows; + if (consumer != null && data != null && !data.isEmpty()) { + data.forEach(consumer); } } public Sheet map(Function mapper) { - if (this.isEmpty()) return (Sheet) this; + Collection data = this.rows; + if (data == null || data.isEmpty()) return (Sheet) this; final List list = new ArrayList<>(); - for (T item : this.rows) { + for (T item : data) { 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); + Collection data = this.rows; + if (consumer != null && data != null && !data.isEmpty()) { + data.parallelStream().forEach(consumer); } } @Override public Spliterator spliterator() { - return (this.rows == null) ? new ArrayList().spliterator() : this.rows.spliterator(); + Collection data = this.rows; + return (data == null) ? new ArrayList().spliterator() : data.spliterator(); } public Stream stream() { - return (this.rows == null) ? new ArrayList().stream() : this.rows.stream(); + Collection data = this.rows; + return (data == null) ? new ArrayList().stream() : data.stream(); } public Stream parallelStream() { - return (this.rows == null) ? new ArrayList().parallelStream() : this.rows.parallelStream(); + Collection data = this.rows; + return (data == null) ? new ArrayList().parallelStream() : data.parallelStream(); } public Object[] toArray() { - return (this.rows == null) ? new ArrayList().toArray() : this.rows.toArray(); + Collection data = this.rows; + return (data == null) ? new ArrayList().toArray() : data.toArray(); } public E[] toArray(E[] a) { - return (this.rows == null) ? new ArrayList().toArray(a) : this.rows.toArray(a); + Collection data = this.rows; + return (data == null) ? new ArrayList().toArray(a) : data.toArray(a); } public E[] toArray(IntFunction generator) { - return (this.rows == null) ? new ArrayList().toArray(generator.apply(0)) : this.rows.toArray(generator.apply(this.rows.size())); + Collection data = this.rows; + return (data == null) ? new ArrayList().toArray(generator.apply(0)) : data.toArray(generator.apply(data.size())); } }