prefixedmechanism.js 2.8 KB

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