Refactoy Locales for DateTimeFormatter.
[platform/framework/native/appfw.git] / src / locales / FLcl_DateTimeSymbolsImpl.cpp
index fe337ed..6216283 100644 (file)
@@ -75,24 +75,19 @@ _DateTimeSymbolsImpl::Initialize(const Locale& locale, CalendarType calendarType
 {
        SysTryReturnResult(NID_LCL, _LocaleImpl::IsSupported(locale), E_INVALID_ARG, "Given locale is not supported");
 
-       _LocaleData localeData;
-       result r = localeData.SetDateFormatSymbols(locale);   // this will set ICU DateTimeSymbol in _LocaleData object for future use
-
-       if (!IsFailed(r))
-       {
-               __pErasList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_ERA_LIST);
-               __pMonthsList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_MONTH_LIST);
-               __pShortMonthsList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_SHORT_MONTH_LIST);
-               __pWeekdaysList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_WEEKDAY_LIST);
-               __pShortWeekdaysList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_SHORT_WEEKDAY_LIST);
-               __pAmPmList = localeData.GetDateFormatSymbolAttrArrayN(DATE_FORMAT_SYM_AM_PM_LIST);
-               __CalendarType = calendarType;
-
-               UErrorCode ec = U_ZERO_ERROR;
-               __pIcuDateFormatSymbols = new IcuDateFormatSymbols(ec);
-       }
-
-       return r;
+       UErrorCode ec = U_ZERO_ERROR;
+       __pIcuDateFormatSymbols = new U_ICU_NAMESPACE::DateFormatSymbols(_LocaleData::GetIcuLocale(locale), ec);
+       SysTryReturnResult(NID_LCL, __pIcuDateFormatSymbols, E_OUT_OF_MEMORY, "It is not enough memory.");
+
+       __pErasList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_ERA_LIST);
+       __pMonthsList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_MONTH_LIST);
+       __pShortMonthsList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_SHORT_MONTH_LIST);
+       __pWeekdaysList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_WEEKDAY_LIST);
+       __pShortWeekdaysList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_SHORT_WEEKDAY_LIST);
+       __pAmPmList = GetSymbolAttributeArrayN(DATE_FORMAT_SYM_AM_PM_LIST);
+       __CalendarType = calendarType;
+
+       return E_SUCCESS;
 }
 
 void
@@ -100,18 +95,28 @@ _DateTimeSymbolsImpl::Set(const _DateTimeSymbolsImpl* pOther)
 {
        SysTryReturnVoidResult(NID_LCL, pOther != null, E_INVALID_ARG,
                                "[%s] Invalid argument is used. pOther instance is invalid", GetErrorMessage(E_INVALID_ARG));
+       ReleaseAll();
 
-       __pErasList = CloneArrayListN(pOther->__pErasList);                         // Copying EraList
-       __pMonthsList = CloneArrayListN(pOther->__pMonthsList);                     // Copying Month List
-       __pShortMonthsList = CloneArrayListN(pOther->__pShortMonthsList);           // Copying Short Month List
-       __pWeekdaysList = CloneArrayListN(pOther->__pWeekdaysList);                 // Copying Weekdays List
-       __pShortWeekdaysList = CloneArrayListN(pOther->__pShortWeekdaysList);       // Copying Short Weekdays List
-       __pAmPmList = CloneArrayListN(pOther->__pAmPmList);                         // Copying AM/ PM String
+       __pErasList = CloneArrayListN(pOther->__pErasList);
+       __pMonthsList = CloneArrayListN(pOther->__pMonthsList);
+       __pShortMonthsList = CloneArrayListN(pOther->__pShortMonthsList);
+       __pWeekdaysList = CloneArrayListN(pOther->__pWeekdaysList);
+       __pShortWeekdaysList = CloneArrayListN(pOther->__pShortWeekdaysList);
+       __pAmPmList = CloneArrayListN(pOther->__pAmPmList);
        if(pOther->__pTimeZonesMap)
        {
-               __pTimeZonesMap = CloneMultiHashMapN(pOther->__pTimeZonesMap);                   // Copying available TimeZone List
+               __pTimeZonesMap = CloneMultiHashMapN(pOther->__pTimeZonesMap);
        }
-       __CalendarType = pOther->__CalendarType;                                    // Copying Calendar Type
+       __CalendarType = pOther->__CalendarType;
+
+       if(__pIcuDateFormatSymbols)
+       {
+               delete __pIcuDateFormatSymbols;
+               __pIcuDateFormatSymbols = null;
+       }
+       _DateTimeSymbolsImpl* pDTS = const_cast<_DateTimeSymbolsImpl*>(pOther);
+       __pIcuDateFormatSymbols = new U_ICU_NAMESPACE::DateFormatSymbols(*(pDTS->GetIcuSymbols()));
+       SysTryReturnVoidResult(NID_LCL, __pIcuDateFormatSymbols, E_OUT_OF_MEMORY, "It is not enough memory.");
 }
 
 
