> How about something like this:
>
> int lincr = nextNonNegative(incr); // lincr needn't be long now, rename?
> ...
> private static int nextNonNegative(AtomicInteger a) {
> while (true) {
> int current = a.get();
> int next = current == Integer.MAX_VALUE ? 0 : current + 1;
> if (a.compareAndSet(current, next)) {
> return current;
> }
> }
> }
That sounds lovely. Race condition not good. I will try to apply
this principle today.
> And some unrelated questions: Why does idToAlpha use double internally?
Empiricism. When I wrote that snippet originally (~Java 1.3 or 1.4
mind you), using the doubles got me the best measured performance. I
assume that otherwise, casting expenses were incurred in the loop. I
can check it again on a modern JVM.
I suspect I don't have the most efficient conversion algorithm here,
but it's the one I happen to remember. Better/faster ideas are
welcome!
> Why
> does it take a Number argument, when AFAICS it is only called on long
> values?
As to Number, I generally try to write public utility functions to be
broadly accessible ... since I was casting the input argument anyway
for performance, accepting Number seemed a neighborly thing to do.
(Like accepting CharSequence instead of String).
- Rob