Debian Codegraph @

closure-library/closure/goog/storage/mechanism/iterablemechanism.js

raw
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 /**
16 * @fileoverview Interface for storing, retieving and scanning data using some
17 * persistence mechanism.
18 *
19 */
20
21 goog.provide('goog.storage.mechanism.IterableMechanism');
22
23 goog.require('goog.array');
24 goog.require('goog.asserts');
25 goog.require('goog.iter');
26 goog.require('goog.storage.mechanism.Mechanism');
27
28
29
30 /**
31 * Interface for all iterable storage mechanisms.
32 *
33 * @constructor
34 * @struct
35 * @extends {goog.storage.mechanism.Mechanism}
36 */
37 goog.storage.mechanism.IterableMechanism = function() {
38 goog.storage.mechanism.IterableMechanism.base(this, 'constructor');
39 };
40 goog.inherits(
41 goog.storage.mechanism.IterableMechanism, goog.storage.mechanism.Mechanism);
42
43
44 /**
45 * Get the number of stored key-value pairs.
46 *
47 * Could be overridden in a subclass, as the default implementation is not very
48 * efficient - it iterates over all keys.
49 *
50 * @return {number} Number of stored elements.
51 */
52 goog.storage.mechanism.IterableMechanism.prototype.getCount = function() {
53 var count = 0;
54 goog.iter.forEach(this.__iterator__(true), function(key) {
55 goog.asserts.assertString(key);
56 count++;
57 });
58 return count;
59 };
60
61
62 /**
63 * Returns an iterator that iterates over the elements in the storage. Will
64 * throw goog.iter.StopIteration after the last element.
65 *
66 * @param {boolean=} opt_keys True to iterate over the keys. False to iterate
67 * over the values. The default value is false.
68 * @return {!goog.iter.Iterator} The iterator.
69 */
70 goog.storage.mechanism.IterableMechanism.prototype.__iterator__ =
71 goog.abstractMethod;
72
73
74 /**
75 * Remove all key-value pairs.
76 *
77 * Could be overridden in a subclass, as the default implementation is not very
78 * efficient - it iterates over all keys.
79 */
80 goog.storage.mechanism.IterableMechanism.prototype.clear = function() {
81 // This converts the keys to an array first because otherwise
82 // removing while iterating results in unstable ordering of keys and
83 // can skip keys or terminate early.
84 var keys = goog.iter.toArray(this.__iterator__(true));
85 var selfObj = this;
86 goog.array.forEach(keys, function(key) { selfObj.remove(key); });
87 };