]> source.dussan.org Git - poi.git/commitdiff
modified EvaluationCell to make Evaluation API more easily wrapable.
authorJosh Micich <josh@apache.org>
Wed, 3 Dec 2008 05:07:31 +0000 (05:07 +0000)
committerJosh Micich <josh@apache.org>
Wed, 3 Dec 2008 05:07:31 +0000 (05:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@722778 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java
src/java/org/apache/poi/ss/formula/EvaluationCell.java
src/java/org/apache/poi/ss/formula/FormulaCellCache.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java

index 97b581724a75ce8c05f3bb25ca658bae53aae7c8..66069f8cbcdd302878289f0a0eece8355d46aeeb 100644 (file)
@@ -34,15 +34,12 @@ final class HSSFEvaluationCell implements EvaluationCell {
                _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() {
index b1f467c5c146081940c0929ac85f0d37d739aeae..e225934061f3d22c5fda946170183a5f4fb095cb 100644 (file)
 
 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();
index 4a1fce6b80275f941f45ec742a7a834ac551894a..81cd4d46897a92e0860a849268cfced04327d9a2 100644 (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) {
index 0f78ef6a4557d6b6f7a9e3b76ce101e38ceed530..127e4da86aa21b1af3e7374316f8d0d81b938f62 100644 (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() {