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