From 65ecb0b5fa542a87102f078cf0d3dc825a828b4a Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Wed, 4 Dec 2013 22:50:25 +0000 Subject: [PATCH] ICU-8464 Change C++ code according to latest API proposal. X-SVN-Rev: 34712 --- icu4c/source/i18n/reldatefmt.cpp | 30 +++++++++++------ icu4c/source/i18n/unicode/reldatefmt.h | 32 ++++++++++++++----- icu4c/source/test/intltest/reldatefmttest.cpp | 31 +++++++++--------- 3 files changed, 60 insertions(+), 33 deletions(-) diff --git a/icu4c/source/i18n/reldatefmt.cpp b/icu4c/source/i18n/reldatefmt.cpp index 02c79175ef2..ba7e7572037 100644 --- a/icu4c/source/i18n/reldatefmt.cpp +++ b/icu4c/source/i18n/reldatefmt.cpp @@ -610,13 +610,33 @@ static void getFromCache(const char *locale, SharedPtr& pt } RelativeDateTimeFormatter::RelativeDateTimeFormatter(UErrorCode& status) { - getFromCache(Locale::getDefault().getName(), ptr, status); + getFromCache(Locale::getDefault().getName(), ptr, status); } RelativeDateTimeFormatter::RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status) { getFromCache(locale.getName(), ptr, status); } +RelativeDateTimeFormatter::RelativeDateTimeFormatter( + const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status) { + getFromCache(locale.getName(), ptr, status); + if (U_FAILURE(status)) { + return; + } + RelativeDateTimeData* wptr = ptr.readWrite(); + if (wptr == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + return; + } + if (!wptr->numberFormat.adoptInstead(nfToAdopt)) { + status = U_MEMORY_ALLOCATION_ERROR; + return; + } +} + +const NumberFormat& RelativeDateTimeFormatter::getNumberFormat() const { + return *ptr->numberFormat; +} RelativeDateTimeFormatter::RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other) : ptr(other.ptr) { } @@ -694,14 +714,6 @@ UnicodeString& RelativeDateTimeFormatter::combineDateAndTime( return ptr->combinedDateAndTime->format(formattable, 2, appendTo, fpos, status); } -void RelativeDateTimeFormatter::setNumberFormat(const NumberFormat& nf) { - RelativeDateTimeData *wptr = ptr.readWrite(); - NumberFormat *newNf = (NumberFormat *) nf.clone(); - if (newNf != NULL && wptr != NULL) { - wptr->numberFormat.adoptInstead(newNf); - } -} - U_NAMESPACE_END #endif /* !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/i18n/unicode/reldatefmt.h b/icu4c/source/i18n/unicode/reldatefmt.h index 3bd1ca06298..0bf6ed630e4 100644 --- a/icu4c/source/i18n/unicode/reldatefmt.h +++ b/icu4c/source/i18n/unicode/reldatefmt.h @@ -236,7 +236,13 @@ class NumberFormat; * involving one single unit. This API does not support relative dates * involving compound units. * e.g "in 5 days and 4 hours" nor does it support parsing. - * This class is NOT thread-safe. + *

+ * This class is mostly thread safe and immutable with the following caveats: + * 1. The assignment operator violates Immutability. It must not be used + * concurrently with other operations. + * 2. Caller must not hold onto adopted pointers. + *

+ * This class is not intended for public subclassing. *

* Here are some examples of use: *

@@ -292,13 +298,25 @@ public: */ RelativeDateTimeFormatter(UErrorCode& status); - /** * Create RelativeDateTimeFormatter with given locale. * @draft ICU 53 */ RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status); + /** + * Create RelativeDateTimeFormatter with given locale and NumberFormat. + * + * @param locale the locale + * @param nfToAdopt Constructed object takes ownership of this pointer. + * It is an error for caller to delete this pointer or change its + * contents after calling this constructor. + * @status Any error is returned here. + * @draft ICU 53 + */ + RelativeDateTimeFormatter( + const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status); + /** * Copy constructor. * @draft ICU 53 @@ -365,14 +383,12 @@ public: UnicodeString& appendTo, UErrorCode& status) const; /** - * Specify which NumberFormat object this object should use for - * formatting numbers. By default this object uses the default - * NumberFormat object for this object's locale. - * @param nf the NumberFormat object to use. - * @see #format(double, Direction, RelativeUnit) + * Returns the NumberFormat this object is using. + * * @draft ICU 53 */ - void setNumberFormat(const NumberFormat& nf); + const NumberFormat& getNumberFormat() const; + private: RelativeDateTimeFormatter(); SharedPtr ptr; diff --git a/icu4c/source/test/intltest/reldatefmttest.cpp b/icu4c/source/test/intltest/reldatefmttest.cpp index 39ae56b028d..09bf0bb565f 100644 --- a/icu4c/source/test/intltest/reldatefmttest.cpp +++ b/icu4c/source/test/intltest/reldatefmttest.cpp @@ -185,7 +185,7 @@ private: void TestSpanishNoQuantity(); void TestFormatWithQuantityIllegalArgument(); void TestFormatWithoutQuantityIllegalArgument(); - void TestSetNumberFormat(); + void TestCustomNumberFormat(); void TestCombineDateAndTime(); void RunTest( const Locale& locale, @@ -235,7 +235,7 @@ void RelativeDateTimeFormatterTest::runIndexedTest( TESTCASE_AUTO(TestSpanishNoQuantity); TESTCASE_AUTO(TestFormatWithQuantityIllegalArgument); TESTCASE_AUTO(TestFormatWithoutQuantityIllegalArgument); - TESTCASE_AUTO(TestSetNumberFormat); + TESTCASE_AUTO(TestCustomNumberFormat); TESTCASE_AUTO(TestCombineDateAndTime); TESTCASE_AUTO_END; } @@ -279,22 +279,21 @@ void RelativeDateTimeFormatterTest::TestFormatWithoutQuantityIllegalArgument() { VerifyIllegalArgument(fmt, UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_NOW); } -void RelativeDateTimeFormatterTest::TestSetNumberFormat() { +void RelativeDateTimeFormatterTest::TestCustomNumberFormat() { + NumberFormat *nf; UErrorCode status = U_ZERO_ERROR; - RelativeDateTimeFormatter fmt("en", status); - if (U_FAILURE(status)) { - dataerrln("Failure creating format object - %s", u_errorName(status)); - return; + { + RelativeDateTimeFormatter fmt("en", status); + if (U_FAILURE(status)) { + dataerrln( + "Failure creating format object - %s", u_errorName(status)); + return; + } + nf = (NumberFormat *) fmt.getNumberFormat().clone(); } - LocalPointer numberFormat(NumberFormat::createInstance("en", status)); - numberFormat->setMinimumFractionDigits(1); - numberFormat->setMaximumFractionDigits(1); - fmt.setNumberFormat(*numberFormat); - - // Prove that we made a defensive copy. - numberFormat->setMinimumFractionDigits(3); - numberFormat->setMaximumFractionDigits(3); - + nf->setMinimumFractionDigits(1); + nf->setMaximumFractionDigits(1); + RelativeDateTimeFormatter fmt("en", nf, status); RunTest(fmt, kEnglishDecimal, LENGTHOF(kEnglishDecimal), "en decimal digits"); } -- 2.40.0