From 766dc98ce36338acc4ad9c21d931c0843994ee40 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 6 Feb 2008 14:35:05 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../poi/hssf/record/ExtendedFormatRecord.java | 52 +++++++++++++++++++ .../apache/poi/hssf/record/FontRecord.java | 33 ++++++++++++ .../poi/hssf/usermodel/HSSFCellStyle.java | 26 +++++++++- .../apache/poi/hssf/usermodel/HSSFFont.java | 25 ++++++++- .../poi/hssf/usermodel/TestCellStyle.java | 29 +++++++++++ 7 files changed, 165 insertions(+), 2 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index df0e5e51ad..d2430ca19e 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,6 +36,7 @@ + Implement hashCode() and equals(obj) on HSSFFont and HSSFCellStyle 44345 - Implement CountA, CountIf, Index, Rows and Columns functions 44336 - Properly escape sheet names as required when figuring out the text of formulas 44326 - Improvements to how SystemOutLogger and CommonsLogger log messages with exceptions, and avoid an infinite loop with certain log messages with exceptions diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 9a7a3f43cb..8d43546e19 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + Implement hashCode() and equals(obj) on HSSFFont and HSSFCellStyle 44345 - Implement CountA, CountIf, Index, Rows and Columns functions 44336 - Properly escape sheet names as required when figuring out the text of formulas 44326 - Improvements to how SystemOutLogger and CommonsLogger log messages with exceptions, and avoid an infinite loop with certain log messages with exceptions diff --git a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java index 221668cb49..c668d5f876 100644 --- a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java +++ b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java @@ -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; + } + + } diff --git a/src/java/org/apache/poi/hssf/record/FontRecord.java b/src/java/org/apache/poi/hssf/record/FontRecord.java index 35d365a679..42e058f472 100644 --- a/src/java/org/apache/poi/hssf/record/FontRecord.java +++ b/src/java/org/apache/poi/hssf/record/FontRecord.java @@ -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; + } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index cdea9ee5be..3c9b6b1bb5 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -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; + } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java index cfaa5e4f48..1bae5a83bc 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java @@ -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; + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java index bdf5702c64..0daa80326e 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java @@ -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

-- 2.39.5