iterablemechanism.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Interface for storing, retieving and scanning data using some
  16. * persistence mechanism.
  17. *
  18. */
  19. goog.provide('goog.storage.mechanism.IterableMechanism');
  20. goog.require('goog.array');
  21. goog.require('goog.asserts');
  22. goog.require('goog.iter');
  23. goog.require('goog.storage.mechanism.Mechanism');
  24. /**
  25. * Interface for all iterable storage mechanisms.
  26. *
  27. * @constructor
  28. * @struct
  29. * @extends {goog.storage.mechanism.Mechanism}
  30. */
  31. goog.storage.mechanism.IterableMechanism = function() {
  32. goog.storage.mechanism.IterableMechanism.base(this, 'constructor');
  33. };
  34. goog.inherits(
  35. goog.storage.mechanism.IterableMechanism, goog.storage.mechanism.Mechanism);
  36. /**
  37. * Get the number of stored key-value pairs.
  38. *
  39. * Could be overridden in a subclass, as the default implementation is not very
  40. * efficient - it iterates over all keys.
  41. *
  42. * @return {number} Number of stored elements.
  43. */
  44. goog.storage.mechanism.IterableMechanism.prototype.getCount = function() {
  45. var count = 0;
  46. goog.iter.forEach(this.__iterator__(true), function(key) {
  47. goog.asserts.assertString(key);
  48. count++;
  49. });
  50. return count;
  51. };
  52. /**
  53. * Returns an iterator that iterates over the elements in the storage. Will
  54. * throw goog.iter.StopIteration after the last element.
  55. *
  56. * @param {boolean=} opt_keys True to iterate over the keys. False to iterate
  57. * over the values. The default value is false.
  58. * @return {!goog.iter.Iterator} The iterator.
  59. */
  60. goog.storage.mechanism.IterableMechanism.prototype.__iterator__ =
  61. goog.abstractMethod;
  62. /**
  63. * Remove all key-value pairs.
  64. *
  65. * Could be overridden in a subclass, as the default implementation is not very
  66. * efficient - it iterates over all keys.
  67. */
  68. goog.storage.mechanism.IterableMechanism.prototype.clear = function() {
  69. // This converts the keys to an array first because otherwise
  70. // removing while iterating results in unstable ordering of keys and
  71. // can skip keys or terminate early.
  72. var keys = goog.iter.toArray(this.__iterator__(true));
  73. var selfObj = this;
  74. goog.array.forEach(keys, function(key) { selfObj.remove(key); });
  75. };