cn.hutool.core.map.multi.RowKeyTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.core.map.multi;
import cn.hutool.core.builder.Builder;
import cn.hutool.core.collection.ComputeIter;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.collection.TransIter;
import cn.hutool.core.map.AbsEntry;
import cn.hutool.core.map.MapUtil;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 将行的键作为主键的{@link Table}实现
* 此结构为: 行=(列=值)
*
* @param 行类型
* @param 列类型
* @param 值类型
* @author Guava, Looly
* @since 5.7.23
*/
public class RowKeyTable extends AbsTable {
final Map> raw;
/**
* 列的Map创建器,用于定义Table中Value对应Map类型
*/
final Builder extends Map> columnBuilder;
//region 构造
/**
* 构造
*/
public RowKeyTable() {
this(new HashMap<>());
}
/**
* 构造
*
* @param isLinked 是否有序,有序则使用{@link java.util.LinkedHashMap}作为原始Map
* @since 5.8.0
*/
public RowKeyTable(boolean isLinked) {
this(MapUtil.newHashMap(isLinked), () -> MapUtil.newHashMap(isLinked));
}
/**
* 构造
*
* @param raw 原始Map
*/
public RowKeyTable(Map> raw) {
this(raw, HashMap::new);
}
/**
* 构造
*
* @param raw 原始Map
* @param columnMapBuilder 列的map创建器
*/
public RowKeyTable(Map> raw, Builder extends Map> columnMapBuilder) {
this.raw = raw;
this.columnBuilder = null == columnMapBuilder ? HashMap::new : columnMapBuilder;
}
//endregion
@Override
public Map> rowMap() {
return raw;
}
@Override
public V put(R rowKey, C columnKey, V value) {
return raw.computeIfAbsent(rowKey, (key) -> columnBuilder.build()).put(columnKey, value);
}
@Override
public V remove(R rowKey, C columnKey) {
final Map map = getRow(rowKey);
if (null == map) {
return null;
}
final V value = map.remove(columnKey);
if (map.isEmpty()) {
raw.remove(rowKey);
}
return value;
}
@Override
public boolean isEmpty() {
return raw.isEmpty();
}
@Override
public void clear() {
this.raw.clear();
}
@Override
public boolean containsColumn(C columnKey) {
if (columnKey == null) {
return false;
}
for (Map map : raw.values()) {
if (null != map && map.containsKey(columnKey)) {
return true;
}
}
return false;
}
//region columnMap
@Override
public Map> columnMap() {
Map> result = columnMap;
return (result == null) ? columnMap = new ColumnMap() : result;
}
private Map> columnMap;
private class ColumnMap extends AbstractMap> {
@Override
public Set>> entrySet() {
return new ColumnMapEntrySet();
}
}
private class ColumnMapEntrySet extends AbstractSet>> {
private final Set columnKeySet = columnKeySet();
@Override
public Iterator>> iterator() {
return new TransIter<>(columnKeySet.iterator(),
c -> MapUtil.entry(c, getColumn(c)));
}
@Override
public int size() {
return columnKeySet.size();
}
}
//endregion
//region columnKeySet
@Override
public Set columnKeySet() {
Set result = columnKeySet;
return (result == null) ? columnKeySet = new ColumnKeySet() : result;
}
private Set columnKeySet;
private class ColumnKeySet extends AbstractSet {
@Override
public Iterator iterator() {
return new ColumnKeyIterator();
}
@Override
public int size() {
return IterUtil.size(iterator());
}
}
private class ColumnKeyIterator extends ComputeIter {
final Map seen = columnBuilder.build();
final Iterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy