]> source.dussan.org Git - poi.git/commitdiff
Implement hashCode and equals for HSSFFont and HSSFCellStyle
authorNick Burch <nick@apache.org>
Wed, 6 Feb 2008 14:35:05 +0000 (14:35 +0000)
committerNick Burch <nick@apache.org>
Wed, 6 Feb 2008 14:35:05 +0000 (14:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@619001 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java
src/java/org/apache/poi/hssf/record/FontRecord.java
src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
src/java/org/apache/poi/hssf/usermodel/HSSFFont.java
src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java

index df0e5e51adb43f1269e256acfa95fec69e241617..d2430ca19ec6e0a56235859d2d507ece85a08c6d 100644 (file)
@@ -36,6 +36,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.1-beta1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="add">Implement hashCode() and equals(obj) on HSSFFont and HSSFCellStyle</action>
            <action dev="POI-DEVELOPERS" type="fix">44345 - Implement CountA, CountIf, Index, Rows and Columns functions</action>
            <action dev="POI-DEVELOPERS" type="fix">44336 - Properly escape sheet names as required when figuring out the text of formulas</action>
            <action dev="POI-DEVELOPERS" type="add">44326 - Improvements to how SystemOutLogger and CommonsLogger log messages with exceptions, and avoid an infinite loop with certain log messages with exceptions</action>
index 9a7a3f43cba804cbdc9286518442ad90b77920ad..8d43546e19f5fe0b4e498fc1d68f834aa14147c3 100644 (file)
@@ -33,6 +33,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1-beta1" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="add">Implement hashCode() and equals(obj) on HSSFFont and HSSFCellStyle</action>
            <action dev="POI-DEVELOPERS" type="fix">44345 - Implement CountA, CountIf, Index, Rows and Columns functions</action>
            <action dev="POI-DEVELOPERS" type="fix">44336 - Properly escape sheet names as required when figuring out the text of formulas</action>
            <action dev="POI-DEVELOPERS" type="add">44326 - Improvements to how SystemOutLogger and CommonsLogger log messages with exceptions, and avoid an infinite loop with certain log messages with exceptions</action>
index 221668cb491a2afb9612cc4e0cf4d40e4913a1a9..c668d5f8762ee6ac99a0e4159a887e8cd09a15c8 100644 (file)
@@ -1814,4 +1814,56 @@ public class ExtendedFormatRecord
     {
         return sid;
     }
+
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + field_1_font_index;
+               result = prime * result + field_2_format_index;
+               result = prime * result + field_3_cell_options;
+               result = prime * result + field_4_alignment_options;
+               result = prime * result + field_5_indention_options;
+               result = prime * result + field_6_border_options;
+               result = prime * result + field_7_palette_options;
+               result = prime * result + field_8_adtl_palette_options;
+               result = prime * result + field_9_fill_palette_options;
+               return result;
+       }
+
+       /**
+        * Will consider two different records with the same
+        *  contents as equals, as the various indexes
+        *  that matter are embedded in the records
+        */
+       public boolean equals(Object obj) {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (obj instanceof ExtendedFormatRecord) {
+                       final ExtendedFormatRecord other = (ExtendedFormatRecord) obj;
+                       if (field_1_font_index != other.field_1_font_index)
+                               return false;
+                       if (field_2_format_index != other.field_2_format_index)
+                               return false;
+                       if (field_3_cell_options != other.field_3_cell_options)
+                               return false;
+                       if (field_4_alignment_options != other.field_4_alignment_options)
+                               return false;
+                       if (field_5_indention_options != other.field_5_indention_options)
+                               return false;
+                       if (field_6_border_options != other.field_6_border_options)
+                               return false;
+                       if (field_7_palette_options != other.field_7_palette_options)
+                               return false;
+                       if (field_8_adtl_palette_options != other.field_8_adtl_palette_options)
+                               return false;
+                       if (field_9_fill_palette_options != other.field_9_fill_palette_options)
+                               return false;
+                       return true;
+               }
+               return false;
+       }
+    
+    
 }
index 35d365a679bad8017d10fbf30e2f31bdd3f6ccee..42e058f4724147f2faaaf15bc6dfdb6aa2e7c4f6 100644 (file)
@@ -538,4 +538,37 @@ public class FontRecord
     {
         return sid;
     }
