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