summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/esodata
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bjc/esodata')
-rw-r--r--src/main/java/bjc/esodata/TapeLibrary.java163
-rw-r--r--src/main/java/bjc/esodata/TapeView.java102
2 files changed, 265 insertions, 0 deletions
diff --git a/src/main/java/bjc/esodata/TapeLibrary.java b/src/main/java/bjc/esodata/TapeLibrary.java
new file mode 100644
index 0000000..922833f
--- /dev/null
+++ b/src/main/java/bjc/esodata/TapeLibrary.java
@@ -0,0 +1,163 @@
+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;
+ } else {
+ 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;
+ }
+}
diff --git a/src/main/java/bjc/esodata/TapeView.java b/src/main/java/bjc/esodata/TapeView.java
new file mode 100644
index 0000000..bfc33a9
--- /dev/null
+++ b/src/main/java/bjc/esodata/TapeView.java
@@ -0,0 +1,102 @@
+package bjc.esodata;
+
+/**
+ * An interface which allows you to view a given type as a tape.
+ *
+ * @author Ben Culkin
+ *
+ * @param <ElementType> The type of element contained in the tape.
+ */
+public interface TapeView<ElementType> extends Tape<ElementType>
+{
+ /**
+ * Return a view of this object as a tape.
+ *
+ * @return A view of this object as a tape.
+ */
+ public Tape<ElementType> tapeView();
+
+ @Override
+ public default ElementType item()
+ {
+ return tapeView().item();
+ }
+
+ @Override
+ public default void item(ElementType itm)
+ {
+ tapeView().item(itm);
+ }
+
+ @Override
+ public default int size()
+ {
+ return tapeView().size();
+ }
+
+ @Override
+ public default int position()
+ {
+ return tapeView().position();
+ }
+
+ @Override
+ public default void insertBefore(ElementType itm)
+ {
+ tapeView().insertBefore(itm);
+ }
+
+ @Override
+ public default void insertAfter(ElementType itm)
+ {
+ tapeView().insertAfter(itm);
+ }
+
+ @Override
+ public default ElementType remove()
+ {
+ return tapeView().remove();
+ }
+
+ @Override
+ public default void first()
+ {
+ tapeView().first();
+ }
+
+ @Override
+ public default void last()
+ {
+ tapeView().last();
+ }
+
+ @Override
+ public default boolean left()
+ {
+ return tapeView().left();
+ }
+
+ @Override
+ public default boolean left(int amt)
+ {
+ return tapeView().left(amt);
+ }
+
+ @Override
+ public default boolean right()
+ {
+ return tapeView().right();
+ }
+
+ @Override
+ public default boolean right(int amt)
+ {
+ return tapeView().right(amt);
+ }
+
+ @Override
+ public default boolean seekTo(int pos)
+ {
+ return tapeView().seekTo(pos);
+ }
+}