modified EvaluationCell to make Evaluation API more easily wrapable.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@722778 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-12-03 05:07:31 +00:00
parent ba3de4c4d2
commit ed2b70b976
4 changed files with 25 additions and 28 deletions

View File

@ -34,15 +34,12 @@ final class HSSFEvaluationCell implements EvaluationCell {
_evalSheet = evalSheet;
}
public HSSFEvaluationCell(HSSFCell cell) {
_cell = cell;
_evalSheet = new HSSFEvaluationSheet(cell.getSheet());
this(cell, new HSSFEvaluationSheet(cell.getSheet()));
}
// Note - hashCode and equals defined according to underlying cell
public int hashCode() {
return _cell.hashCode();
}
public boolean equals(Object obj) {
return _cell == ((HSSFEvaluationCell)obj)._cell;
public Object getIdentityKey() {
// save memory by just using the cell itself as the identity key
// Note - this assumes HSSFCell has not overridden hashCode and equals
return _cell;
}
public HSSFCell getHSSFCell() {

View File

@ -17,19 +17,22 @@
package org.apache.poi.ss.formula;
import java.util.HashMap;
/**
* Abstracts a cell for the purpose of formula evaluation. This interface represents both formula
* and non-formula cells.<br/>
*
* Implementors of this class must implement {@link Object#hashCode()} and {@link Object#equals(Object)}
* to provide an <em>identity</em> relationship based on the underlying HSSF or XSSF cell <p/>
*
* For POI internal use only
*
* @author Josh Micich
*/
public interface EvaluationCell {
// consider method Object getUnderlyingCell() to reduce memory consumption in formula cell cache
/**
* @return an Object that identifies the underlying cell, suitable for use as a key in a {@link HashMap}
*/
Object getIdentityKey();
EvaluationSheet getSheet();
int getRowIndex();
int getColumnIndex();

View File

@ -31,11 +31,11 @@ final class FormulaCellCache {
void processEntry(FormulaCellCacheEntry entry);
}
private Map _formulaEntriesByCell;
private final Map<Object, FormulaCellCacheEntry> _formulaEntriesByCell;
public FormulaCellCache() {
// assumes HSSFCell does not override hashCode or equals, otherwise we need IdentityHashMap
_formulaEntriesByCell = new HashMap();
// assumes the object returned by EvaluationCell.getIdentityKey() has a well behaved hashCode+equals
_formulaEntriesByCell = new HashMap<Object, FormulaCellCacheEntry>();
}
public CellCacheEntry[] getCacheEntries() {
@ -53,15 +53,15 @@ final class FormulaCellCache {
* @return <code>null</code> if not found
*/
public FormulaCellCacheEntry get(EvaluationCell cell) {
return (FormulaCellCacheEntry) _formulaEntriesByCell.get(cell);
return _formulaEntriesByCell.get(cell.getIdentityKey());
}
public void put(EvaluationCell cell, FormulaCellCacheEntry entry) {
_formulaEntriesByCell.put(cell, entry);
_formulaEntriesByCell.put(cell.getIdentityKey(), entry);
}
public FormulaCellCacheEntry remove(EvaluationCell cell) {
return (FormulaCellCacheEntry) _formulaEntriesByCell.remove(cell);
return _formulaEntriesByCell.remove(cell.getIdentityKey());
}
public void applyOperation(IEntryOperation operation) {

View File

@ -30,22 +30,19 @@ final class XSSFEvaluationCell implements EvaluationCell {
private final EvaluationSheet _evalSheet;
private final XSSFCell _cell;
public XSSFEvaluationCell(XSSFCell cell) {
_cell = cell;
_evalSheet = new XSSFEvaluationSheet(cell.getSheet());
}
public XSSFEvaluationCell(XSSFCell cell, XSSFEvaluationSheet evaluationSheet) {
_cell = cell;
_evalSheet = evaluationSheet;
}
// Note - hashCode and equals defined according to underlying cell
public int hashCode() {
return _cell.hashCode();
public XSSFEvaluationCell(XSSFCell cell) {
this(cell, new XSSFEvaluationSheet(cell.getSheet()));
}
public boolean equals(Object obj) {
return _cell == ((XSSFEvaluationCell)obj)._cell;
public Object getIdentityKey() {
// save memory by just using the cell itself as the identity key
// Note - this assumes HSSFCell has not overridden hashCode and equals
return _cell;
}
public XSSFCell getXSSFCell() {