Skip to content

Method: MemoryShiftListImpl()

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