Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
5 package org.mozilla.gecko.sync;
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;
13 import org.mozilla.gecko.background.common.log.Logger;
15 public class InfoCounts {
16 static final String LOG_TAG = "InfoCounts";
18 /**
19 * Counts fetched from the server, or <code>null</code> if not yet fetched.
20 */
21 private Map<String, Integer> counts = null;
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>();
28 Set<Entry<String, Object>> entrySet = record.object.entrySet();
30 String key;
31 Object value;
33 for (Entry<String, Object> entry : entrySet) {
34 key = entry.getKey();
35 value = entry.getValue();
37 if (value instanceof Integer) {
38 map.put(key, (Integer) value);
39 continue;
40 }
42 if (value instanceof Long) {
43 map.put(key, ((Long) value).intValue());
44 continue;
45 }
47 Logger.warn(LOG_TAG, "Skipping info/collection_counts entry for " + key);
48 }
50 this.counts = Collections.unmodifiableMap(map);
51 }
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 }