1 /* 2 ******************************************************************************* 3 * 4 * Copyright (C) 2012-2013, International Business Machines 5 * Corporation and others. All Rights Reserved. 6 * 7 ******************************************************************************* 8 * file name: listformatter.h 9 * encoding: US-ASCII 10 * tab size: 8 (not used) 11 * indentation:4 12 * 13 * created on: 20120426 14 * created by: Umesh P. Nair 15 */ 16 17 #ifndef __LISTFORMATTER_H__ 18 #define __LISTFORMATTER_H__ 19 20 #include "unicode/utypes.h" 21 22 #ifndef U_HIDE_DRAFT_API 23 24 #include "unicode/unistr.h" 25 #include "unicode/locid.h" 26 27 U_NAMESPACE_BEGIN 28 29 /** @internal */ 30 class Hashtable; 31 32 /** @internal */ 33 struct ListFormatData : public UMemory { 34 UnicodeString twoPattern; 35 UnicodeString startPattern; 36 UnicodeString middlePattern; 37 UnicodeString endPattern; 38 ListFormatDataListFormatData39 ListFormatData(const UnicodeString& two, const UnicodeString& start, const UnicodeString& middle, const UnicodeString& end) : 40 twoPattern(two), startPattern(start), middlePattern(middle), endPattern(end) {} 41 }; 42 43 44 /** 45 * \file 46 * \brief C++ API: API for formatting a list. 47 */ 48 49 50 /** 51 * An immutable class for formatting a list, using data from CLDR (or supplied 52 * separately). 53 * 54 * Example: Input data ["Alice", "Bob", "Charlie", "Delta"] will be formatted 55 * as "Alice, Bob, Charlie and Delta" in English. 56 * 57 * The ListFormatter class is not intended for public subclassing. 58 * @draft ICU 50 59 */ 60 class U_COMMON_API ListFormatter : public UObject{ 61 62 public: 63 /** 64 * Creates a ListFormatter appropriate for the default locale. 65 * 66 * @param errorCode ICU error code, set if no data available for default locale. 67 * @return Pointer to a ListFormatter object for the default locale, 68 * created from internal data derived from CLDR data. 69 * @draft ICU 50 70 */ 71 static ListFormatter* createInstance(UErrorCode& errorCode); 72 73 /** 74 * Creates a ListFormatter appropriate for a locale. 75 * 76 * @param locale The locale. 77 * @param errorCode ICU error code, set if no data available for the given locale. 78 * @return A ListFormatter object created from internal data derived from 79 * CLDR data. 80 * @draft ICU 50 81 */ 82 static ListFormatter* createInstance(const Locale& locale, UErrorCode& errorCode); 83 84 85 /** 86 * Destructor. 87 * 88 * @draft ICU 50 89 */ 90 virtual ~ListFormatter(); 91 92 93 /** 94 * Formats a list of strings. 95 * 96 * @param items An array of strings to be combined and formatted. 97 * @param n_items Length of the array items. 98 * @param appendTo The string to which the result should be appended to. 99 * @param errorCode ICU error code, set if there is an error. 100 * @return Formatted string combining the elements of items, appended to appendTo. 101 * @draft ICU 50 102 */ 103 UnicodeString& format(const UnicodeString items[], int32_t n_items, 104 UnicodeString& appendTo, UErrorCode& errorCode) const; 105 106 /** 107 * @internal constructor made public for testing. 108 */ 109 ListFormatter(const ListFormatData& listFormatterData); 110 111 private: 112 static void initializeHash(UErrorCode& errorCode); 113 static const ListFormatData* getListFormatData(const Locale& locale, UErrorCode& errorCode); 114 115 ListFormatter(); 116 ListFormatter(const ListFormatter&); 117 118 ListFormatter& operator = (const ListFormatter&); 119 void addNewString(const UnicodeString& pattern, UnicodeString& originalString, 120 const UnicodeString& newString, UErrorCode& errorCode) const; 121 122 const ListFormatData& data; 123 }; 124 125 U_NAMESPACE_END 126 127 #endif /* U_HIDE_DRAFT_API */ 128 #endif 129