From ed2b70b976d0a4cf2c6739c41cb59ac3e2171b72 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Wed, 3 Dec 2008 05:07:31 +0000 Subject: [PATCH] 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 --- .../poi/hssf/usermodel/HSSFEvaluationCell.java | 13 +++++-------- .../apache/poi/ss/formula/EvaluationCell.java | 11 +++++++---- .../apache/poi/ss/formula/FormulaCellCache.java | 12 ++++++------ .../poi/xssf/usermodel/XSSFEvaluationCell.java | 17 +++++++---------- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java index 97b581724..66069f8cb 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java @@ -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() { diff --git a/src/java/org/apache/poi/ss/formula/EvaluationCell.java b/src/java/org/apache/poi/ss/formula/EvaluationCell.java index b1f467c5c..e22593406 100644 --- a/src/java/org/apache/poi/ss/formula/EvaluationCell.java +++ b/src/java/org/apache/poi/ss/formula/EvaluationCell.java @@ -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.
* - * Implementors of this class must implement {@link Object#hashCode()} and {@link Object#equals(Object)} - * to provide an identity relationship based on the underlying HSSF or XSSF cell

- * * 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(); diff --git a/src/java/org/apache/poi/ss/formula/FormulaCellCache.java b/src/java/org/apache/poi/ss/formula/FormulaCellCache.java index 4a1fce6b8..81cd4d468 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaCellCache.java +++ b/src/java/org/apache/poi/ss/formula/FormulaCellCache.java @@ -31,11 +31,11 @@ final class FormulaCellCache { void processEntry(FormulaCellCacheEntry entry); } - private Map _formulaEntriesByCell; + private final Map _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(); } public CellCacheEntry[] getCacheEntries() { @@ -53,15 +53,15 @@ final class FormulaCellCache { * @return null 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) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java index 0f78ef6a4..127e4da86 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java @@ -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() {