ICU-8464 Change C++ code according to latest API proposal.

X-SVN-Rev: 34712
This commit is contained in:
Travis Keep 2013-12-04 22:50:25 +00:00
parent 686c54f0fd
commit 65ecb0b5fa
3 changed files with 60 additions and 33 deletions

View file

@ -610,13 +610,33 @@ static void getFromCache(const char *locale, SharedPtr<RelativeDateTimeData>& 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 */

View file

@ -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.
* <p>
* 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.
* <p>
* This class is not intended for public subclassing.
* <p>
* Here are some examples of use:
* <blockquote>
@ -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<icu::RelativeDateTimeData> ptr;

View file

@ -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(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");
}