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. import org.apache.commons.betwixt.io.IDGenerator;
  18. /** <p>Abstract superclass for {@link IDGenerator} implementations.</p>
  19. *
  20. * <p>It implements the entire <code>IDGenerator</code> interface.
  21. * When <code>nextId</code> is called,
  22. * this class sets the <code>LastId</code> property (as well
  23. * as returning the value).
  24. * Subclasses should override {@link #nextIdImpl}.</p>
  25. *
  26. * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
  27. * @version $Revision: 1.7 $
  28. */
  29. public abstract class AbstractIDGenerator implements IDGenerator {
  30. /** Last <code>ID</code> returned */
  31. private String lastId = "0";
  32. /**
  33. * Gets last <code>ID</code> returned.
  34. *
  35. * @return the last id created by the generated
  36. */
  37. public final String getLastId() {
  38. return lastId;
  39. }
  40. /**
  41. * <p>Generate next <code>ID</code>.</p>
  42. *
  43. * <p>This method obtains the next <code>ID</code> from subclass
  44. * and then uses this to set the <code>LastId</code> property.</p>
  45. *
  46. * @return the next id generated
  47. */
  48. public final String nextId() {
  49. lastId = nextIdImpl();
  50. return lastId;
  51. }
  52. /**
  53. * Subclasses should <strong>provide an implementation</strong> for this method.
  54. * This implementation needs only provide the next <code>ID</code>
  55. * value (according to it's algorithm).
  56. * Setting the <code>LastId</code> property can be left to this class.
  57. *
  58. * @return the next id generated
  59. */
  60. protected abstract String nextIdImpl();
  61. }