ICU-6503 save date time pattern generator as private member, minimize data time pattern generator instantiation times

X-SVN-Rev: 24573
This commit is contained in:
Xiaomei Ji 2008-09-15 21:20:19 +00:00
parent b7c81e69de
commit ff17b981a5
5 changed files with 63 additions and 66 deletions

View File

@ -279,26 +279,14 @@ DateFormat::createInstance()
DateFormat* U_EXPORT2
DateFormat::createPatternInstance(const UnicodeString& skeleton,
const Locale& locale,
DateTimePatternGenerator* dtpng,
UErrorCode& status)
{
if ( U_FAILURE(status) ) {
return NULL;
}
DateTimePatternGenerator* dtptg =
DateTimePatternGenerator::createInstance(locale, status);
if ( dtptg == NULL ) {
status = U_MEMORY_ALLOCATION_ERROR;
delete dtptg;
return NULL;
}
if ( U_FAILURE(status) ) {
delete dtptg;
return NULL;
}
const UnicodeString pattern = dtptg->getBestPattern(skeleton, status);
delete dtptg;
const UnicodeString pattern = dtpng->getBestPattern(skeleton, status);
if ( U_FAILURE(status) ) {
return NULL;
}

View File

@ -53,10 +53,10 @@ static const char gDateTimePatternsTag[]="DateTimePatterns";
// latestFirst:
static const UChar gLaterFirstPrefix[] = {LOW_L, LOW_A, LOW_T, LOW_E, LOW_S,LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON, 0};
static const UChar gLaterFirstPrefix[] = {LOW_L, LOW_A, LOW_T, LOW_E, LOW_S,LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON};
// earliestFirst:
static const UChar gEarlierFirstPrefix[] = {LOW_E, LOW_A, LOW_R, LOW_L, LOW_I, LOW_E, LOW_S, LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON, 0};
static const UChar gEarlierFirstPrefix[] = {LOW_E, LOW_A, LOW_R, LOW_L, LOW_I, LOW_E, LOW_S, LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON};
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateIntervalFormat)
@ -74,8 +74,6 @@ DateIntervalFormat* U_EXPORT2
DateIntervalFormat::createInstance(const UnicodeString& skeleton,
const Locale& locale,
UErrorCode& status) {
DateFormat* dtfmt = DateFormat::createPatternInstance(skeleton, locale, status);
#ifdef DTITVFMT_DEBUG
char result[1000];
char result_1[1000];
@ -89,7 +87,7 @@ DateIntervalFormat::createInstance(const UnicodeString& skeleton,
#endif
DateIntervalInfo* dtitvinf = new DateIntervalInfo(locale, status);
return create(dtfmt, dtitvinf, &skeleton, status);
return create(locale, dtitvinf, &skeleton, status);
}
@ -107,9 +105,8 @@ DateIntervalFormat::createInstance(const UnicodeString& skeleton,
const Locale& locale,
const DateIntervalInfo& dtitvinf,
UErrorCode& status) {
DateFormat* dtfmt = DateFormat::createPatternInstance(skeleton, locale, status);
DateIntervalInfo* ptn = dtitvinf.clone();
return create(dtfmt, ptn, &skeleton, status);
return create(locale, ptn, &skeleton, status);
}
@ -117,7 +114,8 @@ DateIntervalFormat::DateIntervalFormat()
: fInfo(NULL),
fDateFormat(NULL),
fFromCalendar(NULL),
fToCalendar(NULL)
fToCalendar(NULL),
fDtpng(NULL)
{}
@ -126,7 +124,8 @@ DateIntervalFormat::DateIntervalFormat(const DateIntervalFormat& itvfmt)
fInfo(NULL),
fDateFormat(NULL),
fFromCalendar(NULL),
fToCalendar(NULL) {
fToCalendar(NULL),
fDtpng(NULL) {
*this = itvfmt;
}
@ -138,6 +137,7 @@ DateIntervalFormat::operator=(const DateIntervalFormat& itvfmt) {
delete fInfo;
delete fFromCalendar;
delete fToCalendar;
delete fDtpng;
if ( itvfmt.fDateFormat ) {
fDateFormat = (SimpleDateFormat*)itvfmt.fDateFormat->clone();
} else {
@ -163,6 +163,9 @@ DateIntervalFormat::operator=(const DateIntervalFormat& itvfmt) {
for ( i = 0; i< DateIntervalInfo::kIPI_MAX_INDEX; ++i ) {
fIntervalPatterns[i] = itvfmt.fIntervalPatterns[i];
}
if (itvfmt.fDtpng) {
fDtpng = itvfmt.fDtpng->clone();
}
}
return *this;
}
@ -173,6 +176,7 @@ DateIntervalFormat::~DateIntervalFormat() {
delete fDateFormat;
delete fFromCalendar;
delete fToCalendar;
delete fDtpng;
}
@ -207,7 +211,9 @@ DateIntervalFormat::operator==(const Format& other) const {
fFromCalendar->isEquivalentTo(*fmt->fFromCalendar) &&
fToCalendar &&
fToCalendar->isEquivalentTo(*fmt->fToCalendar) &&
fSkeleton == fmt->fSkeleton );
fSkeleton == fmt->fSkeleton &&
fDtpng &&
(*fDtpng == *fmt->fDtpng) );
int8_t i;
for (i = 0; i< DateIntervalInfo::kIPI_MAX_INDEX && res == TRUE; ++i ) {
res = ( fIntervalPatterns[i].firstPart ==
@ -408,25 +414,35 @@ DateIntervalFormat::getDateFormat() const {
}
DateIntervalFormat::DateIntervalFormat(DateFormat* dtfmt,
DateIntervalFormat::DateIntervalFormat(const Locale& locale,
DateIntervalInfo* dtItvInfo,
const UnicodeString* skeleton,
UErrorCode& status)
: fInfo(0),
fDateFormat(0),
fFromCalendar(0),
fToCalendar(0)
: fInfo(NULL),
fDateFormat(NULL),
fFromCalendar(NULL),
fToCalendar(NULL),
fDtpng(NULL)
{
if ( U_FAILURE(status) ) {
delete dtfmt;
delete dtItvInfo;
return;
}
if ( dtfmt == NULL || dtItvInfo == NULL ) {
fDtpng = DateTimePatternGenerator::createInstance(locale, status);
DateFormat* dtfmt = DateFormat::createPatternInstance(*skeleton, locale,
fDtpng, status);
if ( U_FAILURE(status) ) {
delete dtItvInfo;
delete fDtpng;
delete dtfmt;
return;
}
if ( dtfmt == NULL || dtItvInfo == NULL || fDtpng == NULL ) {
status = U_MEMORY_ALLOCATION_ERROR;
// safe to delete NULL
delete dtfmt;
delete dtItvInfo;
delete fDtpng;
return;
}
if ( skeleton ) {
@ -447,15 +463,14 @@ DateIntervalFormat::DateIntervalFormat(DateFormat* dtfmt,
DateIntervalFormat* U_EXPORT2
DateIntervalFormat::create(DateFormat* dtfmt,
DateIntervalFormat::create(const Locale& locale,
DateIntervalInfo* dtitvinf,
const UnicodeString* skeleton,
UErrorCode& status) {
DateIntervalFormat* f = new DateIntervalFormat(dtfmt, dtitvinf,
DateIntervalFormat* f = new DateIntervalFormat(locale, dtitvinf,
skeleton, status);
if ( f == NULL ) {
status = U_MEMORY_ALLOCATION_ERROR;
delete dtfmt;
delete dtitvinf;
} else if ( U_FAILURE(status) ) {
// safe to delete f, although nothing acutally is saved
@ -507,11 +522,6 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
return;
}
const Locale& locale = fDateFormat->getSmpFmtLocale();
DateTimePatternGenerator* dtpng = DateTimePatternGenerator::createInstance(locale, status);
if ( U_FAILURE(status) ) {
delete dtpng;
return;
}
if ( fSkeleton.isEmpty() ) {
UnicodeString fullPattern;
fDateFormat->toPattern(fullPattern);
@ -525,9 +535,8 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
#endif
// fSkeleton is already set by createDateIntervalInstance()
// or by createInstance(UnicodeString skeleton, .... )
fSkeleton = dtpng->getSkeleton(fullPattern, status);
fSkeleton = fDtpng->getSkeleton(fullPattern, status);
if ( U_FAILURE(status) ) {
delete dtpng;
return;
}
}
@ -581,9 +590,8 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
if ( dateSkeleton.length() == 0 ) {
// prefix with yMd
timeSkeleton.insert(0, gDateFormatSkeleton[DateFormat::kShort]);
UnicodeString pattern =dtpng->getBestPattern(timeSkeleton, status);
UnicodeString pattern = fDtpng->getBestPattern(timeSkeleton, status);
if ( U_FAILURE(status) ) {
delete dtpng;
return;
}
// for fall back interval patterns,
@ -599,7 +607,6 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
} else {
// TODO: fall back
}
delete dtpng;
return;
} // end of skeleton not found
// interval patterns for skeleton are found in resource
@ -608,9 +615,8 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
} else if ( dateSkeleton.length() == 0 ) {
// prefix with yMd
timeSkeleton.insert(0, gDateFormatSkeleton[DateFormat::kShort]);
UnicodeString pattern =dtpng->getBestPattern(timeSkeleton, status);
UnicodeString pattern = fDtpng->getBestPattern(timeSkeleton, status);
if ( U_FAILURE(status) ) {
delete dtpng;
return;
}
// for fall back interval patterns,
@ -636,17 +642,17 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
if ( !fieldExistsInSkeleton(UCAL_DATE, dateSkeleton) ) {
// prefix skeleton with 'd'
skeleton.insert(0, LOW_D);
setFallbackPattern(UCAL_DATE, skeleton, dtpng, status);
setFallbackPattern(UCAL_DATE, skeleton, status);
}
if ( !fieldExistsInSkeleton(UCAL_MONTH, dateSkeleton) ) {
// then prefix skeleton with 'M'
skeleton.insert(0, CAP_M);
setFallbackPattern(UCAL_MONTH, skeleton, dtpng, status);
setFallbackPattern(UCAL_MONTH, skeleton, status);
}
if ( !fieldExistsInSkeleton(UCAL_YEAR, dateSkeleton) ) {
// then prefix skeleton with 'y'
skeleton.insert(0, LOW_Y);
setFallbackPattern(UCAL_YEAR, skeleton, dtpng, status);
setFallbackPattern(UCAL_YEAR, skeleton, status);
}
/*
@ -661,13 +667,11 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
if ( U_FAILURE(status) ) {
delete calData;
delete dtpng;
return;
}
if ( calData == NULL ) {
status = U_MEMORY_ALLOCATION_ERROR;
delete dtpng;
return;
}
@ -679,11 +683,10 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
(int32_t)DateFormat::kDateTime,
&dateTimeFormatLength, &status);
if ( U_FAILURE(status) ) {
delete dtpng;
return;
}
UnicodeString datePattern = dtpng->getBestPattern(dateSkeleton, status);
UnicodeString datePattern = fDtpng->getBestPattern(dateSkeleton, status);
concatSingleDate2TimeInterval(dateTimeFormat, dateTimeFormatLength,
datePattern, UCAL_AM_PM, status);
@ -693,7 +696,6 @@ DateIntervalFormat::initializePattern(UErrorCode& status) {
datePattern, UCAL_MINUTE, status);
delete calData;
}
delete dtpng;
}
@ -937,12 +939,11 @@ DateIntervalFormat::setSeparateDateTimePtn(
void
DateIntervalFormat::setFallbackPattern(UCalendarDateFields field,
const UnicodeString& skeleton,
DateTimePatternGenerator* dtpng,
UErrorCode& status) {
if ( U_FAILURE(status) ) {
return;
}
UnicodeString pattern =dtpng->getBestPattern(skeleton, status);
UnicodeString pattern = fDtpng->getBestPattern(skeleton, status);
if ( U_FAILURE(status) ) {
return;
}

View File

@ -317,7 +317,8 @@ DateIntervalInfo::initializeData(const Locale& locale, UErrorCode& err)
ures_close(calBundle);
ures_close(rb);
status = U_ZERO_ERROR;
locNameLen = uloc_getParent(parentLocale, parentLocale,50,&status);
locNameLen = uloc_getParent(parentLocale, parentLocale,
ULOC_FULLNAME_CAPACITY,&status);
} while ( locNameLen > 0 );
}

View File

@ -37,6 +37,7 @@
U_NAMESPACE_BEGIN
class TimeZone;
class DateTimePatternGenerator;
/**
* DateFormat is an abstract class for a family of classes that convert dates and
@ -430,6 +431,7 @@ public:
*
* @param skeleton the skeleton on which date format based.
* @param locale the given locale.
* @param dtpng the date time pattern generator.
* @param status Output param to be set to success/failure code.
* If it is failure, the returned date formatter will
* be NULL.
@ -437,9 +439,10 @@ public:
* @internal ICU 4.0
*/
static DateFormat* U_EXPORT2 createPatternInstance(
const UnicodeString& skeleton,
const Locale& locale,
UErrorCode& status);
const UnicodeString& skeleton,
const Locale& locale,
DateTimePatternGenerator* dtpng,
UErrorCode& status);
/**
* Creates a time formatter with the given formatting style for the given

View File

@ -24,6 +24,7 @@
#include "unicode/smpdtfmt.h"
#include "unicode/dtintrv.h"
#include "unicode/dtitvinf.h"
#include "unicode/dtptngen.h"
U_NAMESPACE_BEGIN
@ -580,13 +581,13 @@ private:
* DateFormat and DateIntervalInfo objects.
* Caller should not delete them.
*
* @param dtfmt the SimpleDateFormat object to be adopted.
* @param locale the locale of this date interval formatter.
* @param dtitvinf the DateIntervalInfo object to be adopted.
* @param skeleton the skeleton of the date formatter
* @param status output param set to success/failure code on exit
* @internal ICU 4.0
*/
DateIntervalFormat(DateFormat* dtfmt, DateIntervalInfo* dtItvInfo,
DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
const UnicodeString* skeleton, UErrorCode& status);
@ -596,14 +597,14 @@ private:
*
* It is a wrapper of the constructor.
*
* @param dtfmt the DateFormat object to be adopted.
* @param locale the locale of this date interval formatter.
* @param dtitvinf the DateIntervalInfo object to be adopted.
* @param skeleton the skeleton of this formatter.
* @param status Output param set to success/failure code.
* @return a date time interval formatter which the caller owns.
* @internal ICU 4.0
*/
static DateIntervalFormat* U_EXPORT2 create(DateFormat* dtfmt,
static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
DateIntervalInfo* dtitvinf,
const UnicodeString* skeleton,
UErrorCode& status);
@ -684,13 +685,11 @@ private:
* a skeleton, and a date time pattern generator.
* @param field the largest different calendar field
* @param skeleton a skeleton
* @param dtpng date time pattern generator
* @param status output param set to success/failure code on exit
* @internal ICU 4.0
*/
void setFallbackPattern(UCalendarDateFields field,
const UnicodeString& skeleton,
DateTimePatternGenerator* dtpng,
UErrorCode& status);
@ -924,6 +923,11 @@ private:
Calendar* fFromCalendar;
Calendar* fToCalendar;
/**
* Date time pattern generator
*/
DateTimePatternGenerator* fDtpng;
/**
* Following are interval information relavent (locale) to this formatter.
*/