1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2014, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  *
7  *******************************************************************************
8  */
9 #include "unicode/utypes.h"
10 
11 #if !UCONFIG_NO_SERVICE
12 
13 #include "unicode/resbund.h"
14 #include "uresimp.h"
15 #include "cmemory.h"
16 #include "servloc.h"
17 #include "ustrfmt.h"
18 #include "uhash.h"
19 #include "charstr.h"
20 #include "uassert.h"
21 
22 #define UNDERSCORE_CHAR ((UChar)0x005f)
23 #define AT_SIGN_CHAR    ((UChar)64)
24 #define PERIOD_CHAR     ((UChar)46)
25 
26 U_NAMESPACE_BEGIN
27 
28 /*
29  ******************************************************************
30  */
31 
SimpleLocaleKeyFactory(UObject * objToAdopt,const UnicodeString & locale,int32_t kind,int32_t coverage)32 SimpleLocaleKeyFactory::SimpleLocaleKeyFactory(UObject* objToAdopt,
33                                                const UnicodeString& locale,
34                                                int32_t kind,
35                                                int32_t coverage)
36   : LocaleKeyFactory(coverage)
37   , _obj(objToAdopt)
38   , _id(locale)
39   , _kind(kind)
40 {
41 }
42 
SimpleLocaleKeyFactory(UObject * objToAdopt,const Locale & locale,int32_t kind,int32_t coverage)43 SimpleLocaleKeyFactory::SimpleLocaleKeyFactory(UObject* objToAdopt,
44                                                const Locale& locale,
45                                                int32_t kind,
46                                                int32_t coverage)
47   : LocaleKeyFactory(coverage)
48   , _obj(objToAdopt)
49   , _id()
50   , _kind(kind)
51 {
52     LocaleUtility::initNameFromLocale(locale, _id);
53 }
54 
~SimpleLocaleKeyFactory()55 SimpleLocaleKeyFactory::~SimpleLocaleKeyFactory()
56 {
57   delete _obj;
58   _obj = NULL;
59 }
60 
61 UObject*
create(const ICUServiceKey & key,const ICUService * service,UErrorCode & status) const62 SimpleLocaleKeyFactory::create(const ICUServiceKey& key, const ICUService* service, UErrorCode& status) const
63 {
64     if (U_SUCCESS(status)) {
65         const LocaleKey& lkey = (const LocaleKey&)key;
66         if (_kind == LocaleKey::KIND_ANY || _kind == lkey.kind()) {
67             UnicodeString keyID;
68             lkey.currentID(keyID);
69             if (_id == keyID) {
70                 return service->cloneInstance(_obj);
71             }
72         }
73     }
74     return NULL;
75 }
76 
77 //UBool
78 //SimpleLocaleKeyFactory::isSupportedID(const UnicodeString& id, UErrorCode& /* status */) const
79 //{
80 //    return id == _id;
81 //}
82 
83 void
updateVisibleIDs(Hashtable & result,UErrorCode & status) const84 SimpleLocaleKeyFactory::updateVisibleIDs(Hashtable& result, UErrorCode& status) const
85 {
86     if (U_SUCCESS(status)) {
87         if (_coverage & 0x1) {
88             result.remove(_id);
89         } else {
90             result.put(_id, (void*)this, status);
91         }
92     }
93 }
94 
95 #ifdef SERVICE_DEBUG
96 UnicodeString&
debug(UnicodeString & result) const97 SimpleLocaleKeyFactory::debug(UnicodeString& result) const
98 {
99     LocaleKeyFactory::debug(result);
100     result.append((UnicodeString)", id: ");
101     result.append(_id);
102     result.append((UnicodeString)", kind: ");
103     result.append(_kind);
104     return result;
105 }
106 
107 UnicodeString&
debugClass(UnicodeString & result) const108 SimpleLocaleKeyFactory::debugClass(UnicodeString& result) const
109 {
110     return result.append((UnicodeString)"SimpleLocaleKeyFactory");
111 }
112 #endif
113 
114 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleLocaleKeyFactory)
115 
116 U_NAMESPACE_END
117 
118 /* !UCONFIG_NO_SERVICE */
119 #endif
120 
121 
122