mobile/android/base/sync/InfoCounts.java

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:82e4d49abd82
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.gecko.sync;
6
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Map.Entry;
11 import java.util.Set;
12
13 import org.mozilla.gecko.background.common.log.Logger;
14
15 public class InfoCounts {
16 static final String LOG_TAG = "InfoCounts";
17
18 /**
19 * Counts fetched from the server, or <code>null</code> if not yet fetched.
20 */
21 private Map<String, Integer> counts = null;
22
23 @SuppressWarnings("unchecked")
24 public InfoCounts(final ExtendedJSONObject record) {
25 Logger.debug(LOG_TAG, "info/collection_counts is " + record.toJSONString());
26 HashMap<String, Integer> map = new HashMap<String, Integer>();
27
28 Set<Entry<String, Object>> entrySet = record.object.entrySet();
29
30 String key;
31 Object value;
32
33 for (Entry<String, Object> entry : entrySet) {
34 key = entry.getKey();
35 value = entry.getValue();
36
37 if (value instanceof Integer) {
38 map.put(key, (Integer) value);
39 continue;
40 }
41
42 if (value instanceof Long) {
43 map.put(key, ((Long) value).intValue());
44 continue;
45 }
46
47 Logger.warn(LOG_TAG, "Skipping info/collection_counts entry for " + key);
48 }
49
50 this.counts = Collections.unmodifiableMap(map);
51 }
52
53 /**
54 * Return the server count for the given collection, or null if the counts
55 * have not been fetched or the given collection does not have a count.
56 *
57 * @param collection
58 * The collection to inspect.
59 * @return the number of elements in the named collection.
60 */
61 public Integer getCount(String collection) {
62 if (counts == null) {
63 return null;
64 }
65 return counts.get(collection);
66 }
67 }

mercurial