summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/InfoCounts.java
blob: 832e97d1026124fcfe10c319609dca32b9b2f5de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 <code>null</code> if not yet fetched.
   */
  private Map<String, Integer> counts = null;

  @SuppressWarnings("unchecked")
  public InfoCounts(final ExtendedJSONObject record) {
    Logger.debug(LOG_TAG, "info/collection_counts is " + record.toJSONString());
    HashMap<String, Integer> map = new HashMap<String, Integer>();

    Set<Entry<String, Object>> entrySet = record.object.entrySet();

    String key;
    Object value;

    for (Entry<String, Object> 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);
  }
}