This is a simplified example of how it will be used!
A map with n-elements (in this example there are three; One
, Two
and Three
). The map is defined as <String, DecrementableInteger>
were the later is a wrapper for a mutable integer.
class DecrementableInteger
{
private int timeLeftMax;
private int timeLeft;
public DecrementableInteger (int v) {
timeLeft = v;
timeLeftMax = v;
}
public void update (int v) {
timeLeft -= v;
}
public int get () {
return timeLeft;
}
public void reset () {
timeLeft = timeLeftMax;
}
}
Then there is the usage of it.
class Test
{
private final Map<String, DecrementableInteger> integers = new CircleHashMap<>(); //?
private String activeKey = null;
Test () {
integers.put("One", new DecrementableInteger(4));
integers.put("Two", new DecrementableInteger(1));
integers.put("Three", new DecrementableInteger(2));
}
void update () {
if (activeKey == null) {
for (Entry<String, DecrementableInteger> e : integers.entrySet()) {
e.getValue().update (1);
if (e.getValue().get () <= 0) {
System.out.println(e.getKey() + " just finished, resetting.");
e.getValue().reset();
activeKey = e.getKey();
break;
}
}
} else {
System.out.println(activeKey + " did their thing, continuing!");
activeKey = null;
}
}
}
The update
method is called many times every second.
The first iteration will give the following result.
- One => 3
- Two => 0,
Two just finished, resetting.
Two => 1
Iteration broke, continue at Three
next time.
- Three => 1
Iteration done, start over at One
next time.
- One => 2,
- Two => 0,
Two just finished, resetting.
Two => 1
Iteration broke, continue at Three
next time.
- Three => 0,
Three just finished, resetting.
Three => 2
Iteration done, start over at One
next time.
- One => 1
- ...
Meaning the code will need have the following qualities:
- The Map needs to remember its order, it can not shuffle them around.
- The code needs to remember were it left off; currently in
activeKey
- The iteration needs to be able to start from
activeKey
Is there a efficient and neat solution to a CircleList
? If so, what would that solution look like.