Skip to content

Method: getRound(int, boolean)

1: package de.fhdw.gaming.ipspiel23.memory;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: /**
7: * implements {@link IGameMemory}.
8: *
9: * @param <ROUND_DATA> Round-Data, stored in the memory.
10: */
11: public class GameMemory<ROUND_DATA> implements IGameMemory<ROUND_DATA> {
12:
13: /**
14: * List where the round-data is stored.
15: */
16: private final List<ROUND_DATA> data;
17:
18: /**
19: * capacity of the memory, contains if it's unlimited and if it's the default.
20: */
21: private final IGameMemoryCapacity memoryCapacity;
22:
23: /**
24: * Constructor.
25: *
26: * @param capacity capacity.
27: */
28: public GameMemory(final IGameMemoryCapacity capacity) {
29: this.memoryCapacity = capacity;
30: this.data = new ArrayList<>();
31: }
32:
33: @Override
34: public int size() {
35: return this.data.size();
36: }
37:
38: @Override
39: public int capacity() {
40: return this.memoryCapacity.getCapacity();
41: }
42:
43: @Override
44: public void add(final ROUND_DATA item) {
45: if (!this.memoryCapacity.isUnlimited() && this.data.size() >= capacity()) {
46: this.data.remove(0);
47: }
48: this.data.add(item);
49: }
50:
51: @Override
52: public void clear() {
53: this.data.clear();
54: }
55:
56: @Override
57: /**
58: * Returns the round data at the specified index.
59: *
60: * @param index the index of the round data to retrieve.
61: * @param fromEnd if true, retrieves the round data from the end of the buffer.
62: * @return the round data at the specified index, or null if the buffer is empty.
63: */
64: public ROUND_DATA getRound(final int index, final boolean fromEnd) {
65:• if (data.isEmpty()) {
66: return null;
67: }
68:• if (fromEnd) {
69: return data.get(data.size() - index - 1);
70: } else {
71: return data.get(index);
72: }
73: }
74:
75: /**
76: * Returns the round data at the specified index.
77: * Counted from the end of the buffer.
78: *
79: * @param index the index of the round data to retrieve.
80: * @return the round data at the specified index, or null if the buffer is empty.
81: */
82: @Override
83: public ROUND_DATA getRound(final int index) {
84: return getRound(index, true);
85: }
86:
87: /**
88: * Returns the latest round data in the buffer.
89: *
90: * @return the latest round data in the buffer, or null if the buffer is empty.
91: */
92: @Override
93: public ROUND_DATA getLatestRound() {
94: return getRound(0, true);
95: }
96: }