// Copyright 2009 Google Inc.  All Rights Reserved.

/**
 * @fileoverview Simple cache for javascript objects.
 * @author Michal Drewniak
 */

/**
 * Cache class.
 * Keeps a copy of data (such as json feed) in a local object.
 * @constructor
 */
earth.Cache = function() {
  /**
   * Container for cached data.
   * @type {Object}
   */
  this.items = {};
};

/**
 * Adds item to the cache.
 * @param {Object} item Item to be added to the cache.
 * @param {string} id Unique text identifying the item. Usually feed url.
 * @return {boolean} Flag indicating whether item was successfully added.
 */
earth.Cache.prototype.addItem = function(item, id) {
  if (!item[id]) {
    this.items[id] = item;
    return true;
  } else {
    return false;
  }
};

/**
 * Returns cached object.
 * @param {string} id Unique text identifying the item.
 * @return {Object} Cached object. Returns null if object not found.
 */
earth.Cache.prototype.getItem = function(id) {
  return this.items[id];
};

/**
 * Clears the cache.
 */
earth.Cache.prototype.clearCache = function() {
  for (var item in this.items) {
    delete this.items[item];
  }
};
