summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/esodata/TapeLibrary.java
blob: 3794dd599d1edabf297aa32d2786e26e5ff02e85 (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
/* 
 * esodata - data structures and other things, of varying utility
 * Copyright 2022, Ben Culkin
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package bjc.esodata;

import java.util.*;

/**
 * Represents a library of possible tapes, with a single tape 'mounted' or active
 * at a time.
 * 
 * @author Ben Culkin
 *
 * @param <ElementType> The type stored on each tape.
 */
public class TapeLibrary<ElementType> implements TapeView<ElementType>
{
	private final Map<String, Tape<ElementType>> library;
	
	private String            currentLabel;
	private Tape<ElementType> currentTape;

	private boolean allowAutoCreation;
	
	/**
	 * Get a view of this tape library as a map.
	 * 
	 * Modifications to this map will apply to the library, but will not
	 * affect whether a given tape is mounted or not. Be forewarned.
	 * 
	 * @return A view onto this tape library as a map.
	 */
	public Map<String, Tape<ElementType>> asMap()
	{
		return library;
	}

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

	/**
	 * Create a new tape, with a given tape mounted by default.
	 * 
	 * @param label The label of the tape.
	 * @param tape  The tape to mount.
	 */
	public TapeLibrary(String label, Tape<ElementType> tape)
	{
		this();
		library.put(label, tape);
		
		this.currentLabel = label;
		this.currentTape  = tape;
	}

	@Override
	public Tape<ElementType> tapeView()
	{
		return currentTape;
	}
	
	/**
	 * Insert a tape into this library.
	 * 
	 * @param label The label to use for the tape.
	 * @param tape The tape to add.
	 * 
	 * @return The tape which previously had that label, or null if there was not one.
	 */
	public Tape<ElementType> insertTape(String label, Tape<ElementType> tape)
	{
		return library.put(label, tape);
	}
	
	/**
	 * Remove a tape from this library.
	 * 
	 * @param label The label of the tape to remove.
	 * 
	 * @return The tape which had that label, or null if there was not one.
	 */
	public Tape<ElementType> removeTape(String label)
	{
		return library.remove(label);
	}
	
	/**
	 * Check if this library has a tape with a given label.
	 * 
	 * @param label The label of the tape to check for.
	 * 
	 * @return Whether or not the library contains a tape with that label.
	 */
	public boolean hasTape(String label) 
	{
		return allowAutoCreation ? true : library.containsKey(label);
	}
	
	/**
	 * Mount a different tape in the library.
	 * 
	 * @param label The label of the tape to mount.
	 * 
	 * @return True if the tape was successfully mounted, false otherwise.
	 */
	public boolean mountTape(String label)
	{
		if (library.containsKey(label) || allowAutoCreation)
		{
			currentLabel = label;
			currentTape  = library.computeIfAbsent(
						label,
						(ignored) -> new SingleTape<>());
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * Returns the label of the current tape.
	 * 
	 * @return The label of the current tape, or null if no tape is
	 *         currently 'mounted'.
	 */
	public String currentLabel()
	{
		return currentLabel;
	}
	
	/**
	 * Unmount the currently mounted tape.
	 */
	public void ejectTape()
	{
		currentTape  = null;
		currentLabel = null;
	}
	
	/**
	 * Check if this tape library currently allows auto-creation of
	 * non-existing tapes.
	 * 
	 * @return Whether or not auto-creation of tapes is currently allowed.
	 */
	public boolean isAllowAutoCreation() {
		return allowAutoCreation;
	}
	
	/**
	 * Set whether or not this library allows auto-creation of non-existing
	 * tapes.
	 * 
	 * @param allowAutoCreation Whether tape auto-creation is allowed.
	 */
	public void setAllowAutoCreation(boolean allowAutoCreation)
	{
		this.allowAutoCreation = allowAutoCreation;
	}
}