diff -r 000000000000 -r 6474c204b198 mobile/android/base/sync/InfoCounts.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mobile/android/base/sync/InfoCounts.java Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,67 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.gecko.sync; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.mozilla.gecko.background.common.log.Logger; + +public class InfoCounts { + static final String LOG_TAG = "InfoCounts"; + + /** + * Counts fetched from the server, or null if not yet fetched. + */ + private Map counts = null; + + @SuppressWarnings("unchecked") + public InfoCounts(final ExtendedJSONObject record) { + Logger.debug(LOG_TAG, "info/collection_counts is " + record.toJSONString()); + HashMap map = new HashMap(); + + Set> entrySet = record.object.entrySet(); + + String key; + Object value; + + for (Entry entry : entrySet) { + key = entry.getKey(); + value = entry.getValue(); + + if (value instanceof Integer) { + map.put(key, (Integer) value); + continue; + } + + if (value instanceof Long) { + map.put(key, ((Long) value).intValue()); + continue; + } + + Logger.warn(LOG_TAG, "Skipping info/collection_counts entry for " + key); + } + + this.counts = Collections.unmodifiableMap(map); + } + + /** + * Return the server count for the given collection, or null if the counts + * have not been fetched or the given collection does not have a count. + * + * @param collection + * The collection to inspect. + * @return the number of elements in the named collection. + */ + public Integer getCount(String collection) { + if (counts == null) { + return null; + } + return counts.get(collection); + } +}