forked from squeek502/SpiceOfLife
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'Midnight145/master' into dev
- Loading branch information
Showing
4 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/squeek/spiceoflife/foodtracker/foodqueue/FixedHungerQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package squeek.spiceoflife.foodtracker.foodqueue; | ||
|
||
import squeek.spiceoflife.foodtracker.FoodEaten; | ||
|
||
public class FixedHungerQueue extends FixedSizeQueue { | ||
|
||
protected int hunger; | ||
protected int hungerOverflow; | ||
|
||
public FixedHungerQueue(int limit) { | ||
super(limit); | ||
} | ||
|
||
@Override | ||
public boolean add(FoodEaten foodEaten) { | ||
boolean added = super.add(foodEaten); | ||
if (added) { | ||
hunger += foodEaten.foodValues.hunger; | ||
trimToMaxSize(); | ||
} | ||
return added; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
super.clear(); | ||
hunger = hungerOverflow = 0; | ||
} | ||
|
||
public int hunger() { | ||
return hunger; | ||
} | ||
|
||
@Override | ||
protected void trimToMaxSize() { | ||
while (hunger > limit && peekFirst() != null) { | ||
hunger -= 1; | ||
hungerOverflow += 1; | ||
|
||
while (hungerOverflow >= peekFirst().foodValues.hunger) { | ||
hungerOverflow -= removeFirst().foodValues.hunger; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters