I want to make a utility function or class which provides incremeneted primary key integers provided an upper and lower range
I came up with this:-
public class PrimaryGen
{
private static int upper_range;
private static int lower_range;
private int counter;
PrimaryGen(int upper,int lower)
{
upper_range=upper;
lower_range=lower;
counter=lower_range-1;
}
int getNextVal()
{
if(counter>upper_range)
{
throw new RuntimeException("Index out of range");
}
counter++;
return counter;
}
}
The main problem that I have is that this class may get accessed two times once for setting the range and inserting primary keys to the table and next time by other function to do query on the table where the int primary key range will be passed and the next incremental values will be generated if the ranges are correct.
What changes should I make to this so that the above logic runs correctly?