Skip to content

Commit

Permalink
forgot to add generated id to used ids
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZZank committed Oct 23, 2024
1 parent e8917f8 commit 6c921d5
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
public class SimpleDatabase<T> extends AbstractDatabase<T> {

private final IntOpenHashSet ids = new IntOpenHashSet();
/**
* the "smallest acceptable" id, any id smaller than this is considered as used and can
* be skipped when searching for new id, this condition can be maintained by:
* - refresh lowerBound after new id is generated: {@link #nextID()}
* - refresh lowerBound after one id is removed: {@link #removeID(int)}
*/
private int lowerBound = 0;

@Override
public synchronized int nextID() {
for (int i = lowerBound; i < Integer.MAX_VALUE ; i++) {
if (!ids.contains(i)) {
ids.add(i);
lowerBound = i + 1;
return i;
}
}
throw new IllegalStateException(String.format("All integer id from 0 to %s have been consumed", Integer.MAX_VALUE));
throw new IllegalStateException(String.format("All integer id from 0 to %s have been consumed, it's abnormal", Integer.MAX_VALUE));
}

@Override
Expand Down

0 comments on commit 6c921d5

Please sign in to comment.