Debian Codegraph @

closure-library/closure/goog/storage/mechanism/prefixedmechanism.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 Wraps an iterable storage mechanism and creates artificial
17 * namespaces using a prefix in the global namespace.
18 *
19 */
20
21 goog.provide('goog.storage.mechanism.PrefixedMechanism');
22
23 goog.require('goog.iter.Iterator');
24 goog.require('goog.storage.mechanism.IterableMechanism');
25
26
27
28 /**
29 * Wraps an iterable storage mechanism and creates artificial namespaces.
30 *
31 * @param {!goog.storage.mechanism.IterableMechanism} mechanism Underlying
32 * iterable storage mechanism.
33 * @param {string} prefix Prefix for creating an artificial namespace.
34 * @constructor
35 * @struct
36 * @extends {goog.storage.mechanism.IterableMechanism}
37 * @final
38 */
39 goog.storage.mechanism.PrefixedMechanism = function(mechanism, prefix) {
40 goog.storage.mechanism.PrefixedMechanism.base(this, 'constructor');
41 /**
42 * The mechanism to be prefixed.
43 *
44 * @private {goog.storage.mechanism.IterableMechanism}
45 */
46 this.mechanism_ = mechanism;
47
48 /**
49 * The prefix for creating artificial namespaces.
50 *
51 * @private {string}
52 */
53 this.prefix_ = prefix + '::';
54 };
55 goog.inherits(
56 goog.storage.mechanism.PrefixedMechanism,
57 goog.storage.mechanism.IterableMechanism);
58
59
60 /** @override */
61 goog.storage.mechanism.PrefixedMechanism.prototype.set = function(key, value) {
62 this.mechanism_.set(this.prefix_ + key, value);
63 };
64
65
66 /** @override */
67 goog.storage.mechanism.PrefixedMechanism.prototype.get = function(key) {
68 return this.mechanism_.get(this.prefix_ + key);
69 };
70
71
72 /** @override */
73 goog.storage.mechanism.PrefixedMechanism.prototype.remove = function(key) {
74 this.mechanism_.remove(this.prefix_ + key);
75 };
76
77
78 /** @override */
79 goog.storage.mechanism.PrefixedMechanism.prototype.__iterator__ = function(
80 opt_keys) {
81 var subIter = this.mechanism_.__iterator__(true);
82 var selfObj = this;
83 var newIter = new goog.iter.Iterator();
84 newIter.next = function() {
85 var key = /** @type {string} */ (subIter.next());
86 while (key.substr(0, selfObj.prefix_.length) != selfObj.prefix_) {
87 key = /** @type {string} */ (subIter.next());
88 }
89 return opt_keys ? key.substr(selfObj.prefix_.length) :
90 selfObj.mechanism_.get(key);
91 };
92 return newIter;
93 };