]> granicus.if.org Git - icu/commitdiff
ICU-9346 C++ Collator API cleanup, fix safeClone() bug (setRuleStringFromCollator...
authorMarkus Scherer <markus.icu@gmail.com>
Fri, 3 Aug 2012 21:49:28 +0000 (21:49 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Fri, 3 Aug 2012 21:49:28 +0000 (21:49 +0000)
X-SVN-Rev: 32100

icu4c/readme.html
icu4c/source/i18n/coll.cpp
icu4c/source/i18n/tblcoll.cpp
icu4c/source/i18n/unicode/coll.h
icu4c/source/i18n/unicode/tblcoll.h
icu4c/source/test/intltest/apicoll.cpp

index e8a673f6cdc4c647f5412980818fa19e06530ec8..9e4405920aacedf7acb9f6945a84d55d8221d500 100644 (file)
@@ -23,7 +23,7 @@
     It is not recommended for production use.
     </p>
 
-    <p>Last updated: 2012-Jun-25<br />
+    <p>Last updated: 2012-Aug-03<br />
      Copyright &copy; 1997-2012 International Business Machines Corporation and
     others. All Rights Reserved.</p>
     <!-- Remember that there is a copyright at the end too -->
     <code>u_setMutexFunctions()</code> and <code>u_setAtomicIncDecFunctions()</code>
     with empty implementation functions.</p>
 
+    <h3>C++ Collator subclassing-API breaking changes</h3>
+    <p>We have made some changes to the C++ Collator API for ICU 50
+    that will make it easier to use and implement the Collator but
+    which are incompatible for subclasses.
+    If there are subclasses, they will have to be modified as well.
+    It will be easy to adapt subclass source code, if there is any.
+    We think it is unlikely (or at least rare) that users write subclasses of the C++ Collator,
+    given the number of virtual methods and the complexities of a collation implementation.</p>
+
+    <p>For details see the email "ICU4C C++ Collator subclassing-API breaking changes"
+    sent on 2012-jul-25 to the icu-design and icu-support
+    <a href="http://site.icu-project.org/contacts">mailing lists</a>,
+    and <a href="http://bugs.icu-project.org/trac/ticket/9346">ICU ticket #9346</a>,
+    including the <a href="http://bugs.icu-project.org/trac/review/9346">changes for that ticket</a>.</p>
+
+    <p>In particular, the <code>class TestCollator</code> in
+    <a href="http://bugs.icu-project.org/trac/browser/icu/trunk/source/test/intltest/apicoll.cpp"><code>source/test/intltest/apicoll.cpp</code></a>
+    illustrates how a subclass needs to be changed.
+    However, note that the TestCollator was also simplified slightly;
+    not all changes made there were strictly required for the API signature changes.</p>
+
     <h2><a name="Download" href="#Download" id="Download">How To Download the
     Source Code</a></h2>
 
index 4e191f1b496d816517e9b7815b5c551d28c2ab21..2aaa80a30e1bc514bcb1de833fb280aaf11e509b 100644 (file)
@@ -37,6 +37,8 @@
  * 01/29/01     synwee      Modified into a C++ wrapper calling C APIs (ucol.h)
  */
 
+#include "utypeinfo.h"  // for 'typeid' to work
+
 #include "unicode/utypes.h"
 
 #if !UCONFIG_NO_COLLATION
@@ -428,7 +430,7 @@ Collator::EComparisonResult Collator::compare(const UnicodeString& source,
                                     const UnicodeString& target) const
 {
     UErrorCode ec = U_ZERO_ERROR;
-    return (Collator::EComparisonResult)compare(source, target, ec);
+    return (EComparisonResult)compare(source, target, ec);
 }
 
 // implement deprecated, previously abstract method
@@ -437,7 +439,7 @@ Collator::EComparisonResult Collator::compare(const UnicodeString& source,
                                     int32_t length) const
 {
     UErrorCode ec = U_ZERO_ERROR;
-    return (Collator::EComparisonResult)compare(source, target, length, ec);
+    return (EComparisonResult)compare(source, target, length, ec);
 }
 
 // implement deprecated, previously abstract method
@@ -446,7 +448,7 @@ Collator::EComparisonResult Collator::compare(const UChar* source, int32_t sourc
                                     const
 {
     UErrorCode ec = U_ZERO_ERROR;
-    return (Collator::EComparisonResult)compare(source, sourceLength, target, targetLength, ec);
+    return (EComparisonResult)compare(source, sourceLength, target, targetLength, ec);
 }
 
 UCollationResult Collator::compare(UCharIterator &/*sIter*/,
@@ -574,7 +576,8 @@ Collator::Collator(const Collator &other)
 
 UBool Collator::operator==(const Collator& other) const
 {
-    return (UBool)(this == &other);
+    // Subclasses: Call this method and then add more specific checks.
+    return typeid(*this) == typeid(other);
 }
 
 UBool Collator::operator!=(const Collator& other) const
@@ -855,6 +858,18 @@ Collator::getFunctionalEquivalent(const char* keyword, const Locale& locale,
     return Locale::createFromName(loc);
 }
 
+Collator::ECollationStrength
+Collator::getStrength(void) const {
+    UErrorCode intStatus = U_ZERO_ERROR;
+    return (ECollationStrength)getAttribute(UCOL_STRENGTH, intStatus);
+}
+
+void
+Collator::setStrength(ECollationStrength newStrength) {
+    UErrorCode intStatus = U_ZERO_ERROR;
+    setAttribute(UCOL_STRENGTH, (UColAttributeValue)newStrength, intStatus);
+}
+
 int32_t U_EXPORT2 
 Collator::getReorderCodes(int32_t* /* dest*/,
                           int32_t /* destCapacity*/,
index 83df78271d0b9d23c7f18bbf0b51584e55775c4c..8e23b956d7afdbb98ece3dda1730ff162ed95555 100644 (file)
@@ -54,8 +54,6 @@
  * 01/29/01     synwee      Modified into a C++ wrapper calling C APIs (ucol.h)
  */
 
-#include "utypeinfo.h"  // for 'typeid' to work
-
 #include "unicode/utypes.h"
 
 #if !UCONFIG_NO_COLLATION
@@ -103,7 +101,7 @@ RuleBasedCollator::RuleBasedCollator(const UnicodeString& rules,
                                      UErrorCode& status) : dataIsOwned(FALSE)
 {
     construct(rules,
-        getUCollationStrength(collationStrength),
+        (UColAttributeValue)collationStrength,
         UCOL_DEFAULT,
         status);
 }
@@ -125,7 +123,7 @@ RuleBasedCollator::RuleBasedCollator(const UnicodeString& rules,
                                      UErrorCode& status) : dataIsOwned(FALSE)
 {
     construct(rules,
-        getUCollationStrength(collationStrength),
+        (UColAttributeValue)collationStrength,
         decompositionMode,
         status);
 }
@@ -193,30 +191,16 @@ RuleBasedCollator::~RuleBasedCollator()
 UBool RuleBasedCollator::operator==(const Collator& that) const
 {
   /* only checks for address equals here */
-  if (Collator::operator==(that))
+  if (this == &that) {
     return TRUE;
-
-  if (typeid(*this) != typeid(that))
+  }
+  if (!Collator::operator==(that)) {
     return FALSE;  /* not the same class */
+  }
 
   RuleBasedCollator& thatAlias = (RuleBasedCollator&)that;
 
-  // weiv: use C function, commented code below is wrong
   return ucol_equals(this->ucollator, thatAlias.ucollator);
-  /*
-  synwee : orginal code does not check for data compatibility
-  */
-  /*
-  if (ucollator != thatAlias.ucollator)
-    return FALSE;
-
-  return TRUE;
-  */
-}
-
-UBool RuleBasedCollator::operator!=(const Collator& other) const
-{
-    return !(*this == other);
 }
 
 // aliasing, not write-through
@@ -333,15 +317,9 @@ void RuleBasedCollator::getVersion(UVersionInfo versionInfo) const
     }
 }
 
-Collator::EComparisonResult RuleBasedCollator::compare(
-                                               const UnicodeString& source,
-                                               const UnicodeString& target,
-                                               int32_t length) const
-{
-    UErrorCode status = U_ZERO_ERROR;
-    return getEComparisonResult(compare(source.getBuffer(), uprv_min(length,source.length()), target.getBuffer(), uprv_min(length,target.length()), status));
-}
-
+/**
+* Compare two strings using this collator
+*/
 UCollationResult RuleBasedCollator::compare(
                                                const UnicodeString& source,
                                                const UnicodeString& target,
@@ -351,16 +329,6 @@ UCollationResult RuleBasedCollator::compare(
     return compare(source.getBuffer(), uprv_min(length,source.length()), target.getBuffer(), uprv_min(length,target.length()), status);
 }
 
-Collator::EComparisonResult RuleBasedCollator::compare(const UChar* source,
-                                                       int32_t sourceLength,
-                                                       const UChar* target,
-                                                       int32_t targetLength)
-                                                       const
-{
-    return  getEComparisonResult(ucol_strcoll(ucollator, source, sourceLength,
-                                                         target, targetLength));
-}
-
 UCollationResult RuleBasedCollator::compare(const UChar* source,
                                                        int32_t sourceLength,
                                                        const UChar* target,
@@ -374,17 +342,6 @@ UCollationResult RuleBasedCollator::compare(const UChar* source,
     }
 }
 
-/**
-* Compare two strings using this collator
-*/
-Collator::EComparisonResult RuleBasedCollator::compare(
-                                             const UnicodeString& source,
-                                             const UnicodeString& target) const
-{
-    return getEComparisonResult(ucol_strcoll(ucollator, source.getBuffer(), source.length(),
-                                                        target.getBuffer(), target.length()));
-}
-
 UCollationResult RuleBasedCollator::compare(
                                              const UnicodeString& source,
                                              const UnicodeString& target,
@@ -536,7 +493,7 @@ void RuleBasedCollator::setAttribute(UColAttribute attr,
 }
 
 UColAttributeValue RuleBasedCollator::getAttribute(UColAttribute attr,
-                                                      UErrorCode &status)
+                                                      UErrorCode &status) const
 {
     if (U_FAILURE(status))
         return UCOL_DEFAULT;
@@ -548,12 +505,12 @@ uint32_t RuleBasedCollator::setVariableTop(const UChar *varTop, int32_t len, UEr
     return ucol_setVariableTop(ucollator, varTop, len, &status);
 }
 
-uint32_t RuleBasedCollator::setVariableTop(const UnicodeString varTop, UErrorCode &status) {
+uint32_t RuleBasedCollator::setVariableTop(const UnicodeString &varTop, UErrorCode &status) {
     checkOwned();
     return ucol_setVariableTop(ucollator, varTop.getBuffer(), varTop.length(), &status);
 }
 
-void RuleBasedCollator::setVariableTop(const uint32_t varTop, UErrorCode &status) {
+void RuleBasedCollator::setVariableTop(uint32_t varTop, UErrorCode &status) {
     checkOwned();
     ucol_restoreVariableTop(ucollator, varTop, &status);
 }
@@ -562,7 +519,7 @@ uint32_t RuleBasedCollator::getVariableTop(UErrorCode &status) const {
   return ucol_getVariableTop(ucollator, &status);
 }
 
-Collator* RuleBasedCollator::safeClone(void)
+Collator* RuleBasedCollator::safeClone(void) const
 {
     UErrorCode intStatus = U_ZERO_ERROR;
     int32_t buffersize = U_COL_SAFECLONE_BUFFERSIZE;
@@ -575,10 +532,10 @@ Collator* RuleBasedCollator::safeClone(void)
     RuleBasedCollator *result = new RuleBasedCollator();
     // Null pointer check
     if (result != NULL) {
-           result->ucollator = ucol;
-           result->dataIsOwned = TRUE;
-           result->isWriteThroughAlias = FALSE;
-           setRuleStringFromCollator();
+        result->ucollator = ucol;
+        result->dataIsOwned = TRUE;
+        result->isWriteThroughAlias = FALSE;
+        result->setRuleStringFromCollator();
     }
 
     return result;
@@ -599,21 +556,6 @@ int32_t RuleBasedCollator::getSortKey(const UChar *source,
     return ucol_getSortKey(ucollator, source, sourceLength, result, resultLength);
 }
 
-Collator::ECollationStrength RuleBasedCollator::getStrength(void) const
-{
-    UErrorCode intStatus = U_ZERO_ERROR;
-    return getECollationStrength(ucol_getAttribute(ucollator, UCOL_STRENGTH,
-                                &intStatus));
-}
-
-void RuleBasedCollator::setStrength(ECollationStrength newStrength)
-{
-    checkOwned();
-    UErrorCode intStatus = U_ZERO_ERROR;
-    UCollationStrength strength = getUCollationStrength(newStrength);
-    ucol_setAttribute(ucollator, UCOL_STRENGTH, strength, &intStatus);
-}
-
 int32_t RuleBasedCollator::getReorderCodes(int32_t *dest,
                                           int32_t destCapacity,
                                           UErrorCode& status) const
@@ -651,7 +593,7 @@ int32_t RuleBasedCollator::hashCode() const
 /**
 * return the locale of this collator
 */
-const Locale RuleBasedCollator::getLocale(ULocDataLocaleType type, UErrorCode &status) const {
+Locale RuleBasedCollator::getLocale(ULocDataLocaleType type, UErrorCode &status) const {
     const char *result = ucol_getLocaleByType(ucollator, type, &status);
     if(result == NULL) {
         Locale res("");
index 93dbb722572196ad369292f76ddd4bca17b64da4..b1d802809cd03cf2ee15bcb5573de4bd30ea1a8e 100644 (file)
@@ -206,11 +206,11 @@ public:
      */
     enum ECollationStrength
     {
-        PRIMARY    = 0,
-        SECONDARY  = 1,
-        TERTIARY   = 2,
-        QUATERNARY = 3,
-        IDENTICAL  = 15
+        PRIMARY    = UCOL_PRIMARY,  // 0
+        SECONDARY  = UCOL_SECONDARY,  // 1
+        TERTIARY   = UCOL_TERTIARY,  // 2
+        QUATERNARY = UCOL_QUATERNARY,  // 3
+        IDENTICAL  = UCOL_IDENTICAL  // 15
     };
 
     /**
@@ -225,9 +225,9 @@ public:
      */
     enum EComparisonResult
     {
-        LESS = -1,
-        EQUAL = 0,
-        GREATER = 1
+        LESS = UCOL_LESS,  // -1
+        EQUAL = UCOL_EQUAL,  // 0
+        GREATER = UCOL_GREATER  // 1
     };
 
     // Collator public destructor -----------------------------------------
@@ -241,17 +241,30 @@ public:
     // Collator public methods --------------------------------------------
 
     /**
-     * Returns true if "other" is the same as "this"
+     * Returns TRUE if "other" is the same as "this".
+     *
+     * The base class implementation returns TRUE if "other" has the same type/class as "this":
+     * <code>typeid(*this) == typeid(other)</code>.
+     *
+     * Subclass implementations should do something like the following:
+     * <pre>
+     *   if (this == &other) { return TRUE; }
+     *   if (!Collator::operator==(other)) { return FALSE; }  // not the same class
+     *
+     *   const MyCollator &o = (const MyCollator&)other;
+     *   (compare this vs. o's subclass fields)
+     * </pre>
      * @param other Collator object to be compared
-     * @return true if other is the same as this.
+     * @return TRUE if other is the same as this.
      * @stable ICU 2.0
      */
     virtual UBool operator==(const Collator& other) const;
 
     /**
      * Returns true if "other" is not the same as "this".
+     * Calls !Collator::operator==(other) which works for all subclasses.
      * @param other Collator object to be compared
-     * @return true if other is not the same as this.
+     * @return TRUE if other is not the same as this.
      * @stable ICU 2.0
      */
     virtual UBool operator!=(const Collator& other) const;
@@ -404,6 +417,24 @@ public:
      * The comparison function compares the character data stored in two
      * different string arrays. Returns information about whether a string array
      * is less than, greater than or equal to another string array.
+     * <p>Example of use:
+     * <pre>
+     * .       UChar ABC[] = {0x41, 0x42, 0x43, 0};  // = "ABC"
+     * .       UChar abc[] = {0x61, 0x62, 0x63, 0};  // = "abc"
+     * .       UErrorCode status = U_ZERO_ERROR;
+     * .       Collator *myCollation =
+     * .                         Collator::createInstance(Locale::US, status);
+     * .       if (U_FAILURE(status)) return;
+     * .       myCollation->setStrength(Collator::PRIMARY);
+     * .       // result would be Collator::EQUAL ("abc" == "ABC")
+     * .       // (no primary difference between "abc" and "ABC")
+     * .       Collator::EComparisonResult result =
+     * .                             myCollation->compare(abc, 3, ABC, 3);
+     * .       myCollation->setStrength(Collator::TERTIARY);
+     * .       // result would be Collator::LESS ("abc" &lt;&lt;&lt; "ABC")
+     * .       // (with tertiary difference between "abc" and "ABC")
+     * .       result = myCollation->compare(abc, 3, ABC, 3);
+     * </pre>
      * @param source the source string array to be compared with.
      * @param sourceLength the length of the source string array.  If this value
      *        is equal to -1, the string array is null-terminated.
@@ -528,7 +559,7 @@ public:
      * @deprecated ICU 2.8 This API is under consideration for revision
      * in ICU 3.0.
      */
-    virtual const Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const = 0;
+    virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const = 0;
 
     /**
      * Convenience method for comparing two strings based on the collation rules.
@@ -566,7 +597,7 @@ public:
     UBool equals(const UnicodeString& source, const UnicodeString& target) const;
 
     /**
-     * Determines the minimum strength that will be use in comparison or
+     * Determines the minimum strength that will be used in comparison or
      * transformation.
      * <p>E.g. with strength == SECONDARY, the tertiary difference is ignored
      * <p>E.g. with strength == PRIMARY, the secondary and tertiary difference
@@ -575,7 +606,7 @@ public:
      * @see Collator#setStrength
      * @deprecated ICU 2.6 Use getAttribute(UCOL_STRENGTH...) instead
      */
-    virtual ECollationStrength getStrength(void) const = 0;
+    virtual ECollationStrength getStrength(void) const;
 
     /**
      * Sets the minimum strength to be used in comparison or transformation.
@@ -595,7 +626,7 @@ public:
      * @param newStrength the new comparison level.
      * @deprecated ICU 2.6 Use setAttribute(UCOL_STRENGTH...) instead
      */
-    virtual void setStrength(ECollationStrength newStrength) = 0;
+    virtual void setStrength(ECollationStrength newStrength);
 
     /**
      * Retrieves the reordering codes for this collator.
@@ -612,9 +643,9 @@ public:
      * @see UColReorderCode
      * @stable ICU 4.8 
      */
-     virtual int32_t U_EXPORT2 getReorderCodes(int32_t *dest,
-                                    int32_t destCapacity,
-                                    UErrorCode& status) const;
+     virtual int32_t getReorderCodes(int32_t *dest,
+                                     int32_t destCapacity,
+                                     UErrorCode& status) const;
 
     /**
      * Sets the ordering of scripts for this collator.
@@ -630,9 +661,9 @@ public:
      * @see UColReorderCode
      * @stable ICU 4.8 
      */
-     virtual void U_EXPORT2 setReorderCodes(const int32_t* reorderCodes,
-                                int32_t reorderCodesLength,
-                                UErrorCode& status) ;
+     virtual void setReorderCodes(const int32_t* reorderCodes,
+                                  int32_t reorderCodesLength,
+                                  UErrorCode& status) ;
 
     /**
      * Retrieves the reorder codes that are grouped with the given reorder code. Some reorder
@@ -849,7 +880,7 @@ public:
      * @stable ICU 2.2
      */
     virtual UColAttributeValue getAttribute(UColAttribute attr,
-                                            UErrorCode &status) = 0;
+                                            UErrorCode &status) const = 0;
 
     /**
      * Sets the variable top to a collation element value of a string supplied.
@@ -872,7 +903,7 @@ public:
      * @return a 32 bit value containing the value of the variable top in upper 16 bits. Lower 16 bits are undefined
      * @stable ICU 2.0
      */
-    virtual uint32_t setVariableTop(const UnicodeString varTop, UErrorCode &status) = 0;
+    virtual uint32_t setVariableTop(const UnicodeString &varTop, UErrorCode &status) = 0;
 
     /**
      * Sets the variable top to a collation element value supplied. Variable top is set to the upper 16 bits.
@@ -881,7 +912,7 @@ public:
      * @param status error code (not changed by function)
      * @stable ICU 2.0
      */
-    virtual void setVariableTop(const uint32_t varTop, UErrorCode &status) = 0;
+    virtual void setVariableTop(uint32_t varTop, UErrorCode &status) = 0;
 
     /**
      * Gets the variable top value of a Collator.
@@ -908,7 +939,7 @@ public:
      * @return pointer to the new clone, user should remove it.
      * @stable ICU 2.2
      */
-    virtual Collator* safeClone(void) = 0;
+    virtual Collator* safeClone(void) const = 0;
 
     /**
      * Get the sort key as an array of bytes from an UnicodeString.
index e4dcee9fe9143772984d054c73c45786a8235007..f08ee599ecf3ea185bb01ff19dda583b3c74b6d5 100644 (file)
@@ -223,14 +223,6 @@ public:
      */
     virtual UBool operator==(const Collator& other) const;
 
-    /**
-     * Returns true if argument is not the same as this object.
-     * @param other Collator object to be compared
-     * @return returns true if argument is not the same as this object.
-     * @stable ICU 2.0
-     */
-    virtual UBool operator!=(const Collator& other) const;
-
     /**
      * Makes a deep copy of the object.
      * The caller owns the returned object.
@@ -264,21 +256,8 @@ public:
     virtual CollationElementIterator* createCollationElementIterator(
                                          const CharacterIterator& source) const;
 
-    /**
-     * Compares a range of character data stored in two different strings based
-     * on the collation rules. Returns information about whether a string is
-     * less than, greater than or equal to another string in a language.
-     * This can be overriden in a subclass.
-     * @param source the source string.
-     * @param target the target string to be compared with the source string.
-     * @return the comparison result. GREATER if the source string is greater
-     *         than the target string, LESS if the source is less than the
-     *         target. Otherwise, returns EQUAL.
-     * @deprecated ICU 2.6 Use overload with UErrorCode&
-     */
-    virtual EComparisonResult compare(const UnicodeString& source,
-                                      const UnicodeString& target) const;
-
+    // Make deprecated versions of Collator::compare() visible.
+    using Collator::compare;
 
     /**
     * The comparison function compares the character data stored in two
@@ -296,23 +275,6 @@ public:
                                       const UnicodeString& target,
                                       UErrorCode &status) const;
 
-    /**
-     * Compares a range of character data stored in two different strings based
-     * on the collation rules up to the specified length. Returns information
-     * about whether a string is less than, greater than or equal to another
-     * string in a language. This can be overriden in a subclass.
-     * @param source the source string.
-     * @param target the target string to be compared with the source string.
-     * @param length compares up to the specified length
-     * @return the comparison result. GREATER if the source string is greater
-     *         than the target string, LESS if the source is less than the
-     *         target. Otherwise, returns EQUAL.
-     * @deprecated ICU 2.6 Use overload with UErrorCode&
-     */
-    virtual EComparisonResult compare(const UnicodeString& source,
-                                      const UnicodeString&  target,
-                                      int32_t length) const;
-
     /**
     * Does the same thing as compare but limits the comparison to a specified 
     * length
@@ -331,43 +293,6 @@ public:
                                       int32_t length,
                                       UErrorCode &status) const;
 
-    /**
-     * The comparison function compares the character data stored in two
-     * different string arrays. Returns information about whether a string array
-     * is less than, greater than or equal to another string array.
-     * <p>Example of use:
-     * <pre>
-     * .       UChar ABC[] = {0x41, 0x42, 0x43, 0};  // = "ABC"
-     * .       UChar abc[] = {0x61, 0x62, 0x63, 0};  // = "abc"
-     * .       UErrorCode status = U_ZERO_ERROR;
-     * .       Collator *myCollation =
-     * .                         Collator::createInstance(Locale::US, status);
-     * .       if (U_FAILURE(status)) return;
-     * .       myCollation->setStrength(Collator::PRIMARY);
-     * .       // result would be Collator::EQUAL ("abc" == "ABC")
-     * .       // (no primary difference between "abc" and "ABC")
-     * .       Collator::EComparisonResult result =
-     * .                             myCollation->compare(abc, 3, ABC, 3);
-     * .       myCollation->setStrength(Collator::TERTIARY);
-     * .       // result would be Collator::LESS ("abc" &lt;&lt;&lt; "ABC")
-     * .       // (with tertiary difference between "abc" and "ABC")
-     * .       result =  myCollation->compare(abc, 3, ABC, 3);
-     * </pre>
-     * @param source the source string array to be compared with.
-     * @param sourceLength the length of the source string array. If this value
-     *        is equal to -1, the string array is null-terminated.
-     * @param target the string that is to be compared with the source string.
-     * @param targetLength the length of the target string array. If this value
-     *        is equal to -1, the string array is null-terminated.
-     * @return Returns a byte value. GREATER if source is greater than target;
-     *         EQUAL if source is equal to target; LESS if source is less than
-     *         target
-     * @deprecated ICU 2.6 Use overload with UErrorCode&
-     */
-    virtual EComparisonResult compare(const UChar* source, int32_t sourceLength,
-                                      const UChar* target, int32_t targetLength)
-                                      const;
-
     /**
     * The comparison function compares the character data stored in two
     * different string arrays. Returns information about whether a string array 
@@ -454,7 +379,7 @@ public:
     *         was instantiated from rules, locale is empty.
     * @deprecated ICU 2.8 likely to change in ICU 3.0, based on feedback
     */
-    virtual const Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
+    virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
 
     /**
      * Gets the table-based rules for the collation object.
@@ -558,7 +483,7 @@ public:
      * @stable ICU 2.2
      */
     virtual UColAttributeValue getAttribute(UColAttribute attr,
-                                            UErrorCode &status);
+                                            UErrorCode &status) const;
 
     /**
      * Sets the variable top to a collation element value of a string supplied.
@@ -581,7 +506,7 @@ public:
      * @return a 32 bit value containing the value of the variable top in upper 16 bits. Lower 16 bits are undefined
      * @stable ICU 2.0
      */
-    virtual uint32_t setVariableTop(const UnicodeString varTop, UErrorCode &status);
+    virtual uint32_t setVariableTop(const UnicodeString &varTop, UErrorCode &status);
 
     /**
      * Sets the variable top to a collation element value supplied. Variable top is set to the upper 16 bits.
@@ -590,7 +515,7 @@ public:
      * @param status error code (not changed by function)
      * @stable ICU 2.0
      */
-    virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
+    virtual void setVariableTop(uint32_t varTop, UErrorCode &status);
 
     /**
      * Gets the variable top value of a Collator.
@@ -616,7 +541,7 @@ public:
      * @return pointer to the new clone, user should remove it.
      * @stable ICU 2.2
      */
-    virtual Collator* safeClone(void);
+    virtual Collator* safeClone(void) const;
 
     /**
      * Get the sort key as an array of bytes from an UnicodeString.
@@ -646,26 +571,6 @@ public:
     virtual int32_t getSortKey(const UChar *source, int32_t sourceLength,
                                uint8_t *result, int32_t resultLength) const;
 
-    /**
-    * Determines the minimum strength that will be use in comparison or
-    * transformation.
-    * <p>E.g. with strength == SECONDARY, the tertiary difference is ignored
-    * <p>E.g. with strength == PRIMARY, the secondary and tertiary difference
-    * are ignored.
-    * @return the current comparison level.
-    * @see RuleBasedCollator#setStrength
-    * @deprecated ICU 2.6 Use getAttribute(UCOL_STRENGTH...) instead
-    */
-    virtual ECollationStrength getStrength(void) const;
-
-    /**
-    * Sets the minimum strength to be used in comparison or transformation.
-    * @see RuleBasedCollator#getStrength
-    * @param newStrength the new comparison level.
-    * @deprecated ICU 2.6 Use setAttribute(UCOL_STRENGTH...) instead
-    */
-    virtual void setStrength(ECollationStrength newStrength);
-
     /**
      * Retrieves the reordering codes for this collator.
      * @param dest The array to fill with the script ordering.
@@ -679,9 +584,9 @@ public:
      * @see Collator#setReorderCodes
      * @stable ICU 4.8 
      */
-     virtual int32_t U_EXPORT2 getReorderCodes(int32_t *dest,
-                                    int32_t destCapacity,
-                                    UErrorCode& status) const;
+     virtual int32_t getReorderCodes(int32_t *dest,
+                                     int32_t destCapacity,
+                                     UErrorCode& status) const;
 
     /**
      * Sets the ordering of scripts for this collator.
@@ -693,9 +598,9 @@ public:
      * @see Collator#getEquivalentReorderCodes
      * @stable ICU 4.8 
      */
-     virtual void U_EXPORT2 setReorderCodes(const int32_t* reorderCodes,
-                                int32_t reorderCodesLength,
-                                UErrorCode& status) ;
+     virtual void setReorderCodes(const int32_t* reorderCodes,
+                                  int32_t reorderCodesLength,
+                                  UErrorCode& status) ;
 
     /**
      * Retrieves the reorder codes that are grouped with the given reorder code. Some reorder
@@ -878,38 +783,13 @@ protected:
     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
 
 private:
-
     // if not owned and not a write through alias, copy the ucollator
     void checkOwned(void);
 
     // utility to init rule string used by checkOwned and construct
     void setRuleStringFromCollator();
 
-    /**
-    * Converts C's UCollationResult to EComparisonResult
-    * @param result member of the enum UComparisonResult
-    * @return EComparisonResult equivalent of UCollationResult
-    * @deprecated ICU 2.6. We will not need it.
-    */
-    Collator::EComparisonResult getEComparisonResult(
-                                            const UCollationResult &result) const;
-
-    /**
-    * Converts C's UCollationStrength to ECollationStrength
-    * @param strength member of the enum UCollationStrength
-    * @return ECollationStrength equivalent of UCollationStrength
-    */
-    Collator::ECollationStrength getECollationStrength(
-                                        const UCollationStrength &strength) const;
-
-    /**
-    * Converts C++'s ECollationStrength to UCollationStrength
-    * @param strength member of the enum ECollationStrength
-    * @return UCollationStrength equivalent of ECollationStrength
-    */
-    UCollationStrength getUCollationStrength(
-      const Collator::ECollationStrength &strength) const;
- public:
+public:
     /** Get the short definition string for a collator. This internal API harvests the collator's
      *  locale and the attribute set and produces a string that can be used for opening 
      *  a collator with the same properties using the ucol_openFromShortString API.
@@ -967,56 +847,6 @@ inline const UCollator * RuleBasedCollator::getUCollator()
 }
 #endif
 
-inline Collator::EComparisonResult RuleBasedCollator::getEComparisonResult(
-                                           const UCollationResult &result) const
-{
-    switch (result)
-    {
-    case UCOL_LESS :
-        return Collator::LESS;
-    case UCOL_EQUAL :
-        return Collator::EQUAL;
-    default :
-        return Collator::GREATER;
-    }
-}
-
-inline Collator::ECollationStrength RuleBasedCollator::getECollationStrength(
-                                       const UCollationStrength &strength) const
-{
-    switch (strength)
-    {
-    case UCOL_PRIMARY :
-        return Collator::PRIMARY;
-    case UCOL_SECONDARY :
-        return Collator::SECONDARY;
-    case UCOL_TERTIARY :
-        return Collator::TERTIARY;
-    case UCOL_QUATERNARY :
-        return Collator::QUATERNARY;
-    default :
-        return Collator::IDENTICAL;
-    }
-}
-
-inline UCollationStrength RuleBasedCollator::getUCollationStrength(
-                             const Collator::ECollationStrength &strength) const
-{
-    switch (strength)
-    {
-    case Collator::PRIMARY :
-        return UCOL_PRIMARY;
-    case Collator::SECONDARY :
-        return UCOL_SECONDARY;
-    case Collator::TERTIARY :
-        return UCOL_TERTIARY;
-    case Collator::QUATERNARY :
-        return UCOL_QUATERNARY;
-    default :
-        return UCOL_IDENTICAL;
-    }
-}
-
 U_NAMESPACE_END
 
 #endif /* #if !UCONFIG_NO_COLLATION */
index 7c998e5db5aa542621c784912c76a5bf777e7a5a..3f31a4d971cad1b240f6aef6f7fbbca88ec18e72 100644 (file)
@@ -1,6 +1,6 @@
 /********************************************************************
  * COPYRIGHT:
- * Copyright (c) 1997-2011, International Business Machines Corporation and
+ * Copyright (c) 1997-2012, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 //===============================================================================
@@ -1902,32 +1902,7 @@ class TestCollator  : public Collator
 public:
     virtual Collator* clone(void) const;
 
-    // dang, markus says we can't use 'using' in ICU.  I hate doing this for
-    // deprecated methods...
-
-    // using Collator::compare;
-
-    virtual EComparisonResult compare(const UnicodeString& source, 
-                                      const UnicodeString& target) const
-    {
-        return Collator::compare(source, target);
-    }
-
-    virtual EComparisonResult compare(const UnicodeString& source,
-                                      const UnicodeString& target,
-                                      int32_t length) const
-    {
-        return Collator::compare(source, target, length);
-    }
-
-    virtual EComparisonResult compare(const UChar* source, 
-                                      int32_t sourceLength, 
-                                      const UChar* target, 
-                                      int32_t targetLength) const
-    {
-        return Collator::compare(source, sourceLength, target, targetLength);
-    }
-
+    using Collator::compare;
 
     virtual UCollationResult compare(const UnicodeString& source, 
                                       const UnicodeString& target,
@@ -1949,8 +1924,7 @@ public:
                                           CollationKey& key,
                                           UErrorCode& status) const;
     virtual int32_t hashCode(void) const;
-    virtual const Locale getLocale(ULocDataLocaleType type, 
-                                   UErrorCode& status) const;
+    virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
     virtual ECollationStrength getStrength(void) const;
     virtual void setStrength(ECollationStrength newStrength);
     virtual UClassID getDynamicClassID(void) const;
@@ -1958,35 +1932,39 @@ public:
     virtual void setAttribute(UColAttribute attr, UColAttributeValue value, 
                               UErrorCode &status);
     virtual UColAttributeValue getAttribute(UColAttribute attr, 
-                                            UErrorCode &status);
+                                            UErrorCode &status) const;
     virtual uint32_t setVariableTop(const UChar *varTop, int32_t len, 
                                     UErrorCode &status);
-    virtual uint32_t setVariableTop(const UnicodeString varTop, 
+    virtual uint32_t setVariableTop(const UnicodeString &varTop, 
                                     UErrorCode &status);
-    virtual void setVariableTop(const uint32_t varTop, UErrorCode &status);
+    virtual void setVariableTop(uint32_t varTop, UErrorCode &status);
     virtual uint32_t getVariableTop(UErrorCode &status) const;
-    virtual Collator* safeClone(void);
+    virtual Collator* safeClone(void) const;
     virtual int32_t getSortKey(const UnicodeString& source,
                             uint8_t* result,
                             int32_t resultLength) const;
     virtual int32_t getSortKey(const UChar*source, int32_t sourceLength,
                              uint8_t*result, int32_t resultLength) const;
     virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
-    virtual UBool operator!=(const Collator& other) const;
+    virtual UBool operator==(const Collator& other) const;
+    // Collator::operator!= calls !Collator::operator== which works for all subclasses.
     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
     TestCollator() : Collator() {};
     TestCollator(UCollationStrength collationStrength, 
            UNormalizationMode decompositionMode) : Collator(collationStrength, decompositionMode) {};
 };
 
-inline UBool TestCollator::operator!=(const Collator& other) const {
-    return Collator::operator!=(other);
-}
+inline UBool TestCollator::operator==(const Collator& other) const {
+    // TestCollator has no fields, so we test for identity.
+    return this == &other;
 
-#define returnEComparisonResult(data) \
-    if (data < 0) return Collator::LESS;\
-    if (data > 0) return Collator::GREATER;\
-    return Collator::EQUAL;
+    // Normally, subclasses should do something like the following:
+    //    if (this == &other) { return TRUE; }
+    //    if (!Collator::operator==(other)) { return FALSE; }  // not the same class
+    //
+    //    const TestCollator &o = (const TestCollator&)other;
+    //    (compare this vs. o's subclass fields)
+}
 
 Collator* TestCollator::clone() const
 {
@@ -2072,8 +2050,7 @@ int32_t TestCollator::hashCode() const
     return 0;
 }
 
-const Locale TestCollator::getLocale(ULocDataLocaleType type, 
-                                     UErrorCode& status) const
+Locale TestCollator::getLocale(ULocDataLocaleType type, UErrorCode& status) const
 {
     // api not used, this is to make the compiler happy
     if (U_FAILURE(status)) {
@@ -2115,7 +2092,7 @@ void TestCollator::setAttribute(UColAttribute attr, UColAttributeValue value,
 }
 
 UColAttributeValue TestCollator::getAttribute(UColAttribute attr, 
-                                              UErrorCode &status)
+                                              UErrorCode &status) const
 {
     // api not used, this is to make the compiler happy
     if (U_FAILURE(status) || attr == UCOL_ATTRIBUTE_COUNT) {
@@ -2134,7 +2111,7 @@ uint32_t TestCollator::setVariableTop(const UChar *varTop, int32_t len,
     return 0;
 }
 
-uint32_t TestCollator::setVariableTop(const UnicodeString varTop, 
+uint32_t TestCollator::setVariableTop(const UnicodeString &varTop, 
                                   UErrorCode &status)
 {
     // api not used, this is to make the compiler happy
@@ -2144,7 +2121,7 @@ uint32_t TestCollator::setVariableTop(const UnicodeString varTop,
     return 0;
 }
 
-void TestCollator::setVariableTop(const uint32_t varTop, UErrorCode &status)
+void TestCollator::setVariableTop(uint32_t varTop, UErrorCode &status)
 {
     // api not used, this is to make the compiler happy
     if (U_SUCCESS(status) && varTop == 0) {
@@ -2162,7 +2139,7 @@ uint32_t TestCollator::getVariableTop(UErrorCode &status) const
     return (uint32_t)(0xFFFFFFFFu);
 }
 
-Collator* TestCollator::safeClone(void)
+Collator* TestCollator::safeClone(void) const
 {
     return new TestCollator();
 }
@@ -2182,7 +2159,7 @@ void CollationAPITest::TestSubclass()
 {
     TestCollator col1;
     TestCollator col2;
-    doAssert(col1 != col2, "2 instance of TestCollator should be different");
+    doAssert(col1 != col2, "2 instances of TestCollator should be different");
     if (col1.hashCode() != col2.hashCode()) {
         errln("Every TestCollator has the same hashcode");
     }