ICU-7387 Changed TimeUnitFormat::EStyle should bto C enum.

X-SVN-Rev: 29916
This commit is contained in:
Claire Ho 2011-04-28 16:42:58 +00:00
parent 6c49e812c0
commit 83d13b22aa
3 changed files with 59 additions and 58 deletions

View File

@ -77,18 +77,18 @@ static const UChar PLURAL_COUNT_TWO[] = {LOW_T, LOW_W, LOW_O, 0};
TimeUnitFormat::TimeUnitFormat(UErrorCode& status)
: fNumberFormat(NULL),
fPluralRules(NULL) {
create(Locale::getDefault(), kFull, status);
create(Locale::getDefault(), UTMUTFMT_FULL_STYLE, status);
}
TimeUnitFormat::TimeUnitFormat(const Locale& locale, UErrorCode& status)
: fNumberFormat(NULL),
fPluralRules(NULL) {
create(locale, kFull, status);
create(locale, UTMUTFMT_FULL_STYLE, status);
}
TimeUnitFormat::TimeUnitFormat(const Locale& locale, EStyle style, UErrorCode& status)
TimeUnitFormat::TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status)
: fNumberFormat(NULL),
fPluralRules(NULL) {
create(locale, style, status);
@ -99,7 +99,7 @@ TimeUnitFormat::TimeUnitFormat(const TimeUnitFormat& other)
: MeasureFormat(other),
fNumberFormat(NULL),
fPluralRules(NULL),
fStyle(kFull)
fStyle(UTMUTFMT_FULL_STYLE)
{
for (TimeUnit::UTimeUnitFields i = TimeUnit::UTIMEUNIT_YEAR;
i < TimeUnit::UTIMEUNIT_FIELD_COUNT;
@ -266,7 +266,8 @@ TimeUnitFormat::parseObject(const UnicodeString& source,
const UHashTok valueTok = elem->value;
// the value is a pair of MessageFormat*
MessageFormat** patterns = (MessageFormat**)valueTok.pointer;
for (EStyle style = kFull; style < kTotal; style = (EStyle)(style + 1)) {
for (UTimeUnitFormatStyle style = UTMUTFMT_FULL_STYLE; style < UTMUTFMT_FORMAT_STYLE_COUNT;
style = (UTimeUnitFormatStyle)(style + 1)) {
MessageFormat* pattern = patterns[style];
pos.setErrorIndex(-1);
pos.setIndex(oldPos);
@ -354,11 +355,11 @@ TimeUnitFormat::parseObject(const UnicodeString& source,
void
TimeUnitFormat::create(const Locale& locale, EStyle style, UErrorCode& status) {
TimeUnitFormat::create(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
if (style < kFull || style > kAbbreviate) {
if (style < UTMUTFMT_FULL_STYLE || style > UTMUTFMT_ABBREVIATED_STYLE) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
@ -383,10 +384,10 @@ TimeUnitFormat::create(const Locale& locale, EStyle style, UErrorCode& status) {
void
TimeUnitFormat::setup(UErrorCode& err) {
initDataMembers(err);
readFromCurrentLocale(kFull, gUnitsTag, err);
checkConsistency(kFull, gUnitsTag, err);
readFromCurrentLocale(kAbbreviate, gShortUnitsTag, err);
checkConsistency(kAbbreviate, gShortUnitsTag, err);
readFromCurrentLocale(UTMUTFMT_FULL_STYLE, gUnitsTag, err);
checkConsistency(UTMUTFMT_FULL_STYLE, gUnitsTag, err);
readFromCurrentLocale(UTMUTFMT_ABBREVIATED_STYLE, gShortUnitsTag, err);
checkConsistency(UTMUTFMT_ABBREVIATED_STYLE, gShortUnitsTag, err);
}
@ -412,7 +413,7 @@ TimeUnitFormat::initDataMembers(UErrorCode& err){
void
TimeUnitFormat::readFromCurrentLocale(EStyle style, const char* key, UErrorCode& err) {
TimeUnitFormat::readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, UErrorCode& err) {
if (U_FAILURE(err)) {
return;
}
@ -496,9 +497,9 @@ TimeUnitFormat::readFromCurrentLocale(EStyle style, const char* key, UErrorCode&
}
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(pluralCount);
if (formatters == NULL) {
formatters = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*));
formatters[kFull] = NULL;
formatters[kAbbreviate] = NULL;
formatters = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
formatters[UTMUTFMT_FULL_STYLE] = NULL;
formatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
countToPatterns->put(pluralCount, formatters, err);
if (U_FAILURE(err)) {
uprv_free(formatters);
@ -532,7 +533,7 @@ TimeUnitFormat::readFromCurrentLocale(EStyle style, const char* key, UErrorCode&
void
TimeUnitFormat::checkConsistency(EStyle style, const char* key, UErrorCode& err) {
TimeUnitFormat::checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& err) {
if (U_FAILURE(err)) {
return;
}
@ -598,7 +599,7 @@ TimeUnitFormat::checkConsistency(EStyle style, const char* key, UErrorCode& err)
// using patterns of plural count "other",
// then, "other" is the searchPluralCount.
void
TimeUnitFormat::searchInLocaleChain(EStyle style, const char* key, const char* localeName,
TimeUnitFormat::searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
TimeUnit::UTimeUnitFields srcTimeUnitField,
const char* srcPluralCount,
const char* searchPluralCount,
@ -631,9 +632,9 @@ TimeUnitFormat::searchInLocaleChain(EStyle style, const char* key, const char* l
}
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount);
if (formatters == NULL) {
formatters = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*));
formatters[kFull] = NULL;
formatters[kAbbreviate] = NULL;
formatters = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
formatters[UTMUTFMT_FULL_STYLE] = NULL;
formatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
countToPatterns->put(srcPluralCount, formatters, err);
if (U_FAILURE(err)) {
uprv_free(formatters);
@ -706,9 +707,9 @@ TimeUnitFormat::searchInLocaleChain(EStyle style, const char* key, const char* l
}
MessageFormat** formatters = (MessageFormat**)countToPatterns->get(srcPluralCount);
if (formatters == NULL) {
formatters = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*));
formatters[kFull] = NULL;
formatters[kAbbreviate] = NULL;
formatters = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
formatters[UTMUTFMT_FULL_STYLE] = NULL;
formatters[UTMUTFMT_ABBREVIATED_STYLE] = NULL;
countToPatterns->put(srcPluralCount, formatters, err);
if (U_FAILURE(err)) {
uprv_free(formatters);
@ -754,8 +755,8 @@ TimeUnitFormat::setNumberFormat(const NumberFormat& format, UErrorCode& status){
while ((elem = fTimeUnitToCountToPatterns[i]->nextElement(pos)) != NULL){
const UHashTok keyTok = elem->value;
MessageFormat** pattern = (MessageFormat**)keyTok.pointer;
pattern[kFull]->setFormat(0, format);
pattern[kAbbreviate]->setFormat(0, format);
pattern[UTMUTFMT_FULL_STYLE]->setFormat(0, format);
pattern[UTMUTFMT_ABBREVIATED_STYLE]->setFormat(0, format);
}
}
}
@ -769,8 +770,8 @@ TimeUnitFormat::deleteHash(Hashtable* htable) {
while ( (element = htable->nextElement(pos)) != NULL ) {
const UHashTok valueTok = element->value;
const MessageFormat** value = (const MessageFormat**)valueTok.pointer;
delete value[kFull];
delete value[kAbbreviate];
delete value[UTMUTFMT_FULL_STYLE];
delete value[UTMUTFMT_ABBREVIATED_STYLE];
//delete[] value;
uprv_free(value);
}
@ -792,7 +793,7 @@ TimeUnitFormat::copyHash(const Hashtable* source, Hashtable* target, UErrorCode&
const UnicodeString* key = (UnicodeString*)keyTok.pointer;
const UHashTok valueTok = element->value;
const MessageFormat** value = (const MessageFormat**)valueTok.pointer;
MessageFormat** newVal = (MessageFormat**)uprv_malloc(kTotal*sizeof(MessageFormat*));
MessageFormat** newVal = (MessageFormat**)uprv_malloc(UTMUTFMT_FORMAT_STYLE_COUNT*sizeof(MessageFormat*));
newVal[0] = (MessageFormat*)value[0]->clone();
newVal[1] = (MessageFormat*)value[1]->clone();
target->put(UnicodeString(*key), newVal, status);

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2010, Google, International Business Machines Corporation and *
* Copyright (C) 2011, Google, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -31,6 +31,19 @@
union UHashTok;
/**
* Constants for various styles.
* There are 2 styles: full name and abbreviated name.
* For example, for English, the full name for hour duration is "3 hours",
* and the abbreviated name is "3 hrs".
* @draft ICU 4.8
*/
enum UTimeUnitFormatStyle {
UTMUTFMT_FULL_STYLE,
UTMUTFMT_ABBREVIATED_STYLE,
UTMUTFMT_FORMAT_STYLE_COUNT
};
U_NAMESPACE_BEGIN
class Hashtable;
@ -69,19 +82,6 @@ class Hashtable;
class U_I18N_API TimeUnitFormat: public MeasureFormat {
public:
/**
* Constants for various styles.
* There are 2 styles: full name and abbreviated name.
* For example, for English, the full name for hour duration is "3 hours",
* and the abbreviated name is "3 hrs".
* @draft ICU 4.2
*/
enum EStyle {
kFull = 0,
kAbbreviate = 1,
kTotal = kAbbreviate + 1
};
/**
* Create TimeUnitFormat with default locale, and full name style.
* Use setLocale and/or setFormat to modify.
@ -99,7 +99,7 @@ public:
* Create TimeUnitFormat given locale and style.
* @draft ICU 4.2
*/
TimeUnitFormat(const Locale& locale, EStyle style, UErrorCode& status);
TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
/**
* Copy constructor.
@ -219,9 +219,9 @@ private:
Locale fLocale;
Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
PluralRules* fPluralRules;
EStyle fStyle;
UTimeUnitFormatStyle fStyle;
void create(const Locale& locale, EStyle style, UErrorCode& status);
void create(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
// it might actually be simpler to make them Decimal Formats later.
// initialize all private data members
@ -231,14 +231,14 @@ private:
void initDataMembers(UErrorCode& status);
// initialize fTimeUnitToCountToPatterns from current locale's resource.
void readFromCurrentLocale(EStyle style, const char* key, UErrorCode& status);
void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
// check completeness of fTimeUnitToCountToPatterns against all time units,
// and all plural rules, fill in fallback as necessary.
void checkConsistency(EStyle style, const char* key, UErrorCode& status);
void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
// fill in fTimeUnitToCountToPatterns from locale fall-back chain
void searchInLocaleChain(EStyle style, const char* key, const char* localeName,
void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
TimeUnit::UTimeUnitFields field, const char*,
const char*, Hashtable*, UErrorCode&);

View File

@ -1,5 +1,5 @@
/********************************************************************
* Copyright (c) 2008-2010, International Business Machines Corporation and
* Copyright (c) 2008-2011, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
@ -41,15 +41,15 @@ void TimeUnitTest::testBasic() {
UErrorCode status = U_ZERO_ERROR;
Locale loc(locales[locIndex]);
TimeUnitFormat** formats = new TimeUnitFormat*[2];
formats[TimeUnitFormat::kFull] = new TimeUnitFormat(loc, status);
formats[UTMUTFMT_FULL_STYLE] = new TimeUnitFormat(loc, status);
if (!assertSuccess("TimeUnitFormat(full)", status, TRUE)) return;
formats[TimeUnitFormat::kAbbreviate] = new TimeUnitFormat(loc, TimeUnitFormat::kAbbreviate, status);
formats[UTMUTFMT_ABBREVIATED_STYLE] = new TimeUnitFormat(loc, UTMUTFMT_ABBREVIATED_STYLE, status);
if (!assertSuccess("TimeUnitFormat(short)", status)) return;
#ifdef TUFMTTS_DEBUG
std::cout << "locale: " << locales[locIndex] << "\n";
#endif
for (int style = TimeUnitFormat::kFull;
style <= TimeUnitFormat::kAbbreviate;
for (int style = UTMUTFMT_FULL_STYLE;
style <= UTMUTFMT_ABBREVIATED_STYLE;
++style) {
for (TimeUnit::UTimeUnitFields j = TimeUnit::UTIMEUNIT_YEAR;
j < TimeUnit::UTIMEUNIT_FIELD_COUNT;
@ -90,8 +90,8 @@ void TimeUnitTest::testBasic() {
}
}
}
delete formats[TimeUnitFormat::kFull];
delete formats[TimeUnitFormat::kAbbreviate];
delete formats[UTMUTFMT_FULL_STYLE];
delete formats[UTMUTFMT_ABBREVIATED_STYLE];
delete[] formats;
}
}
@ -186,11 +186,11 @@ void TimeUnitTest::testAPI() {
delete tmf_en;
TimeUnitFormat* en_long = new TimeUnitFormat(Locale("en"), TimeUnitFormat::kFull, status);
TimeUnitFormat* en_long = new TimeUnitFormat(Locale("en"), UTMUTFMT_FULL_STYLE, status);
if (!assertSuccess("TimeUnitFormat(en...)", status)) return;
delete en_long;
TimeUnitFormat* en_short = new TimeUnitFormat(Locale("en"), TimeUnitFormat::kAbbreviate, status);
TimeUnitFormat* en_short = new TimeUnitFormat(Locale("en"), UTMUTFMT_ABBREVIATED_STYLE, status);
if (!assertSuccess("TimeUnitFormat(en...)", status)) return;
delete en_short;
@ -213,7 +213,7 @@ void TimeUnitTest::testGreek() {
const char* locales[] = {"el-GR", "el"};
TimeUnit::UTimeUnitFields tunits[] = {TimeUnit::UTIMEUNIT_SECOND, TimeUnit::UTIMEUNIT_MINUTE, TimeUnit::UTIMEUNIT_HOUR, TimeUnit::UTIMEUNIT_DAY, TimeUnit::UTIMEUNIT_MONTH, TimeUnit::UTIMEUNIT_YEAR};
TimeUnitFormat::EStyle styles[] = {TimeUnitFormat::kFull, TimeUnitFormat::kAbbreviate};
UTimeUnitFormatStyle styles[] = {UTMUTFMT_FULL_STYLE, UTMUTFMT_ABBREVIATED_STYLE};
const int numbers[] = {1, 7};
const UChar oneSecond[] = {0x0031, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4, 0x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03bf, 0};