summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/esodata/TapeLibrary.java
blob: 4ebfd6c4f5acd59cad31ad9a44070c22144787ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package bjc.utils.esodata;

import java.util.HashMap;
import java.util.Map;

/**
 * A tape changer is essentially a map of tapes.
 *
 * It has a current tape that you can do operations to, but also operations to
 * add/remove other tapes.
 *
 * If there is no tape currently loaded into the changer, all the methods will
 * either return null/false.
 *
 * @param <T>
 *        The element type of the tapes.
 */
public class TapeLibrary<T> implements Tape<T> {
	/* Our backing store of tapes. */
	private final Map<String, Tape<T>> tapes;
	/* The current tape. */
	private Tape<T> currentTape;

	/** Create a new empty tape library. */
	public TapeLibrary() {
		tapes = new HashMap<>();
	}

	@Override
	public T item() {
		if(currentTape == null) return null;

		return currentTape.item();
	}

	@Override
	public void item(final T itm) {
		if(currentTape == null) return;

		currentTape.item(itm);
	}

	@Override
	public int size() {
		if(currentTape == null) return 0;

		return currentTape.size();
	}

	@Override
	public int position() {
		if(currentTape == null) return 0;

		return currentTape.position();
	}

	@Override
	public void insertBefore(final T itm) {
		if(currentTape == null) return;

		currentTape.insertBefore(itm);
	}

	@Override
	public void insertAfter(final T itm) {
		if(currentTape == null) return;

		currentTape.insertAfter(itm);
	}

	@Override
	public T remove() {
		if(currentTape == null) return null;

		return currentTape.remove();
	}

	@Override
	public void first() {
		if(currentTape == null) return;

		currentTape.first();
	}

	@Override
	public void last() {
		if(currentTape == null) return;

		currentTape.last();
	}

	@Override
	public boolean left() {
		return left(1);
	}

	@Override
	public boolean left(final int amt) {
		if(currentTape == null) return false;

		return currentTape.left(amt);
	}

	@Override
	public boolean right() {
		return right(1);
	}

	@Override
	public boolean right(final int amt) {
		if(currentTape == null) return false;

		return currentTape.right(amt);
	}

	/**
	 * Flips the tape.
	 *
	 * The active side becomes inactive, and the inactive side becomes
	 * active.
	 *
	 * If the current tape is not double-sided, does nothing.
	 */
	public void flip() {
		if(currentTape == null) return;

		if(currentTape.isDoubleSided()) {
			((DoubleTape<T>) currentTape).flip();
		}
	}

	@Override
	public boolean isDoubleSided() {
		if(currentTape == null) return false;

		return currentTape.isDoubleSided();
	}

	/**
	 * Check if a tape is currently loaded.
	 *
	 * @return Whether or not a tape is loaded.
	 */
	public boolean isLoaded() {
		return currentTape != null;
	}

	/**
	 * Move to the specified tape in the library.
	 *
	 * Attempting to load a tape that isn't there won't eject the current
	 * tape.
	 *
	 * @param label
	 *        The label of the tape to load.
	 *
	 * @return Whether or not the next tape was loaded.
	 */
	public boolean switchTape(final String label) {
		if(tapes.containsKey(label)) {
			currentTape = tapes.get(label);
			return true;
		}

		return false;
	}

	/**
	 * Inserts a tape into the tape library.
	 *
	 * Any currently loaded tape is ejected.
	 *
	 * The specified tape is loaded.
	 *
	 * Adding a duplicate tape will overwrite any existing types.
	 *
	 * @param label
	 *        The label of the tape to add.
	 *
	 * @param tp
	 *        The tape to insert and load.
	 */
	public void insertTape(final String label, final Tape<T> tp) {
		tapes.put(label, tp);

		currentTape = tp;
	}

	/**
	 * Remove a tape from the library.
	 *
	 * Does nothing if there is not a tape of that name loaded.
	 *
	 * @param label
	 *        The tape to remove.
	 *
	 * @return The removed tape.
	 */
	public Tape<T> removeTape(final String label) {
		return tapes.remove(label);
	}

	/**
	 * Ejects the current tape.
	 *
	 * Does nothing if no tape is loaded.
	 */
	public void eject() {
		currentTape = null;
	}

	/**
	 * Get how many tapes are currently in the library.
	 *
	 * @return How many tapes are currently in the library.
	 */
	public int tapeCount() {
		return tapes.size();
	}

	/**
	 * Check if a specific tape is loaded into the library.
	 *
	 * @param label
	 *        The tape to check for.
	 *
	 * @return Whether or not a tape of that name exists
	 */
	public boolean hasTape(final String label) {
		return tapes.containsKey(label);
	}

	@Override
	public int hashCode() {
		final int prime = 31;

		int result = 1;
		result = prime * result + (currentTape == null ? 0 : currentTape.hashCode());
		result = prime * result + (tapes == null ? 0 : tapes.hashCode());

		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if(this == obj) return true;
		if(obj == null) return false;
		if(!(obj instanceof TapeLibrary<?>)) return false;

		final TapeLibrary<?> other = (TapeLibrary<?>) obj;

		if(currentTape == null) {
			if(other.currentTape != null) return false;
		} else if(!currentTape.equals(other.currentTape)) return false;

		if(tapes == null) {
			if(other.tapes != null) return false;
		} else if(!tapes.equals(other.tapes)) return false;

		return true;
	}

	@Override
	public String toString() {
		return String.format("TapeLibrary [tapes=%s, currentTape='%s']", tapes, currentTape);
	}
}