counter.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2007 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 Callback object that counts matches.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.pattern.callback.Counter');
  20. /**
  21. * Callback class for counting matches.
  22. * @constructor
  23. * @final
  24. */
  25. goog.dom.pattern.callback.Counter = function() {
  26. /**
  27. * The count of objects matched so far.
  28. *
  29. * @type {number}
  30. */
  31. this.count = 0;
  32. /**
  33. * The callback function. Suitable as a callback for
  34. * {@link goog.dom.pattern.Matcher}.
  35. * @private {Function}
  36. */
  37. this.callback_ = null;
  38. };
  39. /**
  40. * Get a bound callback function that is suitable as a callback for
  41. * {@link goog.dom.pattern.Matcher}.
  42. *
  43. * @return {!Function} A callback function.
  44. */
  45. goog.dom.pattern.callback.Counter.prototype.getCallback = function() {
  46. if (!this.callback_) {
  47. this.callback_ = goog.bind(function() {
  48. this.count++;
  49. return false;
  50. }, this);
  51. }
  52. return this.callback_;
  53. };
  54. /**
  55. * Reset the counter.
  56. */
  57. goog.dom.pattern.callback.Counter.prototype.reset = function() {
  58. this.count = 0;
  59. };