Implement hashCode and equals for HSSFFont and HSSFCellStyle

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@619001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-02-06 14:35:05 +00:00
parent 19ad1617d1
commit 142936990c
7 changed files with 164 additions and 1 deletions

View 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>

View 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>

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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>