+
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime
+                               * result
+                               + ((field_11_font_name == null) ? 0 : field_11_font_name
+                                               .hashCode());
+               result = prime * result + field_1_font_height;
+               result = prime * result + field_2_attributes;
+               result = prime * result + field_3_color_palette_index;
+               result = prime * result + field_4_bold_weight;
+               result = prime * result + field_5_super_sub_script;
+               result = prime * result + field_6_underline;
+               result = prime * result + field_7_family;
+               result = prime * result + field_8_charset;
+               result = prime * result + field_9_zero;
+               result = prime * result + field_10_font_name_len;
+               return result;
+       }
+
+       /**
+        * Only returns two for the same exact object -
+        *  creating a second FontRecord with the same
+        *  properties won't be considered equal, as 
+        *  the record's position in the record stream
+        *  matters.
+        */
+       public boolean equals(Object obj) {
+               if (this == obj)
+                       return true;
+               return false;
+       }
 }
index cdea9ee5be4f8d4564b5f7628e70953e9ec75a38..3c9b6b1bb5b6d4ba0530d6eacbdebc5aabda4b31 100644 (file)
@@ -20,7 +20,6 @@ package org.apache.poi.hssf.usermodel;
 
 import org.apache.poi.hssf.model.Workbook;
 import org.apache.poi.hssf.record.ExtendedFormatRecord;
-import org.apache.poi.hssf.record.FormatRecord;
 import org.apache.poi.hssf.util.*;
 
 /**
@@ -913,4 +912,29 @@ public class HSSFCellStyle
     {
         return format.getFillForeground();
     }
+
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((format == null) ? 0 : format.hashCode());
+               result = prime * result + index;
+               return result;
+       }
+
+       public boolean equals(Object obj) {
+               if (this == obj) return true;
+               if (obj == null) return false;
+               if (obj instanceof HSSFCellStyle) {
+                       final HSSFCellStyle other = (HSSFCellStyle) obj;
+                       if (format == null) {
+                               if (other.format != null)
+                                       return false;
+                       } else if (!format.equals(other.format))
+                               return false;
+                       if (index != other.index)
+                               return false;
+                       return true;
+               }
+               return false;
+       }
 }
index cfaa5e4f4827855dc5476c9d031460565834d808..1bae5a83bc9bb8c36c695f38e4c4519dbfdd2543 100644 (file)
@@ -399,5 +399,28 @@ public class HSSFFont
                 "}";
     }
 
-
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((font == null) ? 0 : font.hashCode());
+               result = prime * result + index;
+               return result;
+       }
+
+       public boolean equals(Object obj) {
+               if (this == obj) return true;
+               if (obj == null) return false;
+               if (obj instanceof HSSFFont) {
+                       final HSSFFont other = (HSSFFont) obj;
+                       if (font == null) {
+                               if (other.font != null)
+                                       return false;
+                       } else if (!font.equals(other.font))
+                               return false;
+                       if (index != other.index)
+                               return false;
+                       return true;
+               }
+               return false;
+       }
 }
index bdf5702c648f869bf184839f32812f1ec12e0a6a..0daa80326ecf7a33bb39e61a8f33e171d9aceede 100644 (file)
@@ -137,6 +137,35 @@ public class TestCellStyle
         assertEquals("FIRST ROW ", 0, s.getFirstRowNum());
 
     }
+    
+    public void testHashEquals() {
+        HSSFWorkbook     wb   = new HSSFWorkbook();
+        HSSFSheet        s    = wb.createSheet();
+        HSSFCellStyle    cs1  = wb.createCellStyle();
+        HSSFCellStyle    cs2  = wb.createCellStyle();
+        HSSFRow row = s.createRow((short)0);
+        HSSFCell cell1 = row.createCell((short)1);
+        HSSFCell cell2 = row.createCell((short)2);
+        
+        cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
+        cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy"));
+        
+        cell1.setCellStyle(cs1);
+        cell1.setCellValue(new Date());
+        
+        cell2.setCellStyle(cs2);
+        cell2.setCellValue(new Date());
+        
+        assertEquals(cs1.hashCode(), cs1.hashCode());
+        assertEquals(cs2.hashCode(), cs2.hashCode());
+        assertTrue(cs1.equals(cs1));
+        assertTrue(cs2.equals(cs2));
+        
+        // Change cs1, hash will alter
+        int hash1 = cs1.hashCode();
+        cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/dd/yy"));
+        assertFalse(hash1 == cs1.hashCode());
+    }
 
     /**
      * TEST NAME:  Test Write Sheet Style <P>