_evalSheet = evalSheet;
}
public HSSFEvaluationCell(HSSFCell cell) {
- _cell = cell;
- _evalSheet = new HSSFEvaluationSheet(cell.getSheet());
- }
- // Note - hashCode and equals defined according to underlying cell
- public int hashCode() {
- return _cell.hashCode();
+ this(cell, new HSSFEvaluationSheet(cell.getSheet()));
}
- 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() {
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();
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() {
* @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) {
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() {