This is more of a design question and my main intention to post here is to get diverse inputs on the problem's solution.
What I want to create is a counter. A counter maybe single valued (single char / digit) or multivalued. So I've designed an hierarchy like this (read Key = Counter) :
Key
|
-----------------------------------------------
| | | |
SingleValueKey MultiValueKey NumericKey AlphaNumericKey
(there could be further mix and match between these types)
Now while designing the MultiValueKey, I thought I could simply use a collection of n-SingleValueKey to create a n-character counter. For e.g. a 2 digit (multivalue) counter will use an array of size 2 of SingleValue Numeric counters. What I'm eventually planning is a variable length alpha-numeric key set, base 64, for short urls. The numeric context is just a simple example.
What I'm stuck at now is the "rolling" or "wrapping" of lower order counters into incrementing higher order counters. Foe e.g. for a 2 digit counter, once the units place reaches 9, the next number will be when the units places wraps around to 0 and the tens place is incremented by 1.
I have a few areas I need your feedback on how this should be done :
- The single value counters should throw an exception when its reached its max value? Or should it automatically wrap around? Or should it allow the user to specify which of these approaches should be taken?
- In case the single counter should throw an exception (as I think), there should be a "reset" method to reset the counter to the start - the caller should handle the exception and call reset (before / after it goes on to increment higher order counters). Would this be a good design?
- When the counter is just initialized - new SingleValueNumericKey() - what should be the value of the counter? Should it be ready to use, with no value, OR should it be the first value in its value set?
- Similar to the prev question, when the "reset" method has been called, what should be the value of the counter?
Please help me with your valuable inputs here. If you have suggestions on the design itself, its more than welcome! If I take away something from this thread, I'm going to refer to it in my commits - so you will have the credit for the suggestion :)
Thanks,
Madhur Tanwani
EDIT: Added my final use case to clarify Jason's questions for all.