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

cn.hutool.core.map.multi.AbsTable Maven / Gradle / Ivy

Go to download

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

There is a newer version: 5.8.34
Show newest version
package cn.hutool.core.map.multi;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.collection.TransIter;
import cn.hutool.core.util.ObjectUtil;

import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 抽象{@link Table}接口实现
* 默认实现了: *
    *
  • {@link #equals(Object)}
  • *
  • {@link #hashCode()}
  • *
  • {@link #toString()}
  • *
  • {@link #values()}
  • *
  • {@link #cellSet()}
  • *
  • {@link #iterator()}
  • *
* * @param 行类型 * @param 列类型 * @param 值类型 * @author Guava, Looly * @since 5.7.23 */ public abstract class AbsTable implements Table { @Override public boolean equals(Object obj) { if (obj == this) { return true; } else if (obj instanceof Table) { final Table that = (Table) obj; return this.cellSet().equals(that.cellSet()); } else { return false; } } @Override public int hashCode() { return cellSet().hashCode(); } @Override public String toString() { return rowMap().toString(); } //region values @Override public Collection values() { Collection result = values; return (result == null) ? values = new Values() : result; } private Collection values; private class Values extends AbstractCollection { @Override public Iterator iterator() { return new TransIter<>(cellSet().iterator(), Cell::getValue); } @Override public boolean contains(Object o) { //noinspection unchecked return containsValue((V) o); } @Override public void clear() { AbsTable.this.clear(); } @Override public int size() { return AbsTable.this.size(); } } //endregion //region cellSet @Override public Set> cellSet() { Set> result = cellSet; return (result == null) ? cellSet = new CellSet() : result; } private Set> cellSet; private class CellSet extends AbstractSet> { @Override public boolean contains(Object o) { if (o instanceof Cell) { @SuppressWarnings("unchecked") final Cell cell = (Cell) o; Map row = getRow(cell.getRowKey()); if (null != row) { return ObjectUtil.equals(row.get(cell.getColumnKey()), cell.getValue()); } } return false; } @Override public boolean remove(Object o) { if (contains(o)) { @SuppressWarnings("unchecked") final Cell cell = (Cell) o; AbsTable.this.remove(cell.getRowKey(), cell.getColumnKey()); } return false; } @Override public void clear() { AbsTable.this.clear(); } @Override public Iterator> iterator() { return new AbsTable.CellIterator(); } @Override public int size() { return AbsTable.this.size(); } } //endregion //region iterator @Override public Iterator> iterator() { return new CellIterator(); } /** * 基于{@link Cell}的{@link Iterator}实现 */ private class CellIterator implements Iterator> { final Iterator>> rowIterator = rowMap().entrySet().iterator(); Map.Entry> rowEntry; Iterator> columnIterator = IterUtil.empty(); @Override public boolean hasNext() { return rowIterator.hasNext() || columnIterator.hasNext(); } @Override public Cell next() { if (false == columnIterator.hasNext()) { rowEntry = rowIterator.next(); columnIterator = rowEntry.getValue().entrySet().iterator(); } final Map.Entry columnEntry = columnIterator.next(); return new SimpleCell<>(rowEntry.getKey(), columnEntry.getKey(), columnEntry.getValue()); } @Override public void remove() { columnIterator.remove(); if (rowEntry.getValue().isEmpty()) { rowIterator.remove(); } } } //endregion /** * 简单{@link Cell} 实现 * * @param 行类型 * @param 列类型 * @param 值类型 */ private static class SimpleCell implements Cell, Serializable { private static final long serialVersionUID = 1L; private final R rowKey; private final C columnKey; private final V value; SimpleCell(R rowKey, C columnKey, V value) { this.rowKey = rowKey; this.columnKey = columnKey; this.value = value; } @Override public R getRowKey() { return rowKey; } @Override public C getColumnKey() { return columnKey; } @Override public V getValue() { return value; } @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Cell) { Cell other = (Cell) obj; return ObjectUtil.equal(rowKey, other.getRowKey()) && ObjectUtil.equal(columnKey, other.getColumnKey()) && ObjectUtil.equal(value, other.getValue()); } return false; } @Override public int hashCode() { return Objects.hash(rowKey, columnKey, value); } @Override public String toString() { return "(" + rowKey + "," + columnKey + ")=" + value; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy