]> granicus.if.org Git - icu/commitdiff
ICU-8464 Change C++ code according to latest API proposal.
authorTravis Keep <keep94@gmail.com>
Wed, 4 Dec 2013 22:50:25 +0000 (22:50 +0000)
committerTravis Keep <keep94@gmail.com>
Wed, 4 Dec 2013 22:50:25 +0000 (22:50 +0000)
X-SVN-Rev: 34712

icu4c/source/i18n/reldatefmt.cpp
icu4c/source/i18n/unicode/reldatefmt.h
icu4c/source/test/intltest/reldatefmttest.cpp

index 02c79175ef22defe1bad111f87338935d3484db4..ba7e757203783b95fc1deee748cc745c78f8bc47 100644 (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 */
index 3bd1ca06298aea18c535514cb1506f5b2b4bfb5a..0bf6ed630e41ba2425cc88de65fd12faf6913688 100644 (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;
index 39ae56b028dceacaf705a5d419d768c13001853b..09bf0bb565fb91609b63d584a74e53e51fcb6d4a 100644 (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");
 }