From dc40d3599c8ca0145202ce9843f5a795941add9d Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 19 Oct 2013 13:53:19 +0000 Subject: [PATCH] Patch 55611 - Performance improvement in DateUtil.isADateFormat(int, String) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1533764 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/ExtendedFormatRecord.java | 6 +++- .../org/apache/poi/ss/usermodel/DateUtil.java | 29 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java index cbf520c86f..261cfd8786 100644 --- a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java +++ b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -1861,6 +1860,11 @@ public final class ExtendedFormatRecord } return false; } + + public int[] stateSummary() { + return new int[] { field_1_font_index, field_2_format_index, field_3_cell_options, field_4_alignment_options, + field_5_indention_options, field_6_border_options, field_7_palette_options, field_8_adtl_palette_options, field_9_fill_palette_options }; + } } diff --git a/src/java/org/apache/poi/ss/usermodel/DateUtil.java b/src/java/org/apache/poi/ss/usermodel/DateUtil.java index 2a32e47210..49b5ad12d8 100644 --- a/src/java/org/apache/poi/ss/usermodel/DateUtil.java +++ b/src/java/org/apache/poi/ss/usermodel/DateUtil.java @@ -277,6 +277,14 @@ public class DateUtil { } + // variables for performance optimization: + // avoid re-checking DataUtil.isADateFormat(int, String) if a given format + // string represents a date format if the same string is passed multiple times. + // see https://issues.apache.org/bugzilla/show_bug.cgi?id=55611 + private static int lastFormatIndex = -1; + private static String lastFormatString = null; + private static boolean cached = false; + /** * Given a format ID and its format String, will check to see if the * format represents a date format or not. @@ -290,14 +298,25 @@ public class DateUtil { * @param formatString The format string, eg from FormatRecord.getFormatString * @see #isInternalDateFormat(int) */ + public static boolean isADateFormat(int formatIndex, String formatString) { + + if (formatString != null && formatIndex == lastFormatIndex && formatString.equals(lastFormatString)) { + return cached; + } // First up, is this an internal date format? if(isInternalDateFormat(formatIndex)) { + lastFormatIndex = formatIndex; + lastFormatString = formatString; + cached = true; return true; } // If we didn't get a real string, it can't be if(formatString == null || formatString.length() == 0) { + lastFormatIndex = formatIndex; + lastFormatString = formatString; + cached = false; return false; } @@ -349,6 +368,9 @@ public class DateUtil { // short-circuit if it indicates elapsed time: [h], [m] or [s] if(date_ptrn4.matcher(fs).matches()){ + lastFormatIndex = formatIndex; + lastFormatString = formatString; + cached = true; return true; } @@ -374,7 +396,12 @@ public class DateUtil { // If we get here, check it's only made up, in any case, of: // y m d h s - \ / , . : [ ] // optionally followed by AM/PM - return date_ptrn3b.matcher(fs).matches(); + + boolean result = date_ptrn3b.matcher(fs).matches(); + lastFormatIndex = formatIndex; + lastFormatString = formatString; + cached = result; + return result; } /** -- 2.39.5