Skip to content

Package: MemoryShiftListImpl

MemoryShiftListImpl

nameinstructionbranchcomplexitylinemethod
MemoryShiftListImpl()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
MemoryShiftListImpl(int)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getLength()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
push(Optional)
M: 1 C: 56
98%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 0 C: 11
100%
M: 0 C: 1
100%
pushMyMove(Move)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
pushOpponentMove(Move)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setLength(int)
M: 0 C: 26
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
show()
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.gaming.memory.impl;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5: import java.util.Optional;
6:
7: import de.fhdw.gaming.core.domain.Move;
8: import de.fhdw.gaming.memory.ShiftList;
9:
10: /**
11: * Organises a list of Optional doubles, allowing to set a maximum size for is.
12: */
13: public class MemoryShiftListImpl implements ShiftList {
14:
15: /**
16: * .
17: */
18: private Move<?, ?> lastMyMoveUsed;
19:
20: /**
21: * .
22: */
23: private Move<?, ?> lastOpponentMoveUsed;
24:
25: /**
26: * The number of outcomes this list can save.
27: */
28: private int length = 0;
29:
30: /**
31: * Saves the outcomes of a player given as Optional<Double> as List.
32: */
33: private List<MemoryPairImpl> memoryPair = new ArrayList<MemoryPairImpl>();
34:
35: /**
36: * Creates a MemoryShiftListImpl object with the default length of 0.
37: */
38: public MemoryShiftListImpl() {
39: }
40:
41: /**
42: * Creates a MemoryShiftListImpl object able to save up to length outcomes.
43: *
44: * @param length
45: */
46: public MemoryShiftListImpl(int length) {
47: this.length = length;
48: }
49:
50: /**
51: * Adds an outcome as Optional<Double> to the list. If the list is already full,
52: * the oldest outcome will be deleted before adding the new one.
53: *
54: * @param input
55: */
56: @Override
57: public void push(Optional<Double> input) {
58:
59: final MemoryPairImpl pairToAdd = new MemoryPairImpl(input, lastMyMoveUsed, lastOpponentMoveUsed);
60:
61:• if (length == -1) {
62: memoryPair.add(pairToAdd);
63:• } else if (length < -1) {
64: // throw error
65:• } else if (length == 0) {
66:
67:• } else if (length == memoryPair.size()) {
68: memoryPair.remove(0);
69: memoryPair.add(pairToAdd);
70:• } else if (length > memoryPair.size()) {
71: memoryPair.add(pairToAdd);
72: }
73: }
74:
75: /**
76: * Adds an outcome as Optional<Double> to the list. If the list is already full,
77: * the oldest outcome will be deleted before adding the new one.
78: *
79: * @param moveUsed
80: */
81: public void pushMyMove(Move<?, ?> moveUsed) {
82: lastMyMoveUsed = moveUsed;
83: }
84:
85: /**
86: * Adds an outcome as Optional<Double> to the list. If the list is already full,
87: * the oldest outcome will be deleted before adding the new one.
88: *
89: * @param moveUsed
90: */
91: public void pushOpponentMove(Move<?, ?> moveUsed) {
92: lastOpponentMoveUsed = moveUsed;
93: }
94:
95: /**
96: * Returns the list of all recorded outcomes as Optional<Double>.
97: */
98: @Override
99: public List<MemoryPairImpl> show() {
100:• if (!this.memoryPair.isEmpty()) {
101: return this.memoryPair;
102: } else {
103: return new ArrayList<>();
104: }
105: }
106:
107: /**
108: * Determines how many outcomes can be saved in the list displyyed by the show()
109: * method. If there are too many outcomes saved this will delete the oldest
110: * outcome until the number of outcomes is equal to the length.
111: *
112: * @param length
113: */
114: @Override
115: public void setLength(int length) {
116: this.length = length;
117:• if (length < -1) {
118: throw new IllegalArgumentException(
119: "The length value entered cannot be used. The value must be >= -1.");
120: }
121:• while (length < memoryPair.size() && length != -1) {
122: memoryPair.remove(0);
123: }
124: }
125:
126: /**
127: * Returns the number of outcomes that can be saves.
128: */
129: @Override
130: public int getLength() {
131: return length;
132: }
133:
134: }