}
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) {
}
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 */
* 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>
*/
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
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;
void TestSpanishNoQuantity();
void TestFormatWithQuantityIllegalArgument();
void TestFormatWithoutQuantityIllegalArgument();
- void TestSetNumberFormat();
+ void TestCustomNumberFormat();
void TestCombineDateAndTime();
void RunTest(
const Locale& locale,
TESTCASE_AUTO(TestSpanishNoQuantity);
TESTCASE_AUTO(TestFormatWithQuantityIllegalArgument);
TESTCASE_AUTO(TestFormatWithoutQuantityIllegalArgument);
- TESTCASE_AUTO(TestSetNumberFormat);
+ TESTCASE_AUTO(TestCustomNumberFormat);
TESTCASE_AUTO(TestCombineDateAndTime);
TESTCASE_AUTO_END;
}
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");
}