Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.util.RowDataSet Maven / Gradle / Ivy
Go to download
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2015, Haiyang Li.
*
* 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 com.landawn.abacus.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
import com.landawn.abacus.annotation.SuppressFBWarnings;
import com.landawn.abacus.exception.UncheckedIOException;
import com.landawn.abacus.parser.JSONParser;
import com.landawn.abacus.parser.JSONSerializationConfig;
import com.landawn.abacus.parser.JSONSerializationConfig.JSC;
import com.landawn.abacus.parser.KryoParser;
import com.landawn.abacus.parser.ParserFactory;
import com.landawn.abacus.parser.ParserUtil;
import com.landawn.abacus.parser.ParserUtil.BeanInfo;
import com.landawn.abacus.parser.ParserUtil.PropInfo;
import com.landawn.abacus.parser.XMLConstants;
import com.landawn.abacus.parser.XMLParser;
import com.landawn.abacus.parser.XMLSerializationConfig;
import com.landawn.abacus.parser.XMLSerializationConfig.XSC;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.Fn.Factory;
import com.landawn.abacus.util.If.OrElse;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableObjArray;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.function.IntObjConsumer;
import com.landawn.abacus.util.function.IntObjFunction;
import com.landawn.abacus.util.function.TriFunction;
import com.landawn.abacus.util.function.TriPredicate;
import com.landawn.abacus.util.stream.IntStream;
import com.landawn.abacus.util.stream.ObjIteratorEx;
import com.landawn.abacus.util.stream.Stream;
@SuppressWarnings({ "java:S1192", "java:S1698", "java:S1854", "java:S6539" })
public class RowDataSet implements DataSet, Cloneable {
static final DataSet EMPTY_DATA_SET = new RowDataSet(N.emptyList(), N.emptyList());
static {
EMPTY_DATA_SET.freeze();
}
static final char PROP_NAME_SEPARATOR = '.';
static final String NULL_STRING = "null";
static final char[] NULL_CHAR_ARRAY = NULL_STRING.toCharArray();
static final String TRUE = Boolean.TRUE.toString().intern();
static final char[] TRUE_CHAR_ARRAY = TRUE.toCharArray();
static final String FALSE = Boolean.FALSE.toString().intern();
static final char[] FALSE_CHAR_ARRAY = FALSE.toCharArray();
static final Set> SUPPORTED_COUNT_COLUMN_TYPES = N.asSet(int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class,
Double.class);
static final String POSTFIX_FOR_SAME_JOINED_COLUMN_NAME = "_2";
static final String CACHED_PROP_NAMES = "cachedPropNames";
private static final String ROW = "row";
private static final JSONParser jsonParser = ParserFactory.createJSONParser();
private static final XMLParser xmlParser = ParserFactory.isXMLAvailable() ? ParserFactory.createXMLParser() : null;
private static final KryoParser kryoParser = ParserFactory.isKryoAvailable() ? ParserFactory.createKryoParser() : null;
private static final JSONSerializationConfig jsc = JSC.create().setDateTimeFormat(DateTimeFormat.ISO_8601_TIMESTAMP);
private static final XMLSerializationConfig xsc = XSC.create().setDateTimeFormat(DateTimeFormat.ISO_8601_TIMESTAMP);
private static final Type strType = N.typeOf(String.class);
private List _columnNameList; //NOSONAR
private List> _columnList; //NOSONAR
private Map _columnIndexMap; //NOSONAR
private int[] _columnIndexes; //NOSONAR
private int _currentRowNum = 0; //NOSONAR
private boolean _isFrozen = false; //NOSONAR
private Map _properties; //NOSONAR
private transient int modCount = 0; //NOSONAR
// For Kryo
protected RowDataSet() {
_properties = N.emptyMap();
}
public RowDataSet(final List columnNameList, final List> columnList) {
this(columnNameList, columnList, null);
}
public RowDataSet(final List columnNameList, final List> columnList, final Map properties)
throws IllegalArgumentException {
N.checkArgNotNull(columnNameList);
N.checkArgNotNull(columnList);
N.checkArgument(!N.anyEmpty(columnNameList), "Empty column name found in: {}", columnNameList);
N.checkArgument(!N.hasDuplicates(columnNameList), "Duplicated column names found in: {}", columnNameList);
N.checkArgument(columnNameList.size() == columnList.size(), "The size of column name list: {} is different from the size of column list: {}",
columnNameList.size(), columnList.size());
final int size = columnList.size() == 0 ? 0 : columnList.get(0).size();
for (final List column : columnList) {
N.checkArgument(column.size() == size, "All columns in the specified 'columnList' must have same size.");
}
_columnNameList = columnNameList;
_columnList = columnList;
if (N.isEmpty(properties)) {
_properties = N.emptyMap();
} else {
_properties = Maps.newOrderingMap(properties);
_properties.putAll(properties);
}
}
// @Override
// public String beanName() {
// return _beanName;
// }
//
// @SuppressWarnings("unchecked")
// @Override
// public Class beanClass() {
// return (Class) _beanClass;
// }
@Override
public ImmutableList columnNameList() {
// return _columnNameList;
return ImmutableList.wrap(_columnNameList);
}
@Override
public int columnCount() {
return _columnNameList.size();
}
@Override
public String getColumnName(final int columnIndex) {
return _columnNameList.get(columnIndex);
}
@Override
public int getColumnIndex(final String columnName) throws IllegalArgumentException {
if (_columnIndexMap == null) {
_columnIndexMap = new HashMap<>();
int i = 0;
for (final String e : _columnNameList) {
_columnIndexMap.put(e, i++);
}
}
final Integer columnIndex = _columnIndexMap.get(columnName);
// if (columnIndex == null /* && NameUtil.isCanonicalName(_beanName, columnName)*/) {
// columnIndex = _columnIndexMap.get(NameUtil.getSimpleName(columnName));
// }
if (columnIndex == null) {
throw new IllegalArgumentException("The specified column: " + columnName + " is not included in this DataSet: " + _columnNameList);
}
return columnIndex;
}
int checkColumnName(final String columnName) throws IllegalArgumentException {
return getColumnIndex(columnName);
}
@Override
public int[] getColumnIndexes(final Collection columnNames) throws IllegalArgumentException {
if (N.isEmpty(columnNames)) {
return N.EMPTY_INT_ARRAY;
}
if (isColumnNameList(columnNames)) {
if (_columnIndexes == null) {
final int count = columnNames.size();
_columnIndexes = new int[count];
for (int i = 0; i < count; i++) {
_columnIndexes[i] = i;
}
}
return _columnIndexes.clone();
}
if (_columnIndexMap == null) {
_columnIndexMap = new HashMap<>();
int i = 0;
for (final String e : _columnNameList) {
_columnIndexMap.put(e, i++);
}
}
final int[] columnIndexes = new int[columnNames.size()];
int i = 0;
Integer columnIndex = null;
for (final String columnName : columnNames) {
columnIndex = _columnIndexMap.get(columnName);
// if (columnIndex == null /* && NameUtil.isCanonicalName(_beanName, columnName)*/) {
// columnIndex = _columnIndexMap.get(NameUtil.getSimpleName(columnName));
// }
if (columnIndex == null) {
throw new IllegalArgumentException("The specified column: " + columnName + " is not included in this DataSet: " + _columnNameList);
}
columnIndexes[i++] = columnIndex;
}
return columnIndexes;
}
int[] checkColumnNames(final Collection columnNames) throws IllegalArgumentException {
if (N.isEmpty(columnNames)) {
throw new IllegalArgumentException("The specified columnNames is null or empty");
}
if (isColumnNameList(columnNames)) {
if (_columnIndexes == null) {
final int count = columnNames.size();
_columnIndexes = new int[count];
for (int i = 0; i < count; i++) {
_columnIndexes[i] = i;
}
}
return _columnIndexes;
}
return getColumnIndexes(columnNames);
}
boolean isColumnNameList(final Collection columnNames) {
if (columnNames == _columnNameList) {
return true;
}
if (columnNames instanceof final ImmutableList immutableList) { // NOSONAR
return immutableList.list == _columnNameList;
}
return false;
}
@Override
public boolean containsColumn(final String columnName) {
if (_columnIndexMap == null) {
_columnIndexMap = new HashMap<>();
int i = 0;
for (final String e : _columnNameList) {
_columnIndexMap.put(e, i++);
}
}
return _columnIndexMap.containsKey(columnName); // || _columnIndexMap.containsKey(NameUtil.getSimpleName(columnName));
}
@Override
public boolean containsAllColumns(final Collection columnNames) {
for (final String columnName : columnNames) {
if (!containsColumn(columnName)) {
return false;
}
}
return true;
}
@Override
public void renameColumn(final String columnName, final String newColumnName) throws IllegalArgumentException {
checkFrozen();
final int idx = checkColumnName(columnName);
if (columnName.equals(newColumnName)) {
// ignore.
} else {
if (_columnNameList.contains(newColumnName)) {
throw new IllegalArgumentException("The new column name: " + newColumnName + " is already included this DataSet: " + _columnNameList);
}
if (_columnIndexMap != null) {
_columnIndexMap.put(newColumnName, _columnIndexMap.remove(_columnNameList.get(idx)));
}
_columnNameList.set(idx, newColumnName);
}
modCount++;
}
@Override
public void renameColumns(final Map oldNewNames) throws IllegalArgumentException {
checkFrozen();
if (N.hasDuplicates(oldNewNames.values())) {
throw new IllegalArgumentException("Duplicated new column names: " + oldNewNames.values());
}
for (final Map.Entry entry : oldNewNames.entrySet()) {
checkColumnName(entry.getKey());
if (_columnNameList.contains(entry.getValue()) && !entry.getKey().equals(entry.getValue())) {
throw new IllegalArgumentException("The new column name: " + entry.getValue() + " is already included this DataSet: " + _columnNameList);
}
}
for (final Map.Entry entry : oldNewNames.entrySet()) {
renameColumn(entry.getKey(), entry.getValue());
}
}
// @Override
// public void renameColumn(final String columnName, final Function super String, String> func) {
// renameColumn(columnName, func.apply(columnName));
// }
@Override
public void renameColumns(final Collection columnNames, final Function super String, String> func) throws IllegalArgumentException {
checkColumnNames(columnNames);
final Map map = N.newHashMap(columnNames.size());
for (final String columnName : columnNames) {
map.put(columnName, func.apply(columnName));
}
renameColumns(map);
}
@Override
public void renameColumns(final Function super String, String> func) throws IllegalArgumentException {
renameColumns(_columnNameList, func);
}
@Override
public void moveColumn(final String columnName, final int newPosition) throws IllegalArgumentException {
checkFrozen();
final int idx = checkColumnName(columnName);
if (newPosition < 0 || newPosition >= columnCount()) {
throw new IllegalArgumentException("The new column index must be >= 0 and < " + columnCount());
}
if (idx == newPosition) {
// ignore.
} else {
_columnNameList.add(newPosition, _columnNameList.remove(idx));
_columnList.add(newPosition, _columnList.remove(idx));
_columnIndexMap = null;
_columnIndexes = null;
}
modCount++;
}
@Override
public void moveColumns(final Map columnNameNewPositionMap) {
checkFrozen();
final List> entries = new ArrayList<>(columnNameNewPositionMap.size());
for (final Map.Entry entry : columnNameNewPositionMap.entrySet()) {
checkColumnName(entry.getKey());
if (entry.getValue() < 0 || entry.getValue() >= columnCount()) {
throw new IllegalArgumentException("The new column index must be >= 0 and < " + columnCount());
}
entries.add(entry);
}
N.sort(entries, Comparators.comparingByValue());
for (final Map.Entry entry : entries) {
final int currentColumnIndex = checkColumnName(entry.getKey());
if (currentColumnIndex == entry.getValue()) {
// ignore.
} else {
_columnNameList.add(entry.getValue(), _columnNameList.remove(currentColumnIndex));
_columnList.add(entry.getValue(), _columnList.remove(currentColumnIndex));
_columnIndexMap = null;
}
}
modCount++;
}
@Override
public void swapColumns(final String columnNameA, final String columnNameB) {
checkFrozen();
final int columnIndexA = checkColumnName(columnNameA);
final int columnIndexB = checkColumnName(columnNameB);
if (columnNameA.equals(columnNameB)) {
return;
}
final String tmpColumnNameA = _columnNameList.get(columnIndexA);
_columnNameList.set(columnIndexA, _columnNameList.get(columnIndexB));
_columnNameList.set(columnIndexB, tmpColumnNameA);
final List tmpColumnA = _columnList.get(columnIndexA);
_columnList.set(columnIndexA, _columnList.get(columnIndexB));
_columnList.set(columnIndexB, tmpColumnA);
if (N.notEmpty(_columnIndexMap)) {
_columnIndexMap.put(columnNameA, columnIndexB);
_columnIndexMap.put(columnNameB, columnIndexA);
}
modCount++;
}
@Override
public void moveRow(final int rowIndex, final int newRowIndex) {
checkFrozen();
checkRowNum(rowIndex);
checkRowNum(newRowIndex);
if (rowIndex == newRowIndex) {
return;
}
for (final List column : _columnList) {
column.add(newRowIndex, column.remove(rowIndex));
}
modCount++;
}
@Override
public void swapRows(final int rowIndexA, final int rowIndexB) {
checkFrozen();
checkRowNum(rowIndexA);
checkRowNum(rowIndexB);
if (rowIndexA == rowIndexB) {
return;
}
Object tmp = null;
for (final List column : _columnList) {
tmp = column.get(rowIndexA);
column.set(rowIndexA, column.get(rowIndexB));
column.set(rowIndexB, tmp);
}
modCount++;
}
@Override
public T get(final int rowIndex, final int columnIndex) {
return (T) _columnList.get(columnIndex).get(rowIndex);
}
// @Override
// public T get(final Class extends T> targetType, final int rowIndex, final int columnIndex) {
// T rt = (T) _columnList.get(columnIndex).get(rowIndex);
//
// return (rt == null) ? N.defaultValueOf(targetType) : rt;
// }
@Override
public void set(final int rowIndex, final int columnIndex, final Object element) {
checkFrozen();
_columnList.get(columnIndex).set(rowIndex, element);
modCount++;
}
@Override
public boolean isNull(final int rowIndex, final int columnIndex) {
return get(rowIndex, columnIndex) == null;
}
@Override
public T get(final int columnIndex) {
return (T) _columnList.get(columnIndex).get(_currentRowNum);
}
// @Override
// public T get(final Class extends T> targetType, final int columnIndex) {
// T rt = get(columnIndex);
//
// return (rt == null) ? N.defaultValueOf(targetType) : rt;
// }
@Override
public T get(final String columnName) {
return get(checkColumnName(columnName));
}
// @Override
// public T get(final Class extends T> targetType, final String columnName) {
// return get(targetType, checkColumnName(columnName));
// }
//
// @Override
// public T getOrDefault(int columnIndex, T defaultValue) {
// return columnIndex < 0 ? defaultValue : (T) get(columnIndex);
// }
//
// @Override
// public T getOrDefault(final String columnName, T defaultValue) {
// return getOrDefault(getColumnIndex(columnName), defaultValue);
// }
@Override
public boolean getBoolean(final int columnIndex) {
final Boolean rt = get(columnIndex);
return rt != null && rt;
}
@Override
public boolean getBoolean(final String columnName) {
return getBoolean(checkColumnName(columnName));
}
@Override
public char getChar(final int columnIndex) {
final Character rt = get(columnIndex);
return (rt == null) ? 0 : rt;
}
@Override
public char getChar(final String columnName) {
return getChar(checkColumnName(columnName));
}
@Override
public byte getByte(final int columnIndex) {
final Number rt = get(columnIndex);
return (rt == null) ? 0 : rt.byteValue();
}
@Override
public byte getByte(final String columnName) {
return getByte(checkColumnName(columnName));
}
@Override
public short getShort(final int columnIndex) {
final Number rt = get(columnIndex);
return (rt == null) ? 0 : rt.shortValue();
}
@Override
public short getShort(final String columnName) {
return getShort(checkColumnName(columnName));
}
@Override
public int getInt(final int columnIndex) {
final Number rt = get(columnIndex);
return (rt == null) ? 0 : rt.intValue();
}
@Override
public int getInt(final String columnName) {
return getInt(checkColumnName(columnName));
}
@Override
public long getLong(final int columnIndex) {
final Number rt = get(columnIndex);
return (rt == null) ? 0L : rt.longValue();
}
@Override
public long getLong(final String columnName) {
return getLong(checkColumnName(columnName));
}
@Override
public float getFloat(final int columnIndex) {
final Number rt = get(columnIndex);
return (rt == null) ? 0f : Numbers.toFloat(rt);
}
@Override
public float getFloat(final String columnName) {
return getFloat(checkColumnName(columnName));
}
@Override
public double getDouble(final int columnIndex) {
final Number rt = get(columnIndex);
return (rt == null) ? 0d : Numbers.toDouble(rt);
}
@Override
public double getDouble(final String columnName) {
return getDouble(checkColumnName(columnName));
}
@Override
public boolean isNull(final int columnIndex) {
return get(columnIndex) == null;
}
@Override
public boolean isNull(final String columnName) {
return get(columnName) == null;
}
@Override
public void set(final int columnIndex, final Object value) {
checkFrozen();
_columnList.get(columnIndex).set(_currentRowNum, value);
modCount++;
}
@Override
public void set(final String columnName, final Object value) {
set(checkColumnName(columnName), value);
}
@SuppressWarnings("rawtypes")
@Override
public ImmutableList getColumn(final int columnIndex) {
// return (List) _columnList.get(columnIndex);
return ImmutableList.wrap((List) _columnList.get(columnIndex));
}
@Override
public ImmutableList getColumn(final String columnName) {
return getColumn(checkColumnName(columnName));
}
@SuppressWarnings("rawtypes")
@Override
public List copyColumn(final String columnName) {
return new ArrayList<>((List) _columnList.get(checkColumnName(columnName)));
}
@Override
public void addColumn(final String newColumnName, final Collection> column) {
addColumn(_columnList.size(), newColumnName, column);
}
@Override
public void addColumn(final int newColumnPosition, final String newColumnName, final Collection> column) {
checkFrozen();
if (newColumnPosition < 0 || newColumnPosition > columnCount()) {
throw new IllegalArgumentException("Invalid column index: " + newColumnPosition + ". It must be >= 0 and <= " + columnCount());
}
if (containsColumn(newColumnName)) {
throw new IllegalArgumentException("The new column name: " + newColumnName + " is already included this DataSet: " + _columnNameList);
}
if (N.notEmpty(column) && column.size() != size()) {
throw new IllegalArgumentException("The specified column size[" + column.size() + "] must be same as the this DataSet size[" + size() + "]. ");
}
_columnNameList.add(newColumnPosition, newColumnName);
if (N.isEmpty(column)) {
_columnList.add(newColumnPosition, N.repeat(null, size()));
} else {
_columnList.add(newColumnPosition, new ArrayList<>(column));
}
updateColumnIndex(newColumnPosition, newColumnName);
modCount++;
}
@Override
public void addColumn(final String newColumnName, final String fromColumnName, final Function, ?> func) {
addColumn(_columnList.size(), newColumnName, fromColumnName, func);
}
@Override
public void addColumn(final int newColumnPosition, final String newColumnName, final String fromColumnName, final Function, ?> func) {
checkFrozen();
if (newColumnPosition < 0 || newColumnPosition > columnCount()) {
throw new IllegalArgumentException("Invalid column index: " + newColumnPosition + ". It must be >= 0 and <= " + columnCount());
}
if (containsColumn(newColumnName)) {
throw new IllegalArgumentException("The new column name: " + newColumnName + " is already included this DataSet: " + _columnNameList);
}
final List newColumn = new ArrayList<>(size());
final Function mapperToUse = (Function) func;
final List column = _columnList.get(checkColumnName(fromColumnName));
for (final Object val : column) {
newColumn.add(mapperToUse.apply(val));
}
_columnNameList.add(newColumnPosition, newColumnName);
_columnList.add(newColumnPosition, newColumn);
updateColumnIndex(newColumnPosition, newColumnName);
modCount++;
}
@Override
public void addColumn(final String newColumnName, final Collection fromColumnNames, final Function super DisposableObjArray, ?> func) {
addColumn(_columnList.size(), newColumnName, fromColumnNames, func);
}
@Override
public void addColumn(final int newColumnPosition, final String newColumnName, final Collection fromColumnNames,
final Function super DisposableObjArray, ?> func) {
checkFrozen();
if (containsColumn(newColumnName)) {
throw new IllegalArgumentException("The new column name: " + newColumnName + " is already included this DataSet: " + _columnNameList);
}
final int size = size();
final int[] fromColumnIndexes = checkColumnNames(fromColumnNames);
final Function super DisposableObjArray, Object> mapperToUse = (Function super DisposableObjArray, Object>) func;
final List newColumn = new ArrayList<>(size);
final Object[] row = new Object[fromColumnIndexes.length];
final DisposableObjArray disposableArray = DisposableObjArray.wrap(row);
for (int rowIndex = 0; rowIndex < size; rowIndex++) {
for (int i = 0, len = fromColumnIndexes.length; i < len; i++) {
row[i] = _columnList.get(fromColumnIndexes[i]).get(rowIndex);
}
newColumn.add(mapperToUse.apply(disposableArray));
}
_columnNameList.add(newColumnPosition, newColumnName);
_columnList.add(newColumnPosition, newColumn);
updateColumnIndex(newColumnPosition, newColumnName);
modCount++;
}
private void updateColumnIndex(final int columnIndex, final String newColumnName) {
if (_columnIndexMap != null && columnIndex == _columnIndexMap.size()) {
_columnIndexMap.put(newColumnName, columnIndex);
} else {
_columnIndexMap = null;
}
if (_columnIndexes != null && columnIndex == _columnIndexes.length) {
_columnIndexes = N.copyOf(_columnIndexes, _columnIndexes.length + 1);
_columnIndexes[columnIndex] = columnIndex;
} else {
_columnIndexes = null;
}
}
@Override
public void addColumn(final String newColumnName, final Tuple2 fromColumnNames, final BiFunction, ?, ?> func) {
addColumn(_columnList.size(), newColumnName, fromColumnNames, func);
}
@Override
public void addColumn(final int newColumnPosition, final String newColumnName, final Tuple2 fromColumnNames,
final BiFunction, ?, ?> func) {
checkFrozen();
if (containsColumn(newColumnName)) {
throw new IllegalArgumentException("The new column name: " + newColumnName + " is already included this DataSet: " + _columnNameList);
}
final int size = size();
final List column1 = _columnList.get(checkColumnName(fromColumnNames._1));
final List column2 = _columnList.get(checkColumnName(fromColumnNames._2));
final BiFunction mapperToUse = (BiFunction) func;
final List newColumn = new ArrayList<>(size());
for (int rowIndex = 0; rowIndex < size; rowIndex++) {
newColumn.add(mapperToUse.apply(column1.get(rowIndex), column2.get(rowIndex)));
}
_columnNameList.add(newColumnPosition, newColumnName);
_columnList.add(newColumnPosition, newColumn);
updateColumnIndex(newColumnPosition, newColumnName);
modCount++;
}
@Override
public void addColumn(final String newColumnName, final Tuple3 fromColumnNames, final TriFunction, ?, ?, ?> func) {
addColumn(_columnList.size(), newColumnName, fromColumnNames, func);
}
@Override
public void addColumn(final int newColumnPosition, final String newColumnName, final Tuple3 fromColumnNames,
final TriFunction, ?, ?, ?> func) {
checkFrozen();
if (containsColumn(newColumnName)) {
throw new IllegalArgumentException("The new column name: " + newColumnName + " is already included this DataSet: " + _columnNameList);
}
final int size = size();
final List column1 = _columnList.get(checkColumnName(fromColumnNames._1));
final List column2 = _columnList.get(checkColumnName(fromColumnNames._2));
final List column3 = _columnList.get(checkColumnName(fromColumnNames._3));
final TriFunction mapperToUse = (TriFunction) func;
final List newColumn = new ArrayList<>(size());
for (int rowIndex = 0; rowIndex < size; rowIndex++) {
newColumn.add(mapperToUse.apply(column1.get(rowIndex), column2.get(rowIndex), column3.get(rowIndex)));
}
_columnNameList.add(newColumnPosition, newColumnName);
_columnList.add(newColumnPosition, newColumn);
updateColumnIndex(newColumnPosition, newColumnName);
modCount++;
}
@SuppressWarnings("rawtypes")
@Override
public List removeColumn(final String columnName) {
checkFrozen();
final int columnIndex = checkColumnName(columnName);
_columnIndexMap = null;
_columnIndexes = null;
_columnNameList.remove(columnIndex);
final List removedColumn = _columnList.remove(columnIndex);
modCount++;
return (List) removedColumn;
}
@Override
public void removeColumns(final Collection columnNames) {
checkFrozen();
if (N.isEmpty(columnNames)) {
return;
}
final int[] columnIndexes = checkColumnNames(columnNames);
N.sort(columnIndexes);
for (int i = 0, len = columnIndexes.length; i < len; i++) {
_columnNameList.remove(columnIndexes[i] - i);
_columnList.remove(columnIndexes[i] - i);
}
_columnIndexMap = null;
_columnIndexes = null;
modCount++;
}
@Override
public void removeColumns(final Predicate super String> filter) {
checkFrozen();
final List columnNames = filterColumnNames(_columnNameList, filter);
if (N.notEmpty(columnNames)) {
removeColumns(columnNames);
}
}
// @Deprecated
// @Override
// public void removeColumnsIf(Predicate super String, E> filter) {
// removeColumns(filter);
// }
@Override
public void convertColumn(final String columnName, final Class> targetType) {
checkFrozen();
convertColumnType(checkColumnName(columnName), targetType);
}
@Override
public void convertColumns(final Map> columnTargetTypes) {
checkFrozen();
if (N.isEmpty(columnTargetTypes)) {
return;
}
checkColumnNames(columnTargetTypes.keySet());
for (final Map.Entry> entry : columnTargetTypes.entrySet()) {
convertColumnType(checkColumnName(entry.getKey()), entry.getValue());
}
}
@Override
public void updateColumn(final String columnName, final Function, ?> func) {
checkFrozen();
final Function funcToUse = (Function) func;
final List column = _columnList.get(checkColumnName(columnName));
for (int i = 0, len = size(); i < len; i++) {
column.set(i, funcToUse.apply(column.get(i)));
}
modCount++;
}
@Override
public void updateColumns(final Collection columnNames, final Function, ?> func) {
checkFrozen();
if (N.isEmpty(columnNames)) {
return;
}
checkColumnNames(columnNames);
final Function funcToUse = (Function) func;
for (final String columnName : columnNames) {
final List column = _columnList.get(checkColumnName(columnName));
for (int i = 0, len = size(); i < len; i++) {
column.set(i, funcToUse.apply(column.get(i)));
}
}
modCount++;
}
private void convertColumnType(final int columnIndex, final Class> targetType) {
final List column = _columnList.get(columnIndex);
Object newValue = null;
for (int i = 0, len = size(); i < len; i++) {
newValue = N.convert(column.get(i), targetType);
column.set(i, newValue);
}
modCount++;
}
@Override
public void combineColumns(final Collection columnNames, final String newColumnName, final Class> newColumnType) {
checkFrozen();
final List newColumn = toList(0, size(), columnNames, newColumnType);
removeColumns(columnNames);
addColumn(newColumnName, newColumn);
}
@Override
public void combineColumns(final Collection columnNames, final String newColumnName, final Function super DisposableObjArray, ?> combineFunc) {
addColumn(newColumnName, columnNames, combineFunc);
removeColumns(columnNames);
}
@Override
public void combineColumns(final Predicate super String> columnNameFilter, final String newColumnName, final Class> newColumnType) {
combineColumns(filterColumnNames(_columnNameList, columnNameFilter), newColumnName, newColumnType);
}
@Override
public void combineColumns(final Predicate super String> columnNameFilter, final String newColumnName,
final Function super DisposableObjArray, ?> combineFunc) {
combineColumns(filterColumnNames(_columnNameList, columnNameFilter), newColumnName, combineFunc);
}
@Override
public void combineColumns(final Tuple2 columnNames, final String newColumnName, final BiFunction, ?, ?> combineFunc) {
addColumn(newColumnName, columnNames, combineFunc);
removeColumns(Arrays.asList(columnNames._1, columnNames._2));
}
@Override
public void combineColumns(final Tuple3 columnNames, final String newColumnName, final TriFunction, ?, ?, ?> combineFunc) {
addColumn(newColumnName, columnNames, combineFunc);
removeColumns(Arrays.asList(columnNames._1, columnNames._2, columnNames._3));
}
@Override
public void divideColumn(final String columnName, final Collection newColumnNames, final Function, ? extends List>> divideFunc) {
checkFrozen();
final int columnIndex = checkColumnName(columnName);
if (N.isEmpty(newColumnNames)) {
throw new IllegalArgumentException("New column names can't be null or empty.");
}
if (!N.disjoint(_columnNameList, newColumnNames)) {
throw new IllegalArgumentException("Column names: " + N.intersection(_columnNameList, newColumnNames) + " already are included in this data set.");
}
final Function> divideFuncToUse = (Function>) divideFunc;
final int newColumnsLen = newColumnNames.size();
final List> newColumns = new ArrayList<>(newColumnsLen);
for (int i = 0; i < newColumnsLen; i++) {
newColumns.add(new ArrayList<>(size()));
}
final List column = _columnList.get(columnIndex);
for (final Object val : column) {
final List newVals = divideFuncToUse.apply(val);
for (int i = 0; i < newColumnsLen; i++) {
newColumns.get(i).add(newVals.get(i));
}
}
_columnNameList.remove(columnIndex);
_columnNameList.addAll(columnIndex, newColumnNames);
_columnList.remove(columnIndex);
_columnList.addAll(columnIndex, newColumns);
_columnIndexMap = null;
_columnIndexes = null;
modCount++;
}
@Override
public void divideColumn(final String columnName, final Collection newColumnNames, final BiConsumer, Object[]> output) {
checkFrozen();
final int columnIndex = checkColumnName(columnName);
if (N.isEmpty(newColumnNames)) {
throw new IllegalArgumentException("New column names can't be null or empty.");
}
if (!N.disjoint(_columnNameList, newColumnNames)) {
throw new IllegalArgumentException("Column names: " + N.intersection(_columnNameList, newColumnNames) + " already are included in this data set.");
}
final BiConsumer outputToUse = (BiConsumer) output;
final int newColumnsLen = newColumnNames.size();
final List> newColumns = new ArrayList<>(newColumnsLen);
for (int i = 0; i < newColumnsLen; i++) {
newColumns.add(new ArrayList<>(size()));
}
final List column = _columnList.get(columnIndex);
final Object[] tmp = new Object[newColumnsLen];
for (final Object val : column) {
outputToUse.accept(val, tmp);
for (int i = 0; i < newColumnsLen; i++) {
newColumns.get(i).add(tmp[i]);
}
}
_columnNameList.remove(columnIndex);
_columnNameList.addAll(columnIndex, newColumnNames);
_columnList.remove(columnIndex);
_columnList.addAll(columnIndex, newColumns);
_columnIndexMap = null;
_columnIndexes = null;
modCount++;
}
@Override
public void divideColumn(final String columnName, final Tuple2 newColumnNames, final BiConsumer, Pair> output) {
checkFrozen();
final int columnIndex = checkColumnName(columnName);
checkNewColumnName(newColumnNames._1);
checkNewColumnName(newColumnNames._2);
final BiConsumer> outputToUse = (BiConsumer>) output;
final List newColumn1 = new ArrayList<>(size());
final List newColumn2 = new ArrayList<>(size());
final List column = _columnList.get(columnIndex);
final Pair tmp = new Pair<>();
for (final Object val : column) {
outputToUse.accept(val, tmp);
newColumn1.add(tmp.left);
newColumn2.add(tmp.right);
}
_columnNameList.remove(columnIndex);
_columnNameList.addAll(columnIndex, Arrays.asList(newColumnNames._1, newColumnNames._2));
_columnList.remove(columnIndex);
_columnList.addAll(columnIndex, Arrays.asList(newColumn1, newColumn2));
_columnIndexMap = null;
_columnIndexes = null;
modCount++;
}
@Override
public void divideColumn(final String columnName, final Tuple3 newColumnNames,
final BiConsumer, Triple> output) {
checkFrozen();
final int columnIndex = checkColumnName(columnName);
checkNewColumnName(newColumnNames._1);
checkNewColumnName(newColumnNames._2);
checkNewColumnName(newColumnNames._3);
final BiConsumer> outputToUse = (BiConsumer>) output;
final List newColumn1 = new ArrayList<>(size());
final List newColumn2 = new ArrayList<>(size());
final List newColumn3 = new ArrayList<>(size());
final List column = _columnList.get(columnIndex);
final Triple tmp = new Triple<>();
for (final Object val : column) {
outputToUse.accept(val, tmp);
newColumn1.add(tmp.left);
newColumn2.add(tmp.middle);
newColumn3.add(tmp.right);
}
_columnNameList.remove(columnIndex);
_columnNameList.addAll(columnIndex, Arrays.asList(newColumnNames._1, newColumnNames._2, newColumnNames._3));
_columnList.remove(columnIndex);
_columnList.addAll(columnIndex, Arrays.asList(newColumn1, newColumn2, newColumn3));
_columnIndexMap = null;
_columnIndexes = null;
modCount++;
}
@Override
public void addRow(final Object row) {
addRow(size(), row);
}
@Override
public void addRow(final int newRowPosition, final Object row) {
checkFrozen();
if ((newRowPosition < 0) || (newRowPosition > size())) {
throw new IllegalArgumentException("Invalid row index: " + newRowPosition + ". It must be >= 0 and <= " + size());
}
final Class> rowClass = row.getClass();
final Type> rowType = N.typeOf(rowClass);
if (rowType.isObjectArray()) {
final Object[] a = (Object[]) row;
if (a.length < columnCount()) {
throw new IllegalArgumentException("The size of array (" + a.length + ") is less than the size of column (" + columnCount() + ")");
}
if (newRowPosition == size()) {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(a[i]);
}
} else {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(newRowPosition, a[i]);
}
}
} else if (rowType.isCollection()) {
final Collection c = (Collection) row;
if (c.size() < columnCount()) {
throw new IllegalArgumentException("The size of collection (" + c.size() + ") is less than the size of column (" + columnCount() + ")");
}
final Iterator it = c.iterator();
if (newRowPosition == size()) {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(it.next());
}
} else {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(newRowPosition, it.next());
}
}
} else if (rowType.isMap()) {
final Map map = (Map) row;
final Object[] a = new Object[columnCount()];
int idx = 0;
for (final String columnName : _columnNameList) {
a[idx] = map.get(columnName);
if (a[idx] == null && !map.containsKey(columnName)) {
throw new IllegalArgumentException("Column (" + columnName + ") is not found in map (" + map.keySet() + ")");
}
idx++;
}
if (newRowPosition == size()) {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(a[i]);
}
} else {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(newRowPosition, a[i]);
}
}
} else if (rowType.isBean()) {
final BeanInfo beanInfo = ParserUtil.getBeanInfo(rowClass);
final Object[] a = new Object[columnCount()];
PropInfo propInfo = null;
int idx = 0;
for (final String columnName : _columnNameList) {
propInfo = beanInfo.getPropInfo(columnName);
if (propInfo == null) {
throw new IllegalArgumentException("Column (" + columnName + ") is not found in bean (" + rowClass + ")");
}
a[idx++] = propInfo.getPropValue(row);
}
if (newRowPosition == size()) {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(a[i]);
}
} else {
for (int i = 0, len = columnCount(); i < len; i++) {
_columnList.get(i).add(newRowPosition, a[i]);
}
}
} else {
throw new IllegalArgumentException(
"Unsupported row type: " + ClassUtil.getCanonicalClassName(rowClass) + ". Only Array, List/Set, Map and bean class are supported");
}
modCount++;
}
@Override
public void removeRow(final int rowIndex) {
checkFrozen();
checkRowNum(rowIndex);
for (final List objects : _columnList) {
objects.remove(rowIndex);
}
modCount++;
}
@Override
@SafeVarargs
public final void removeRows(final int... indices) {
checkFrozen();
for (final int rowIndex : indices) {
checkRowNum(rowIndex);
}
for (final List element : _columnList) {
N.deleteAllByIndices(element, indices);
}
modCount++;
}
@Override
public void removeRowRange(final int inclusiveFromRowIndex, final int exclusiveToRowIndex) {
checkFrozen();
this.checkRowIndex(inclusiveFromRowIndex, exclusiveToRowIndex);
for (final List objects : _columnList) {
objects.subList(inclusiveFromRowIndex, exclusiveToRowIndex).clear();
}
modCount++;
}
@Override
public void updateRow(final int rowIndex, final Function, ?> func) {
checkFrozen();
checkRowNum(rowIndex);
final Function funcToUse = (Function) func;
for (final List column : _columnList) {
column.set(rowIndex, funcToUse.apply(column.get(rowIndex)));
}
modCount++;
}
@Override
public void updateRows(final int[] indices, final Function, ?> func) {
checkFrozen();
for (final int rowIndex : indices) {
checkRowNum(rowIndex);
}
final Function funcToUse = (Function) func;
for (final List column : _columnList) {
for (final int rowIndex : indices) {
column.set(rowIndex, funcToUse.apply(column.get(rowIndex)));
}
}
modCount++;
}
@Override
public void updateAll(final Function, ?> func) {
checkFrozen();
final Function funcToUse = (Function) func;
final int size = size();
for (final List column : _columnList) {
for (int i = 0; i < size; i++) {
column.set(i, funcToUse.apply(column.get(i)));
}
}
modCount++;
}
@Override
public void replaceIf(final Predicate> predicate, final Object newValue) {
checkFrozen();
final Predicate predicateToUse = (Predicate) predicate;
final int size = size();
Object val = null;
for (final List column : _columnList) {
for (int i = 0; i < size; i++) {
val = column.get(i);
column.set(i, predicateToUse.test(val) ? newValue : val);
}
}
modCount++;
}
@Override
public void prepend(final DataSet other) {
checkFrozen();
checkIfColumnNamesAreSame(other, true);
final int[] columnIndexesForOther = getColumnIndexes(other.columnNameList());
for (int i = 0, len = columnIndexesForOther.length; i < len; i++) {
_columnList.get(columnIndexesForOther[i]).addAll(0, other.getColumn(i));
}
mergeProperties(other.properties());
modCount++;
}
@Override
public void append(final DataSet other) {
checkFrozen();
checkIfColumnNamesAreSame(other, true);
final int[] columnIndexesForOther = getColumnIndexes(other.columnNameList());
for (int i = 0, len = columnIndexesForOther.length; i < len; i++) {
_columnList.get(columnIndexesForOther[i]).addAll(other.getColumn(i));
}
mergeProperties(other.properties());
modCount++;
}
private void mergeProperties(final Map properties) {
if (N.notEmpty(properties)) {
if (_properties == N. emptyMap()) {
_properties = Maps.newOrderingMap(properties);
}
_properties.putAll(properties);
}
}
@Override
public int currentRowNum() {
return _currentRowNum;
}
@Override
public DataSet absolute(final int rowNum) {
checkRowNum(rowNum);
_currentRowNum = rowNum;
return this;
}
@Override
public Object[] getRow(final int rowIndex) {
return getRow(rowIndex, Object[].class);
}
@Override
public T getRow(final int rowIndex, final Class extends T> rowType) {
return getRow(rowIndex, _columnNameList, rowType);
}
@Override
public T getRow(final int rowIndex, final Collection columnNames, final Class extends T> rowType) {
return getRow(rowIndex, columnNames, rowType, null);
}
@Override
public T getRow(final int rowIndex, final IntFunction extends T> rowSupplier) {
return getRow(rowIndex, _columnNameList, rowSupplier);
}
@Override
public T getRow(final int rowIndex, final Collection columnNames, final IntFunction extends T> rowSupplier) {
return getRow(rowIndex, columnNames, null, rowSupplier);
}
private T getRow(final int rowIndex, final Collection columnNames, Class extends T> rowClass, IntFunction extends T> rowSupplier) {
checkRowNum(rowIndex);
final int[] columnIndexes = checkColumnNames(columnNames);
final int columnCount = columnIndexes.length;
rowClass = rowClass == null ? (Class) rowSupplier.apply(0).getClass() : rowClass;
final Type rowType = N.typeOf(rowClass);
final BeanInfo beanInfo = rowType.isBean() ? ParserUtil.getBeanInfo(rowClass) : null;
rowSupplier = rowSupplier == null && !rowType.isBean() ? this.createRowSupplier(rowClass, rowType) : rowSupplier;
return getRow(beanInfo, rowIndex, columnNames, columnIndexes, columnCount, null, rowClass, rowType, rowSupplier);
}
private T getRow(final BeanInfo beanInfo, final int rowIndex, final Collection columnNames, final int[] columnIndexes, final int columnCount,
final Map prefixAndFieldNameMap, final Class extends T> rowClass, final Type rowType,
final IntFunction extends T> rowSupplier) {
if (rowType.isObjectArray()) {
final Object[] result = (Object[]) rowSupplier.apply(columnCount);
for (int i = 0; i < columnCount; i++) {
result[i] = _columnList.get(columnIndexes[i]).get(rowIndex);
}
return (T) result;
} else if (rowType.isCollection()) {
final Collection result = (Collection) rowSupplier.apply(columnCount);
for (final int columnIndex : columnIndexes) {
result.add(_columnList.get(columnIndex).get(rowIndex));
}
return (T) result;
} else if (rowType.isMap()) {
final Map result = (Map) rowSupplier.apply(columnCount);
for (final int columnIndex : columnIndexes) {
result.put(_columnNameList.get(columnIndex), _columnList.get(columnIndex).get(rowIndex));
}
return (T) result;
} else if (rowType.isBean()) {
final boolean ignoreUnmatchedProperty = isColumnNameList(columnNames);
Object result = rowSupplier == null ? beanInfo.createBeanResult() : rowSupplier.apply(columnCount);
Set mergedPropNames = null;
String propName = null;
PropInfo propInfo = null;
for (int i = 0; i < columnCount; i++) {
propName = _columnNameList.get(columnIndexes[i]);
if (mergedPropNames != null && mergedPropNames.contains(propName)) {
continue;
}
propInfo = beanInfo.getPropInfo(propName);
if (propInfo != null) {
propInfo.setPropValue(result, _columnList.get(columnIndexes[i]).get(rowIndex));
} else {
final int idx = propName.indexOf(PROP_NAME_SEPARATOR);
if (idx <= 0) {
if (ignoreUnmatchedProperty) {
continue;
}
throw new IllegalArgumentException("Property " + propName + " is not found in class: " + rowClass);
}
final String realPropName = propName.substring(0, idx);
propInfo = getPropInfoByPrefix(beanInfo, realPropName, prefixAndFieldNameMap);
if (propInfo == null) {
if (ignoreUnmatchedProperty) {
continue;
} else {
throw new IllegalArgumentException("Property " + propName + " is not found in class: " + rowClass);
}
}
final Type propBeanType = propInfo.type.isCollection() ? (Type) propInfo.type.getElementType() : propInfo.type;
if (!propBeanType.isBean()) {
throw new UnsupportedOperationException("Property: " + propInfo.name + " in class: " + rowClass + " is not a bean type");
}
final Class propBeanClass = propBeanType.clazz();
final BeanInfo propBeanInfo = ParserUtil.getBeanInfo(propBeanClass);
final List newTmpColumnNameList = new ArrayList<>();
final List> newTmpColumnList = new ArrayList<>();
if (mergedPropNames == null) {
mergedPropNames = new HashSet<>();
}
String columnName = null;
String newColumnName = null;
for (int j = i; j < columnCount; j++) {
columnName = _columnNameList.get(columnIndexes[j]);
if (mergedPropNames.contains(columnName)) {
continue;
}
if (columnName.length() > idx && columnName.charAt(idx) == PROP_NAME_SEPARATOR && columnName.startsWith(realPropName)) {
newColumnName = columnName.substring(idx + 1);
newTmpColumnNameList.add(newColumnName);
newTmpColumnList.add(_columnList.get(columnIndexes[j]));
mergedPropNames.add(columnName);
}
}
final RowDataSet tmp = new RowDataSet(newTmpColumnNameList, newTmpColumnList);
final Object propValue = tmp.getRow(propBeanInfo, rowIndex, newTmpColumnNameList, tmp.checkColumnNames(newTmpColumnNameList),
newTmpColumnNameList.size(), prefixAndFieldNameMap, propBeanClass, propBeanType, null);
if (propInfo.type.isCollection()) {
@SuppressWarnings("rawtypes")
final Collection c = N.newCollection((Class) propInfo.clazz);
c.add(propValue);
propInfo.setPropValue(result, c);
} else {
propInfo.setPropValue(result, propValue);
}
}
}
if (rowSupplier == null) {
result = beanInfo.finishBeanResult(result);
}
return (T) result;
} else {
throw new IllegalArgumentException(
"Unsupported row type: " + rowType.clazz().getCanonicalName() + ". Only Array, Collection, Map and bean class are supported");
}
}
@Override
public Optional firstRow() {
return firstRow(Object[].class);
}
@Override
public Optional firstRow(final Class extends T> rowType) {
return firstRow(_columnNameList, rowType);
}
@Override
public Optional firstRow(final Collection columnNames, final Class extends T> rowType) {
return size() == 0 ? (Optional) Optional.empty() : Optional.of(getRow(0, columnNames, rowType));
}
@Override
public Optional firstRow(final IntFunction extends T> rowSupplier) {
return firstRow(_columnNameList, rowSupplier);
}
@Override
public Optional firstRow(final Collection columnNames, final IntFunction extends T> rowSupplier) {
if (size() == 0) {
return Optional.empty();
}
final T row = getRow(0, columnNames, rowSupplier);
return Optional.of(row);
}
@Override
public Optional lastRow() {
return lastRow(Object[].class);
}
@Override
public Optional lastRow(final Class extends T> rowType) {
return lastRow(_columnNameList, rowType);
}
@Override
public Optional lastRow(final Collection columnNames, final Class extends T> rowType) {
return size() == 0 ? (Optional) Optional.empty() : Optional.of(getRow(size() - 1, columnNames, rowType));
}
@Override
public Optional lastRow(final IntFunction extends T> rowSupplier) {
return lastRow(_columnNameList, rowSupplier);
}
@Override
public Optional lastRow(final Collection columnNames, final IntFunction extends T> rowSupplier) {
if (size() == 0) {
return Optional.empty();
}
final T row = getRow(size() - 1, columnNames, rowSupplier);
return Optional.of(row);
}
@Override
public BiIterator iterator(final String columnNameA, final String columnNameB) {
return iterator(0, size(), columnNameA, columnNameB);
}
@Override
public BiIterator iterator(final int fromRowIndex, final int toRowIndex, final String columnNameA, final String columnNameB) {
this.checkRowIndex(fromRowIndex, toRowIndex);
final List columnA = _columnList.get(checkColumnName(columnNameA));
final List columnB = _columnList.get(checkColumnName(columnNameB));
final IntObjConsumer> output = new IntObjConsumer<>() {
private final int expectedModCount = modCount;
@Override
public void accept(final int rowIndex, final Pair output) {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
output.set((A) columnA.get(rowIndex), (B) columnB.get(rowIndex));
}
};
return BiIterator.generate(fromRowIndex, toRowIndex, output);
}
@Override
public TriIterator iterator(final String columnNameA, final String columnNameB, final String columnNameC) {
return iterator(0, size(), columnNameA, columnNameB, columnNameC);
}
@Override
public TriIterator iterator(final int fromRowIndex, final int toRowIndex, final String columnNameA, final String columnNameB,
final String columnNameC) {
this.checkRowIndex(fromRowIndex, toRowIndex);
final List columnA = _columnList.get(checkColumnName(columnNameA));
final List columnB = _columnList.get(checkColumnName(columnNameB));
final List columnC = _columnList.get(checkColumnName(columnNameC));
final IntObjConsumer> output = new IntObjConsumer<>() {
private final int expectedModCount = modCount;
@Override
public void accept(final int rowIndex, final Triple output) {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
output.set((A) columnA.get(rowIndex), (B) columnB.get(rowIndex), (C) columnC.get(rowIndex));
}
};
return TriIterator.generate(fromRowIndex, toRowIndex, output);
}
@Override
public void forEach(final Throwables.Consumer super DisposableObjArray, E> action) throws E {
forEach(_columnNameList, action);
}
@Override
public void forEach(final Collection columnNames, final Throwables.Consumer super DisposableObjArray, E> action) throws E {
forEach(0, size(), columnNames, action);
}
@Override
public void forEach(final int fromRowIndex, final int toRowIndex, final Throwables.Consumer super DisposableObjArray, E> action)
throws E {
forEach(fromRowIndex, toRowIndex, _columnNameList, action);
}
@Override
public void forEach(final int fromRowIndex, final int toRowIndex, final Collection columnNames,
final Throwables.Consumer super DisposableObjArray, E> action) throws IllegalArgumentException, E {
final int[] columnIndexes = checkColumnNames(columnNames);
checkRowIndex(fromRowIndex < toRowIndex ? fromRowIndex : (toRowIndex == -1 ? 0 : toRowIndex), Math.max(fromRowIndex, toRowIndex));
N.checkArgNotNull(action);
if (size() == 0) {
return;
}
final int columnCount = columnIndexes.length;
final Object[] row = new Object[columnCount];
final DisposableObjArray disposableArray = DisposableObjArray.wrap(row);
if (fromRowIndex <= toRowIndex) {
for (int rowIndex = fromRowIndex; rowIndex < toRowIndex; rowIndex++) {
for (int i = 0; i < columnCount; i++) {
row[i] = _columnList.get(columnIndexes[i]).get(rowIndex);
}
action.accept(disposableArray);
}
} else {
for (int rowIndex = N.min(size() - 1, fromRowIndex); rowIndex > toRowIndex; rowIndex--) {
for (int i = 0; i < columnCount; i++) {
row[i] = _columnList.get(columnIndexes[i]).get(rowIndex);
}
action.accept(disposableArray);
}
}
}
@Override
public void forEach(final Tuple2 columnNames, final Throwables.BiConsumer, ?, E> action)
throws IllegalArgumentException, E {
forEach(0, size(), columnNames, action);
}
@Override
public void forEach(final int fromRowIndex, final int toRowIndex, final Tuple2 columnNames,
final Throwables.BiConsumer, ?, E> action) throws E {
final List column1 = _columnList.get(checkColumnName(columnNames._1));
final List column2 = _columnList.get(checkColumnName(columnNames._2));
checkRowIndex(fromRowIndex < toRowIndex ? fromRowIndex : (toRowIndex == -1 ? 0 : toRowIndex), Math.max(fromRowIndex, toRowIndex));
N.checkArgNotNull(action);
if (size() == 0) {
return;
}
final Throwables.BiConsumer actionToUse = (Throwables.BiConsumer) action;
if (fromRowIndex <= toRowIndex) {
for (int rowIndex = fromRowIndex; rowIndex < toRowIndex; rowIndex++) {
actionToUse.accept(column1.get(rowIndex), column2.get(rowIndex));
}
} else {
for (int rowIndex = N.min(size() - 1, fromRowIndex); rowIndex > toRowIndex; rowIndex--) {
actionToUse.accept(column1.get(rowIndex), column2.get(rowIndex));
}
}
}
@Override
public void forEach(final Tuple3 columnNames, final Throwables.TriConsumer, ?, ?, E> action) throws E {
forEach(0, size(), columnNames, action);
}
@Override
public void forEach(final int fromRowIndex, final int toRowIndex, final Tuple3 columnNames,
final Throwables.TriConsumer, ?, ?, E> action) throws IllegalArgumentException, E {
final List column1 = _columnList.get(checkColumnName(columnNames._1));
final List column2 = _columnList.get(checkColumnName(columnNames._2));
final List column3 = _columnList.get(checkColumnName(columnNames._3));
checkRowIndex(fromRowIndex < toRowIndex ? fromRowIndex : (toRowIndex == -1 ? 0 : toRowIndex), Math.max(fromRowIndex, toRowIndex));
N.checkArgNotNull(action);
if (size() == 0) {
return;
}
final Throwables.TriConsumer actionToUse = (Throwables.TriConsumer) action;
if (fromRowIndex <= toRowIndex) {
for (int rowIndex = fromRowIndex; rowIndex < toRowIndex; rowIndex++) {
actionToUse.accept(column1.get(rowIndex), column2.get(rowIndex), column3.get(rowIndex));
}
} else {
for (int rowIndex = N.min(size() - 1, fromRowIndex); rowIndex > toRowIndex; rowIndex--) {
actionToUse.accept(column1.get(rowIndex), column2.get(rowIndex), column3.get(rowIndex));
}
}
}
@Override
public List toList() {
return toList(Object[].class);
}
@Override
public List toList(final int fromRowIndex, final int toRowIndex) {
return toList(fromRowIndex, toRowIndex, Object[].class);
}
@Override
public List toList(final Class extends T> rowType) {
return toList(0, size(), rowType);
}
@Override
public List toList(final int fromRowIndex, final int toRowIndex, final Class extends T> rowType) {
return toList(fromRowIndex, toRowIndex, _columnNameList, rowType);
}
@Override
public List toList(final Collection columnNames, final Class extends T> rowType) {
return toList(0, size(), columnNames, rowType);
}
@Override
public List toList(final int fromRowIndex, final int toRowIndex, final Collection columnNames, final Class extends T> rowType) {
return toList(fromRowIndex, toRowIndex, columnNames, null, rowType, null);
}
@Override
public List toList(final IntFunction extends T> rowSupplier) {
return toList(_columnNameList, rowSupplier);
}
@Override
public List toList(final int fromRowIndex, final int toRowIndex, final IntFunction extends T> rowSupplier) {
return toList(fromRowIndex, toRowIndex, _columnNameList, rowSupplier);
}
@Override
public List toList(final Collection columnNames, final IntFunction extends T> rowSupplier) {
return toList(0, size(), columnNames, rowSupplier);
}
@Override
public List toList(final int fromRowIndex, final int toRowIndex, final Collection columnNames, final IntFunction extends T> rowSupplier) {
return toList(fromRowIndex, toRowIndex, columnNames, null, null, rowSupplier);
}
private List toList(final int fromRowIndex, final int toRowIndex, final Collection columnNames,
final Map prefixAndFieldNameMap, Class extends T> rowClass, IntFunction extends T> rowSupplier) {
checkRowIndex(fromRowIndex, toRowIndex);
final int[] columnIndexes = checkColumnNames(columnNames);
rowClass = rowClass == null ? (Class) rowSupplier.apply(0).getClass() : rowClass;
final Type> rowType = N.typeOf(rowClass);
if (rowType.isBean()) {
return toEntities(ParserUtil.getBeanInfo(rowClass), fromRowIndex, toRowIndex, null, columnNames, prefixAndFieldNameMap, false, true, rowClass,
rowSupplier);
}
rowSupplier = rowSupplier == null && !rowType.isBean() ? this.createRowSupplier(rowClass, rowType) : rowSupplier;
final int columnCount = columnIndexes.length;
final int rowCount = toRowIndex - fromRowIndex;
final List rowList = new ArrayList<>(rowCount);
if (rowType.isObjectArray()) {
Object[] row = null;
for (int rowIndex = fromRowIndex; rowIndex < toRowIndex; rowIndex++) {
//noinspection DataFlowIssue
row = (Object[]) rowSupplier.apply(columnCount);
for (int i = 0; i < columnCount; i++) {
row[i] = _columnList.get(columnIndexes[i]).get(rowIndex);
}
rowList.add(row);
}
} else if (rowType.isCollection()) {
Collection