1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/sync/InfoCounts.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,67 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.sync; 1.9 + 1.10 +import java.util.Collections; 1.11 +import java.util.HashMap; 1.12 +import java.util.Map; 1.13 +import java.util.Map.Entry; 1.14 +import java.util.Set; 1.15 + 1.16 +import org.mozilla.gecko.background.common.log.Logger; 1.17 + 1.18 +public class InfoCounts { 1.19 + static final String LOG_TAG = "InfoCounts"; 1.20 + 1.21 + /** 1.22 + * Counts fetched from the server, or <code>null</code> if not yet fetched. 1.23 + */ 1.24 + private Map<String, Integer> counts = null; 1.25 + 1.26 + @SuppressWarnings("unchecked") 1.27 + public InfoCounts(final ExtendedJSONObject record) { 1.28 + Logger.debug(LOG_TAG, "info/collection_counts is " + record.toJSONString()); 1.29 + HashMap<String, Integer> map = new HashMap<String, Integer>(); 1.30 + 1.31 + Set<Entry<String, Object>> entrySet = record.object.entrySet(); 1.32 + 1.33 + String key; 1.34 + Object value; 1.35 + 1.36 + for (Entry<String, Object> entry : entrySet) { 1.37 + key = entry.getKey(); 1.38 + value = entry.getValue(); 1.39 + 1.40 + if (value instanceof Integer) { 1.41 + map.put(key, (Integer) value); 1.42 + continue; 1.43 + } 1.44 + 1.45 + if (value instanceof Long) { 1.46 + map.put(key, ((Long) value).intValue()); 1.47 + continue; 1.48 + } 1.49 + 1.50 + Logger.warn(LOG_TAG, "Skipping info/collection_counts entry for " + key); 1.51 + } 1.52 + 1.53 + this.counts = Collections.unmodifiableMap(map); 1.54 + } 1.55 + 1.56 + /** 1.57 + * Return the server count for the given collection, or null if the counts 1.58 + * have not been fetched or the given collection does not have a count. 1.59 + * 1.60 + * @param collection 1.61 + * The collection to inspect. 1.62 + * @return the number of elements in the named collection. 1.63 + */ 1.64 + public Integer getCount(String collection) { 1.65 + if (counts == null) { 1.66 + return null; 1.67 + } 1.68 + return counts.get(collection); 1.69 + } 1.70 +}