Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: pkg/intl/lib/lazy_locale_data.dart

Issue 10917069: Async initialization for DateFormat (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * This defines a class for loading locale data incrementally from
7 * an external source as JSON. The external sources expected are either
8 * local files or via HTTP request.
9 */
10
11 #library('lazy_locale_data');
12 #import('dart:uri');
13 #import('intl_helpers.dart');
14 #import('dart:json');
15
16 /**
17 * This implements the very basic map-type operations which are used
18 * in locale lookup, and looks them up based on a URL that defines
19 * the external source.
20 */
21 class LazyLocaleData {
22 /// This holds the data we have loaded.
23 Map map;
24
25 /// The object that actually does the data reading.
26 LocaleDataReader _reader;
27
28 /**
29 * In order to avoid a potentially remote call to see if a locale
30 * is available, we hold a complete list of all the available
31 * locales.
32 */
33 List availableLocales;
34
35 /**
36 * Given a piece of remote data, apply [_creationFunction] to it to
37 * convert it into the right form. Typically this means converting it
38 * from a Map into an object form.
39 */
40 Function _creationFunction;
41
42 /**
43 * The constructor. The [uriString] specifies where the data comes
44 * from. The [_creationFunction] creates the appropriate data type
45 * from the remote data (which typically comes in as a Map). The
46 * [keys] lists the set of remotely available locale names so we know which
47 * things can be fetched without having to check remotely.
48 */
49 LazyLocaleData(this._reader, this._creationFunction, List keys) {
50 map = new Map();
51 availableLocales = keys;
52 }
53
54 Set _availableLocaleSet;
Emily Fortuna 2012/09/04 20:14:42 add a newline below, and perhaps move this declara
Alan Knight 2012/09/04 23:39:03 Done.
55 /// Provides the list of available locales as a set.
56 get availableLocaleSet {
57 if (_availableLocaleSet == null) {
58 _availableLocaleSet = new Set.from(availableLocales);
59 }
60 return _availableLocaleSet;
61 }
62
63 /**
64 * Tests if we have data for the locale available. Note that this returns
65 * true even if the data is known to be available remotely but not yet loaded.
66 */
67 bool containsKey(String locale) => availableLocaleSet.contains(locale);
68
69 /// Return the list of keys/locale names.
Emily Fortuna 2012/09/04 20:14:42 same on consistent commenting format. You can use
Alan Knight 2012/09/04 23:39:03 Done.
70 List getKeys() => availableLocales;
71
72 /**
73 * Returns the data stored for [localeName]. If no data has been loaded
74 * for [localeName], throws an exception. If no data is available for
75 * [localeName] then throw an exception with a different message.
76 */
77 operator [](String localeName) {
78 if (containsKey(localeName)) {
79 var data = map[localeName];
80 if (data == null) {
81 throw new LocaleDataException(
82 "Locale $localeName has not been initialized."
83 " Call initializeDateFormatting($localeName, <data url>) first");
84 } else {
85 return data;
86 }
87 } else {
88 unsupportedLocale(localeName);
89 }
90 }
91
92 /**
93 * Throw an exception indicating that the locale has no data available,
94 * either locally or remotely.
95 */
96 unsupportedLocale(localeName) { throw new LocaleDataException(
97 'Locale $localeName has no data available');}
98
99 /**
100 * Initialize for locale. Internal use only. As a user, call
101 * initializeDateFormatting instead.
102 */
103 Future initLocale(String localeName) {
104 var data = _reader.read(localeName);
105 return jsonData(data).transform( (input) {
106 map[localeName] = _creationFunction(input);});
107 return data;
108 }
109
110 /**
111 * Given a Future [input] whose value is expected to be a string in JSON form,
112 * return another future that parses the JSON into a usable format.
113 */
114 Future jsonData(Future input) {
115 return input.transform( (response) => JSON.parse(response));
116 }
117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698