1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.betwixt.io.id;
  17. /** <p>Generates <code>ID</code>'s in numeric sequence.
  18. * A simple counter is used.
  19. * Every time that {@link #nextIdImpl} is called,
  20. * this counter is incremented.</p>
  21. *
  22. * <p>By default, the counter starts at zero.
  23. * A user can set the initial value by using the
  24. * {@link #SequentialIDGenerator(int start)} constructor.</p>
  25. *
  26. * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
  27. * @version $Revision: 1.7 $
  28. */
  29. public final class SequentialIDGenerator extends AbstractIDGenerator {
  30. /** Counter used to assign <code>ID</code>'s */
  31. private int counter = 0;
  32. /**
  33. * Base constructor.
  34. * Counter starts at zero.
  35. */
  36. public SequentialIDGenerator() {}
  37. /**
  38. * Constructor sets the start value for the counter.
  39. *
  40. * <p><strong>Note</strong> since the counter increments
  41. * before returning the next value,
  42. * first <code>ID</code> generated will be <em>one more</em>
  43. * than the given <code>start</code> parameter.</p>
  44. *
  45. * @param start start the counting at this value
  46. */
  47. public SequentialIDGenerator(int start) {
  48. this.counter = start;
  49. }
  50. /**
  51. * Increment counter and then return value.
  52. *
  53. * @return one more than the current counter (converted to a string)
  54. */
  55. public String nextIdImpl() {
  56. return Integer.toString(++counter);
  57. }
  58. /**
  59. * Gets the current counter value
  60. *
  61. * @return the last ID in the sequence
  62. */
  63. public int getCount() {
  64. return counter;
  65. }
  66. }