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; _evalSheet = evalSheet;
} }
public HSSFEvaluationCell(HSSFCell cell) { public HSSFEvaluationCell(HSSFCell cell) {
_cell = cell; this(cell, new HSSFEvaluationSheet(cell.getSheet()));
_evalSheet = new HSSFEvaluationSheet(cell.getSheet());
} }
// Note - hashCode and equals defined according to underlying cell public Object getIdentityKey() {
public int hashCode() { // save memory by just using the cell itself as the identity key
return _cell.hashCode(); // Note - this assumes HSSFCell has not overridden hashCode and equals
} return _cell;
public boolean equals(Object obj) {
return _cell == ((HSSFEvaluationCell)obj)._cell;
} }
public HSSFCell getHSSFCell() { public HSSFCell getHSSFCell() {

View File

@ -17,19 +17,22 @@
package org.apache.poi.ss.formula; package org.apache.poi.ss.formula;
import java.util.HashMap;
/** /**
* Abstracts a cell for the purpose of formula evaluation. This interface represents both formula * Abstracts a cell for the purpose of formula evaluation. This interface represents both formula
* and non-formula cells.<br/> * 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 * For POI internal use only
* *
* @author Josh Micich * @author Josh Micich
*/ */
public interface EvaluationCell { 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(); EvaluationSheet getSheet();
int getRowIndex(); int getRowIndex();
int getColumnIndex(); int getColumnIndex();

View File

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

View File

@ -30,22 +30,19 @@ final class XSSFEvaluationCell implements EvaluationCell {
private final EvaluationSheet _evalSheet; private final EvaluationSheet _evalSheet;
private final XSSFCell _cell; private final XSSFCell _cell;
public XSSFEvaluationCell(XSSFCell cell) {
_cell = cell;
_evalSheet = new XSSFEvaluationSheet(cell.getSheet());
}
public XSSFEvaluationCell(XSSFCell cell, XSSFEvaluationSheet evaluationSheet) { public XSSFEvaluationCell(XSSFCell cell, XSSFEvaluationSheet evaluationSheet) {
_cell = cell; _cell = cell;
_evalSheet = evaluationSheet; _evalSheet = evaluationSheet;
} }
// Note - hashCode and equals defined according to underlying cell public XSSFEvaluationCell(XSSFCell cell) {
public int hashCode() { this(cell, new XSSFEvaluationSheet(cell.getSheet()));
return _cell.hashCode();
} }
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() { public XSSFCell getXSSFCell() {