ICU-10591 Hoist setContext from SimpleDateFormat/RelativeDateFormat to DateFormat

X-SVN-Rev: 34881
This commit is contained in:
Peter Edberg 2014-01-12 05:23:30 +00:00
parent a5f9fc6974
commit 9cf4a2acd5
7 changed files with 97 additions and 172 deletions

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1997-2013, International Business Machines Corporation and *
* Copyright (C) 1997-2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
@ -25,6 +25,7 @@
#include "unicode/datefmt.h"
#include "unicode/smpdtfmt.h"
#include "unicode/dtptngen.h"
#include "unicode/udisplaycontext.h"
#include "reldtfmt.h"
#include "cstring.h"
@ -42,7 +43,8 @@ U_NAMESPACE_BEGIN
DateFormat::DateFormat()
: fCalendar(0),
fNumberFormat(0)
fNumberFormat(0),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
{
}
@ -51,7 +53,8 @@ DateFormat::DateFormat()
DateFormat::DateFormat(const DateFormat& other)
: Format(other),
fCalendar(0),
fNumberFormat(0)
fNumberFormat(0),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
{
*this = other;
}
@ -75,6 +78,7 @@ DateFormat& DateFormat::operator=(const DateFormat& other)
fNumberFormat = NULL;
}
fBoolFlags = other.fBoolFlags;
fCapitalizationContext = other.fCapitalizationContext;
}
return *this;
}
@ -102,7 +106,8 @@ DateFormat::operator==(const Format& other) const
return (this == fmt) ||
(Format::operator==(other) &&
fCalendar&&(fCalendar->isEquivalentTo(*fmt->fCalendar)) &&
(fNumberFormat && *fNumberFormat == *fmt->fNumberFormat));
(fNumberFormat && *fNumberFormat == *fmt->fNumberFormat) &&
(fCapitalizationContext == fmt->fCapitalizationContext) );
}
//----------------------------------------------------------------------
@ -512,6 +517,36 @@ DateFormat::isLenient() const
return FALSE;
}
//----------------------------------------------------------------------
void DateFormat::setContext(UDisplayContext value, UErrorCode& status)
{
if (U_FAILURE(status))
return;
if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZATION ) {
fCapitalizationContext = value;
} else {
status = U_ILLEGAL_ARGUMENT_ERROR;
}
}
//----------------------------------------------------------------------
UDisplayContext DateFormat::getContext(UDisplayContextType type, UErrorCode& status) const
{
if (U_FAILURE(status))
return (UDisplayContext)0;
if (type != UDISPCTX_TYPE_CAPITALIZATION) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return (UDisplayContext)0;
}
return fCapitalizationContext;
}
//----------------------------------------------------------------------
DateFormat&

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2007-2013, International Business Machines Corporation and
* Copyright (C) 2007-2014, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -45,7 +45,7 @@ RelativeDateFormat::RelativeDateFormat(const RelativeDateFormat& other) :
fDateStyle(other.fDateStyle), fLocale(other.fLocale),
fDayMin(other.fDayMin), fDayMax(other.fDayMax),
fDatesLen(other.fDatesLen), fDates(NULL),
fCapitalizationContext(other.fCapitalizationContext), fCombinedHasDateAtStart(other.fCombinedHasDateAtStart)
fCombinedHasDateAtStart(other.fCombinedHasDateAtStart)
{
if(other.fDateTimeFormatter != NULL) {
fDateTimeFormatter = (SimpleDateFormat*)other.fDateTimeFormatter->clone();
@ -65,7 +65,7 @@ RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatS
const Locale& locale, UErrorCode& status) :
DateFormat(), fDateTimeFormatter(NULL), fDatePattern(), fTimePattern(), fCombinedFormat(NULL),
fDateStyle(dateStyle), fLocale(locale), fDayMin(0), fDayMax(0), fDatesLen(0), fDates(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE), fCombinedHasDateAtStart(FALSE)
fCombinedHasDateAtStart(FALSE)
{
if(U_FAILURE(status) ) {
return;
@ -130,8 +130,7 @@ UBool RelativeDateFormat::operator==(const Format& other) const {
return (fDateStyle==that->fDateStyle &&
fDatePattern==that->fDatePattern &&
fTimePattern==that->fTimePattern &&
fLocale==that->fLocale &&
fCapitalizationContext==that->fCapitalizationContext);
fLocale==that->fLocale );
}
return FALSE;
}
@ -144,6 +143,7 @@ UnicodeString& RelativeDateFormat::format( Calendar& cal,
UErrorCode status = U_ZERO_ERROR;
UnicodeString relativeDayString;
UDisplayContext capitalizationContext = getContext(UDISPCTX_TYPE_CAPITALIZATION, status);
// calculate the difference, in days, between 'cal' and now.
int dayDiff = dayDifference(cal, status);
@ -159,9 +159,9 @@ UnicodeString& RelativeDateFormat::format( Calendar& cal,
if ( relativeDayString.length() > 0 && !fDatePattern.isEmpty() &&
(fTimePattern.isEmpty() || fCombinedFormat == NULL || fCombinedHasDateAtStart)) {
// capitalize relativeDayString according to context for tense, set formatter no context
if ( fCapitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
(fCapitalizationContext==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU && fCapitalizationForRelativeUnits[0]) ||
(fCapitalizationContext==UDISPCTX_CAPITALIZATION_FOR_STANDALONE && fCapitalizationForRelativeUnits[1]) ) {
if ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
(capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU && fCapitalizationForRelativeUnits[0]) ||
(capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_STANDALONE && fCapitalizationForRelativeUnits[1]) ) {
// titlecase first word of relativeDayString, do like LocaleDisplayNamesImpl::adjustForUsageAndContext
int32_t stopPos, stopPosLimit = 8;
if ( stopPosLimit > len ) {
@ -189,7 +189,7 @@ UnicodeString& RelativeDateFormat::format( Calendar& cal,
fDateTimeFormatter->setContext(UDISPCTX_CAPITALIZATION_NONE, status);
} else {
// set our context for the formatter
fDateTimeFormatter->setContext(fCapitalizationContext, status);
fDateTimeFormatter->setContext(capitalizationContext, status);
}
if (fDatePattern.isEmpty()) {
@ -547,36 +547,6 @@ void RelativeDateFormat::loadDates(UErrorCode &status) {
// the fDates[] array could be sorted here, for direct access.
}
//----------------------------------------------------------------------
void RelativeDateFormat::setContext(UDisplayContext value, UErrorCode& status)
{
if (U_FAILURE(status))
return;
if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZATION ) {
fCapitalizationContext = value;
} else {
status = U_ILLEGAL_ARGUMENT_ERROR;
}
}
//----------------------------------------------------------------------
UDisplayContext RelativeDateFormat::getContext(UDisplayContextType type, UErrorCode& status) const
{
if (U_FAILURE(status))
return (UDisplayContext)0;
if (type != UDISPCTX_TYPE_CAPITALIZATION) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return (UDisplayContext)0;
}
return fCapitalizationContext;
}
//----------------------------------------------------------------------
// this should to be in DateFormat, instead it was copied from SimpleDateFormat.

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2007-2013, International Business Machines Corporation and *
* Copyright (C) 2007-2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -19,7 +19,6 @@
#include "unicode/datefmt.h"
#include "unicode/smpdtfmt.h"
#include "unicode/udisplaycontext.h"
U_NAMESPACE_BEGIN
@ -233,30 +232,6 @@ public:
*/
virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
/**
* Set a particular UDisplayContext value in the formatter, such as
* UDISPCTX_CAPITALIZATION_FOR_STANDALONE.
* @param value The UDisplayContext value to set.
* @param status Input/output status. If at entry this indicates a failure
* status, the function will do nothing; otherwise this will be
* updated with any new status from the function.
* @internal
*/
virtual void setContext(UDisplayContext value, UErrorCode& status);
/**
* Get the formatter's UDisplayContext value for the specified UDisplayContextType,
* such as UDISPCTX_TYPE_CAPITALIZATION.
* @param type The UDisplayContextType whose value to return
* @param status Input/output status. If at entry this indicates a failure
* status, the function will do nothing; otherwise this will be
* updated with any new status from the function.
* @return The UDisplayContextValue for the specified type.
* @internal
*/
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
private:
SimpleDateFormat *fDateTimeFormatter;
UnicodeString fDatePattern;
@ -271,7 +246,6 @@ private:
int32_t fDatesLen; // Length of array
URelativeString *fDates; // array of strings
UDisplayContext fCapitalizationContext;
UBool fCapitalizationForRelativeUnits[2];
UBool fCombinedHasDateAtStart;

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1997-2013, International Business Machines Corporation and *
* Copyright (C) 1997-2014, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
@ -245,8 +245,7 @@ SimpleDateFormat::SimpleDateFormat(UErrorCode& status)
fSymbols(NULL),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, true, status).setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, true, status).setBooleanAttribute(UDAT_PARSE_PARTIAL_MATCH, true, status);
construct(kShort, (EStyle) (kShort + kDateOffset), fLocale, status);
@ -262,8 +261,7 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
fSymbols(NULL),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
fDateOverride.setToBogus();
fTimeOverride.setToBogus();
@ -283,8 +281,7 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
fSymbols(NULL),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
fDateOverride.setTo(override);
fTimeOverride.setToBogus();
@ -306,8 +303,7 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
fLocale(locale),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
fDateOverride.setToBogus();
@ -329,8 +325,7 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
fLocale(locale),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
fDateOverride.setTo(override);
@ -355,8 +350,7 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
fSymbols(symbolsToAdopt),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
fDateOverride.setToBogus();
@ -378,8 +372,7 @@ SimpleDateFormat::SimpleDateFormat(const UnicodeString& pattern,
fSymbols(new DateFormatSymbols(symbols)),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
fDateOverride.setToBogus();
@ -402,8 +395,7 @@ SimpleDateFormat::SimpleDateFormat(EStyle timeStyle,
fSymbols(NULL),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, true, status).setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, true, status).setBooleanAttribute(UDAT_PARSE_PARTIAL_MATCH, true, status);
construct(timeStyle, dateStyle, fLocale, status);
@ -426,8 +418,7 @@ SimpleDateFormat::SimpleDateFormat(const Locale& locale,
fSymbols(NULL),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
if (U_FAILURE(status)) return;
initializeSymbols(fLocale, initializeCalendar(NULL, fLocale, status),status);
@ -462,8 +453,7 @@ SimpleDateFormat::SimpleDateFormat(const SimpleDateFormat& other)
fSymbols(NULL),
fTimeZoneFormat(NULL),
fNumberFormatters(NULL),
fOverrideList(NULL),
fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
fOverrideList(NULL)
{
UErrorCode status = U_ZERO_ERROR;
setBooleanAttribute(UDAT_PARSE_ALLOW_WHITESPACE, true, status).setBooleanAttribute(UDAT_PARSE_ALLOW_NUMERIC, true, status).setBooleanAttribute(UDAT_PARSE_PARTIAL_MATCH, true, status);
@ -498,8 +488,6 @@ SimpleDateFormat& SimpleDateFormat::operator=(const SimpleDateFormat& other)
fLocale = other.fLocale;
}
fCapitalizationContext = other.fCapitalizationContext;
return *this;
}
@ -524,8 +512,7 @@ SimpleDateFormat::operator==(const Format& other) const
that->fSymbols != NULL && // Check for pathological object
*fSymbols == *that->fSymbols &&
fHaveDefaultCentury == that->fHaveDefaultCentury &&
fDefaultCenturyStart == that->fDefaultCenturyStart &&
fCapitalizationContext == that->fCapitalizationContext);
fDefaultCenturyStart == that->fDefaultCenturyStart);
}
return FALSE;
}
@ -876,6 +863,7 @@ SimpleDateFormat::_format(Calendar& cal, UnicodeString& appendTo,
UChar prevCh = 0;
int32_t count = 0;
int32_t fieldNum = 0;
UDisplayContext capitalizationContext = getContext(UDISPCTX_TYPE_CAPITALIZATION, status);
// loop through the pattern string character by character
for (int32_t i = 0; i < fPattern.length() && U_SUCCESS(status); ++i) {
@ -884,7 +872,7 @@ SimpleDateFormat::_format(Calendar& cal, UnicodeString& appendTo,
// Use subFormat() to format a repeated pattern character
// when a different pattern or non-pattern character is seen
if (ch != prevCh && count > 0) {
subFormat(appendTo, prevCh, count, fCapitalizationContext, fieldNum++, handler, *workCal, status);
subFormat(appendTo, prevCh, count, capitalizationContext, fieldNum++, handler, *workCal, status);
count = 0;
}
if (ch == QUOTE) {
@ -912,7 +900,7 @@ SimpleDateFormat::_format(Calendar& cal, UnicodeString& appendTo,
// Format the last item in the pattern, if any
if (count > 0) {
subFormat(appendTo, prevCh, count, fCapitalizationContext, fieldNum++, handler, *workCal, status);
subFormat(appendTo, prevCh, count, capitalizationContext, fieldNum++, handler, *workCal, status);
}
if (calClone != NULL) {
@ -3298,36 +3286,6 @@ void SimpleDateFormat::adoptCalendar(Calendar* calendarToAdopt)
//----------------------------------------------------------------------
void SimpleDateFormat::setContext(UDisplayContext value, UErrorCode& status)
{
if (U_FAILURE(status))
return;
if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZATION ) {
fCapitalizationContext = value;
} else {
status = U_ILLEGAL_ARGUMENT_ERROR;
}
}
//----------------------------------------------------------------------
UDisplayContext SimpleDateFormat::getContext(UDisplayContextType type, UErrorCode& status) const
{
if (U_FAILURE(status))
return (UDisplayContext)0;
if (type != UDISPCTX_TYPE_CAPITALIZATION) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return (UDisplayContext)0;
}
return fCapitalizationContext;
}
//----------------------------------------------------------------------
UBool
SimpleDateFormat::isFieldUnitIgnored(UCalendarDateFields field) const {
return isFieldUnitIgnored(fPattern, field);

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1996-2013, International Business Machines
* Copyright (C) 1996-2014, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
*/
@ -980,20 +980,13 @@ udat_getLocaleByType(const UDateFormat *fmt,
return ((Format*)fmt)->getLocaleID(type, *status);
}
U_CAPI void U_EXPORT2
udat_setContext(UDateFormat* fmt, UDisplayContext value, UErrorCode* status)
{
if (U_FAILURE(*status)) {
return;
}
if (dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))!=NULL) {
((SimpleDateFormat*)fmt)->setContext(value, *status);
} else if (dynamic_cast<const RelativeDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))!=NULL) {
((RelativeDateFormat*)fmt)->setContext(value, *status);
} else {
*status = U_ILLEGAL_ARGUMENT_ERROR;
}
((DateFormat*)fmt)->setContext(value, *status);
return;
}
@ -1003,13 +996,7 @@ udat_getContext(UDateFormat* fmt, UDisplayContextType type, UErrorCode* status)
if (U_FAILURE(*status)) {
return (UDisplayContext)0;
}
if (dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))!=NULL) {
return ((SimpleDateFormat*)fmt)->getContext(type, *status);
} else if (dynamic_cast<const RelativeDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))!=NULL) {
return ((RelativeDateFormat*)fmt)->getContext(type, *status);
}
*status = U_ILLEGAL_ARGUMENT_ERROR;
return (UDisplayContext)0;
return ((DateFormat*)fmt)->getContext(type, *status);
}