@@ -134,7 +139,7 @@ _DateTimeSymbolsImpl::AddTimeZoneName(const String& timeZoneId, const String& co
 
        if (!__pTimeZonesMap)
        {
-               MultiHashMap* pMultiHashMap = new (std::nothrow) MultiHashMap();
+               MultiHashMap* pMultiHashMap = new (std::nothrow) MultiHashMap(SingleObjectDeleter);
                SysTryReturnResult(NID_LCL, pMultiHashMap, E_OUT_OF_MEMORY, "Memory allocation failed.");
                pMultiHashMap->Construct();
 
@@ -172,7 +177,7 @@ _DateTimeSymbolsImpl::GetAmPm(void) const
 {
        SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
 
-       ClearLastResult();  // Setting Last result to E_SUCCESS
+       ClearLastResult();
        return __pAmPmList;
 }
 
@@ -182,17 +187,19 @@ _DateTimeSymbolsImpl::SetAmPm(const String& amPm)
 {
        SysAssertf(__pAmPmList != null, "Not yet constructed! Construct() should be called before use");
 
-       std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
+       std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
        SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
 
        result r = SetList(pTempArrayList.get(), AM_PM_COUNT, amPm);
        if (!IsFailed(r))
        {
-               __pAmPmList->RemoveAll(true);
                delete __pAmPmList;
                __pAmPmList = pTempArrayList.release();
+               int count = 0;
+               U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pAmPmList, count);
+               __pIcuDateFormatSymbols->setAmPmStrings(pIcuArray, count);
+               delete[] pIcuArray;
        }
-
        return  r;
 }
 
@@ -208,16 +215,18 @@ _DateTimeSymbolsImpl::SetEras(const String& eras)
 {
        SysAssertf(__pErasList != null, "Not yet constructed! Construct() should be called before use");
 
-       std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
+       std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
        SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
 
        result r = SetList(pTempArrayList.get(), ERAS_COUNT, eras);
        if (!IsFailed(r))
        {
-               __pErasList->RemoveAll(true);
                delete __pErasList;
-
                __pErasList = pTempArrayList.release();
+               int count = 0;
+               U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pErasList, count);
+               __pIcuDateFormatSymbols->setEras(pIcuArray, count);
+               delete[] pIcuArray;
        }
 
        return  r;
@@ -228,7 +237,7 @@ _DateTimeSymbolsImpl::GetMonths(void) const
 {
        SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
 
-       ClearLastResult();   // Setting Last result to E_SUCCESS
+       ClearLastResult();
        return __pMonthsList;
 }
 
@@ -237,16 +246,18 @@ _DateTimeSymbolsImpl::SetMonths(const String& months)
 {
        SysAssertf(__pMonthsList != null, "Not yet constructed! Construct() should be called before use");
 
-       std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
+       std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
        SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
 
        result r = SetList(pTempArrayList.get(), MONTH_COUNT, months);
        if (!IsFailed(r))
        {
-               __pMonthsList->RemoveAll(true);
                delete __pMonthsList;
-
                __pMonthsList = pTempArrayList.release();
+               int count = 0;
+               U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pMonthsList, count);
+               __pIcuDateFormatSymbols->setMonths(pIcuArray, count);
+               delete[] pIcuArray;
        }
 
        return  r;
@@ -256,7 +267,7 @@ _DateTimeSymbolsImpl::GetShortMonths(void) const
 {
        SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
 
-       ClearLastResult();  // Setting Last result to E_SUCCESS
+       ClearLastResult();
        return __pShortMonthsList;
 }
 
@@ -265,16 +276,18 @@ _DateTimeSymbolsImpl::SetShortMonths(const String& shortMonths)
 {
        SysAssertf(__pShortMonthsList != null, "Not yet constructed! Construct() should be called before use");
 
-       std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
+       std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
        SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
 
        result r = SetList(pTempArrayList.get(), MONTH_COUNT, shortMonths);
        if (!IsFailed(r))
        {
-               __pShortMonthsList->RemoveAll(true);
                delete __pShortMonthsList;
-
                __pShortMonthsList = pTempArrayList.release();
+               int count = 0;
+               U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pShortMonthsList, count);
+               __pIcuDateFormatSymbols->setShortMonths(pIcuArray, count);
+               delete[] pIcuArray;
        }
 
        return  r;
@@ -285,7 +298,7 @@ _DateTimeSymbolsImpl::GetWeekdays(void) const
 {
        SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
 
-       ClearLastResult();          // Setting Last result to E_SUCCESS
+       ClearLastResult();
        return __pWeekdaysList;
 }
 
@@ -294,16 +307,18 @@ _DateTimeSymbolsImpl::SetWeekdays(const String& weekdays)
 {
        SysAssertf(__pWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
 
-       std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
+       std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
        SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
 
        result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, weekdays);
        if (!IsFailed(r))
        {
-               __pWeekdaysList->RemoveAll(true);
                delete __pWeekdaysList;
-
                __pWeekdaysList = pTempArrayList.release();
+               int count = 0;
+               U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pWeekdaysList, count);
+               __pIcuDateFormatSymbols->setWeekdays(pIcuArray, count);
+               delete[] pIcuArray;
        }
 
        return  r;
@@ -314,8 +329,7 @@ const IList*
 _DateTimeSymbolsImpl::GetShortWeekdays(void) const
 {
        SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
-
-       ClearLastResult();          // Setting Last result to E_SUCCESS
+       ClearLastResult();
        return __pShortWeekdaysList;
 }
 
@@ -324,16 +338,18 @@ _DateTimeSymbolsImpl::SetShortWeekdays(const String& shortWeekdays)
 {
        SysAssertf(__pShortWeekdaysList != null, "Not yet constructed! Construct() should be called before use");
 
-       std::unique_ptr< ArrayList, AllElementsDeleter > pTempArrayList(new (std::nothrow) ArrayList());
+       std::unique_ptr< ArrayList > pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
        SysTryReturnResult(NID_LCL, pTempArrayList != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
 
        result r = SetList(pTempArrayList.get(), WEEKDAYS_COUNT, shortWeekdays);
        if (!IsFailed(r))
        {
-               __pShortWeekdaysList->RemoveAll(true);
                delete __pShortWeekdaysList;
-
                __pShortWeekdaysList = pTempArrayList.release();
+               int count = 0;
+               U_ICU_NAMESPACE::UnicodeString* pIcuArray = _LocaleData::ConvertOspArrayToIcuStringArrayN(__pShortWeekdaysList, count);
+               __pIcuDateFormatSymbols->setShortWeekdays(pIcuArray, count);
+               delete[] pIcuArray;
        }
 
        return  r;
@@ -392,7 +408,6 @@ _DateTimeSymbolsImpl::GetTimeZoneName(Tizen::Base::String& timeZoneId, int timeZ
 result
 _DateTimeSymbolsImpl::SetTimeZoneName(const Tizen::Base::String& timeZoneId, const Tizen::Base::String& concatenatedTimeZoneName)
 {
-        // checking if concatenatedTimeZoneName has 4 names
         StringTokenizer tokenizer(concatenatedTimeZoneName, L"|");
         SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == MAX_TIMEZONE_NAME),
                         E_INVALID_ARG, "Invalid argument is used. %ls is invalid.", concatenatedTimeZoneName.GetPointer());
@@ -422,15 +437,13 @@ _DateTimeSymbolsImpl::SetTimeZoneName(const Tizen::Base::String& timeZoneId, con
 }
 
 
-// internal function
-// This function makes a copy of input array list of strings
 IList*
 _DateTimeSymbolsImpl::CloneArrayListN(const IList* pList) const
 {
        SysTryReturn(NID_LCL, pList, null, E_INVALID_ARG, "[%s] Invalid argument is used. pList is Null", GetErrorMessage(E_INVALID_ARG));
 
-       ClearLastResult();  // Setting last result to E_SUCCESS
-       std::unique_ptr< ArrayList, AllElementsDeleter > pClonedArrayList(new (std::nothrow) ArrayList());
+       ClearLastResult();
+       std::unique_ptr< ArrayList > pClonedArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
        SysTryReturn(NID_LCL, pClonedArrayList, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
 
        result r = pClonedArrayList->Construct(pList->GetCount());
@@ -459,7 +472,7 @@ _DateTimeSymbolsImpl::CloneMultiHashMapN(const Tizen::Base::Collection::IMultiMa
 
        ClearLastResult();
 
-       std::unique_ptr< MultiHashMap, AllElementsDeleter > pClonedMultiHashMap(new (std::nothrow) MultiHashMap());
+       std::unique_ptr< MultiHashMap > pClonedMultiHashMap(new (std::nothrow) MultiHashMap(SingleObjectDeleter));
        SysTryReturn(NID_LCL, pClonedMultiHashMap, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
        result r = pClonedMultiHashMap->Construct();
@@ -515,11 +528,9 @@ _DateTimeSymbolsImpl::SetList(Tizen::Base::Collection::ArrayList* pArrayList, in
 
        SysTryReturnResult(NID_LCL, (tokenizer.GetTokenCount() == tokenCount), E_INVALID_ARG,
                                "Invalid argument is used. stringWillBeTokenized=%ls", stringWillBeTokenized.GetPointer());
+       SysTryReturnResult(NID_LCL, pArrayList, E_INVALID_ARG, "Invalid argument is used. pArrayList should not be null");
 
-       std::unique_ptr< ArrayList, AllElementsDeleter > pTmpArrayList(pArrayList);
-       SysTryReturnResult(NID_LCL, pTmpArrayList, E_INVALID_ARG, "Invalid argument is used. pArrayList should not be null");
-
-       result r = pTmpArrayList->Construct(tokenCount);  // Constructing list for tokenCount elements
+       result r = pArrayList->Construct(tokenCount);
        if (!IsFailed(r))
        {
                String token;
@@ -529,14 +540,12 @@ _DateTimeSymbolsImpl::SetList(Tizen::Base::Collection::ArrayList* pArrayList, in
                        pListStr.reset(new (std::nothrow) String(token));
                        SysTryReturnResult(NID_LCL, pListStr, E_OUT_OF_MEMORY, "Memory allocation failed.");
 
-                       r = pTmpArrayList->Add(pListStr.get());
+                       r = pArrayList->Add(pListStr.get());
                        SysTryReturnResult(NID_LCL, r == E_SUCCESS, r, "Failed to add value to array list.");
 
                        pListStr.release();
                }
        }
-
-       pTmpArrayList.release();
        return r;
 }
 
@@ -545,26 +554,25 @@ _DateTimeSymbolsImpl::ReleaseList(IList* pList)
 {
        if (pList)
        {
-               pList->RemoveAll(true);         // Remove elements and de-allocate memory used by them
                delete pList;
+               pList = null;
        }
 }
 
-// This function release all the list after deleting their contents
 void
 _DateTimeSymbolsImpl::ReleaseAll(void)
 {
-       ReleaseList(__pErasList);                           // Deleting EraList after deleting its contents
-       ReleaseList(__pMonthsList);                         // Deleting MonthsList after deleting its contents
-       ReleaseList(__pShortMonthsList);                    // Deleting ShortMonthsList after deleting its contents
-       ReleaseList(__pWeekdaysList);                       // Deleting WeekdaysList after deleting its contents
-       ReleaseList(__pShortWeekdaysList);                  // Deleting ShortWeekdaysList after deleting its contents
-       ReleaseList(__pAmPmList);                           // Deleting AmPmList after deleting its contents
+       ReleaseList(__pErasList);
+       ReleaseList(__pMonthsList);
+       ReleaseList(__pShortMonthsList);
+       ReleaseList(__pWeekdaysList);
+       ReleaseList(__pShortWeekdaysList);
+       ReleaseList(__pAmPmList);
 
        if (__pTimeZonesMap)
        {
-               __pTimeZonesMap->RemoveAll(true);               // Removing and deleting contents of TimeZonesMap
-               delete __pTimeZonesMap;                         // Deleting TimeZonesMap
+               delete __pTimeZonesMap;
+               __pTimeZonesMap = null;
        }
 }
 
@@ -574,6 +582,64 @@ _DateTimeSymbolsImpl::GetDateTimeSymbolsImpl(DateTimeSymbols* pDateTimeSymbols)
        return pDateTimeSymbols->__pDateTimeSymbolsImpl;
 }
 
+U_ICU_NAMESPACE::DateFormatSymbols*
+_DateTimeSymbolsImpl::GetIcuSymbols(void)
+{
+       return __pIcuDateFormatSymbols;
+}
+
+Tizen::Base::Collection::IList*
+_DateTimeSymbolsImpl::GetSymbolAttributeArrayN(DateFormatSymbolsAttributes attributeName)
+{
+       SysTryReturn(NID_LCL, __pIcuDateFormatSymbols, null, E_SYSTEM, "It is not initialized.");
+       ClearLastResult();
+       int count = 0;
+       const IcuUnicodeString* pIcuList = null;
+
+       switch (attributeName)
+       {
+               case DATE_FORMAT_SYM_ERA_LIST:
+               {
+                       pIcuList = __pIcuDateFormatSymbols->getEras(count);
+                       break;
+               }
+               case DATE_FORMAT_SYM_MONTH_LIST:
+               {
+                       pIcuList = __pIcuDateFormatSymbols->getMonths(count);
+                       break;
+               }
+               case DATE_FORMAT_SYM_SHORT_MONTH_LIST:
+               {
+                       pIcuList = __pIcuDateFormatSymbols->getShortMonths(count);
+                       break;
+               }
+               case DATE_FORMAT_SYM_WEEKDAY_LIST:
+               {
+                       pIcuList = __pIcuDateFormatSymbols->getWeekdays(count);
+                       break;
+               }
+               case DATE_FORMAT_SYM_SHORT_WEEKDAY_LIST:
+               {
+                       pIcuList = __pIcuDateFormatSymbols->getShortWeekdays(count);
+                       break;
+               }
+               case DATE_FORMAT_SYM_AM_PM_LIST:
+               {
+                       pIcuList = __pIcuDateFormatSymbols->getAmPmStrings(count);
+                       break;
+               }
+               default:
+               {
+                       SetLastResult(E_UNSUPPORTED_OPERATION);
+                       break;
+               }
+       }
+
+       SysTryReturn(NID_LCL, pIcuList, null, E_SYSTEM, "It is faile to get symbols.");
+       return _LocaleData::ConvertIcuStringArrayToOspArrayN(pIcuList, count);
+}
+
+
 };
 };      // Tizen::Locale