View File

@ -1,6 +1,6 @@
/*
********************************************************************************
* Copyright (C) 1997-2013, International Business Machines
* Copyright (C) 1997-2014, International Business Machines
* Corporation and others. All Rights Reserved.
********************************************************************************
*
@ -29,6 +29,7 @@
#include "unicode/format.h"
#include "unicode/locid.h"
#include "unicode/enumset.h"
#include "unicode/udisplaycontext.h"
/**
* \file
@ -673,6 +674,31 @@ public:
*/
virtual void setTimeZone(const TimeZone& zone);
/* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
/**
* Set a particular UDisplayContext value in the formatter, such as
* UDISPCTX_CAPITALIZATION_FOR_STANDALONE.
* @param value The UDisplayContext value to set.
* @param status Input/output status. If at entry this indicates a failure
* status, the function will do nothing; otherwise this will be
* updated with any new status from the function.
* @draft ICU 53
*/
virtual void setContext(UDisplayContext value, UErrorCode& status);
/* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
/**
* Get the formatter's UDisplayContext value for the specified UDisplayContextType,
* such as UDISPCTX_TYPE_CAPITALIZATION.
* @param type The UDisplayContextType whose value to return
* @param status Input/output status. If at entry this indicates a failure
* status, the function will do nothing; otherwise this will be
* updated with any new status from the function.
* @return The UDisplayContextValue for the specified type.
* @draft ICU 53
*/
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
/**
* Set an boolean attribute on this DateFormat.
* May return U_UNSUPPORTED_ERROR if this instance does not support
@ -755,6 +781,8 @@ private:
EnumSet<UDateFormatBooleanAttribute, 0, UDAT_BOOLEAN_ATTRIBUTE_COUNT> fBoolFlags;
UDisplayContext fCapitalizationContext;
public:
#ifndef U_HIDE_OBSOLETE_API
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 1997-2013, International Business Machines Corporation and
* Copyright (C) 1997-2014, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
@ -1083,31 +1083,6 @@ public:
*/
virtual void adoptCalendar(Calendar* calendarToAdopt);
/* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
/**
* Set a particular UDisplayContext value in the formatter, such as
* UDISPCTX_CAPITALIZATION_FOR_STANDALONE.
* @param value The UDisplayContext value to set.
* @param status Input/output status. If at entry this indicates a failure
* status, the function will do nothing; otherwise this will be
* updated with any new status from the function.
* @draft ICU 51
*/
virtual void setContext(UDisplayContext value, UErrorCode& status);
/* Cannot use #ifndef U_HIDE_DRAFT_API for the following draft method since it is virtual */
/**
* Get the formatter's UDisplayContext value for the specified UDisplayContextType,
* such as UDISPCTX_TYPE_CAPITALIZATION.
* @param type The UDisplayContextType whose value to return
* @param status Input/output status. If at entry this indicates a failure
* status, the function will do nothing; otherwise this will be
* updated with any new status from the function.
* @return The UDisplayContextValue for the specified type.
* @draft ICU 51
*/
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
/* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
/**
* Sets the TimeZoneFormat to be used by this date/time formatter.
@ -1538,8 +1513,6 @@ private:
NSOverride *fOverrideList;
UBool fHaveDefaultCentury;
UDisplayContext fCapitalizationContext;
};
inline UDate