diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-07 13:03:39 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-07 13:03:39 -0300 |
| commit | ca2a5c012267c500d9f0fd883c98904d125a64f0 (patch) | |
| tree | dad96199dce1967d6a60ad8419364099eda28348 /base/src/main/java/bjc/utils | |
| parent | 74ba804b29109918f4446cd54092d90c7cf6510e (diff) | |
Refactor tapes
DoubleSided is now its own interface, and the unnecessary tape classes
have been removed
Diffstat (limited to 'base/src/main/java/bjc/utils')
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/DoubleSided.java | 18 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/DoubleTape.java | 19 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/SingleTape.java | 10 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/Tape.java | 7 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/TapeChanger.java | 290 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/TapeLibrary.java | 267 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/esodata/todos.txt | 3 |
7 files changed, 28 insertions, 586 deletions
diff --git a/base/src/main/java/bjc/utils/esodata/DoubleSided.java b/base/src/main/java/bjc/utils/esodata/DoubleSided.java new file mode 100644 index 0000000..83b7c77 --- /dev/null +++ b/base/src/main/java/bjc/utils/esodata/DoubleSided.java @@ -0,0 +1,18 @@ +package bjc.utils.esodata; + +public interface DoubleSided { + /** + * Flips the object. + * + * The active side becomes inactive, and the inactive side becomes + * active. + */ + void flip(); + + /** + * Check which side of the object is active; + * + * @return True if the front side is active, false otherwise. + */ + boolean currentSide(); +} diff --git a/base/src/main/java/bjc/utils/esodata/DoubleTape.java b/base/src/main/java/bjc/utils/esodata/DoubleTape.java index bfc58a4..520ab5e 100644 --- a/base/src/main/java/bjc/utils/esodata/DoubleTape.java +++ b/base/src/main/java/bjc/utils/esodata/DoubleTape.java @@ -24,7 +24,9 @@ package bjc.utils.esodata; * * @author bjculkin */ -public class DoubleTape<T> implements Tape<T> { +public class DoubleTape<T> implements Tape<T>, DoubleSided { + private boolean frontActive; + /* The front-side of the tape. */ private Tape<T> front; /* The back-side of the tape. */ @@ -45,6 +47,8 @@ public class DoubleTape<T> implements Tape<T> { public DoubleTape(final boolean autoExtnd) { front = new SingleTape<>(autoExtnd); back = new SingleTape<>(autoExtnd); + + frontActive = true; } @Override @@ -130,23 +134,20 @@ public class DoubleTape<T> implements Tape<T> { return succ; } - /** - * Flips the tape. - * - * The active side becomes inactive, and the inactive side becomes - * active. - */ + @Override public void flip() { final Tape<T> tmp = front; front = back; back = tmp; + + frontActive = !frontActive; } @Override - public boolean isDoubleSided() { - return true; + public boolean currentSide() { + return frontActive; } @Override diff --git a/base/src/main/java/bjc/utils/esodata/SingleTape.java b/base/src/main/java/bjc/utils/esodata/SingleTape.java index 287f22d..636c04c 100644 --- a/base/src/main/java/bjc/utils/esodata/SingleTape.java +++ b/base/src/main/java/bjc/utils/esodata/SingleTape.java @@ -20,11 +20,6 @@ import java.util.ArrayList; * @author bjculkin */ public class SingleTape<T> implements Tape<T> { - /* - * @NOTE Does this stuff still need to be protected? We're not trying to - * use inheritance to implement tape types any more, so I don't see any - * reason to not have it be private. - */ /* Our backing store. */ protected ArrayList<T> backing; /* Our position in the list. */ @@ -169,11 +164,6 @@ public class SingleTape<T> implements Tape<T> { } @Override - public boolean isDoubleSided() { - return false; - } - - @Override public int hashCode() { final int prime = 31; diff --git a/base/src/main/java/bjc/utils/esodata/Tape.java b/base/src/main/java/bjc/utils/esodata/Tape.java index 91a404a..ae218ac 100644 --- a/base/src/main/java/bjc/utils/esodata/Tape.java +++ b/base/src/main/java/bjc/utils/esodata/Tape.java @@ -112,11 +112,4 @@ public interface Tape<T> { * @return Whether the cursor was moved right. */ boolean right(int amt); - - /** - * Is this tape double sided? - * - * @return Whether or not this tape is double-sided. - */ - boolean isDoubleSided(); } diff --git a/base/src/main/java/bjc/utils/esodata/TapeChanger.java b/base/src/main/java/bjc/utils/esodata/TapeChanger.java deleted file mode 100644 index 2623ad3..0000000 --- a/base/src/main/java/bjc/utils/esodata/TapeChanger.java +++ /dev/null @@ -1,290 +0,0 @@ -package bjc.utils.esodata; - -/** - * A tape changer is essentially a tape 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 TapeChanger<T> implements Tape<T> { - /* Our list of tapes. */ - private Tape<Tape<T>> tapes; - /* The current tape. */ - private Tape<T> currentTape; - - /** Create a new empty tape changer. */ - public TapeChanger() { - tapes = new SingleTape<>(); - } - - /** - * Create a new tape changer with the specified tapes. - * - * @param current - * The tape to mount first. - * - * @param others - * The tapes to put in this tape changer. - */ - @SafeVarargs - public TapeChanger(final Tape<T> current, final Tape<T>... others) { - this(); - - tapes.insertBefore(current); - - for(final Tape<T> tp : others) { - tapes.insertAfter(tp); - tapes.right(); - } - - tapes.first(); - currentTape = tapes.item(); - } - - @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 next tape in the changer. - * - * Attempting to load a tape that isn't there won't eject the current - * tape. - * - * @return Whether or not the next tape was loaded. - */ - public boolean nextTape() { - final boolean succ = tapes.right(); - - if(succ) { - currentTape = tapes.item(); - } - - return succ; - } - - /** - * Move to the previous tape in the changer. - * - * Attempting to load a tape that isn't there won't eject the current - * tape. - * - * @return Whether or not the previous tape was loaded. - */ - public boolean prevTape() { - final boolean succ = tapes.left(); - - if(succ) { - currentTape = tapes.item(); - } - - return succ; - } - - /** - * Inserts a tape into the tape changer. - * - * Any currently loaded tape is ejected, and becomes the previous tape. - * - * The specified tape is loaded. - * - * @param tp - * The tape to insert and load. - */ - public void insertTape(final Tape<T> tp) { - tapes.insertAfter(tp); - tapes.right(); - - currentTape = tapes.item(); - } - - /** - * Removes the current tape. - * - * Does nothing if there is not a tape loaded. - * - * Loads the previous tape, if there is one. - * - * @return The removed tape. - */ - public Tape<T> removeTape() { - if(currentTape == null) return null; - - final Tape<T> tp = tapes.remove(); - currentTape = tapes.item(); - - return tp; - } - - /** - * Ejects the current tape. - * - * Does nothing if no tape is loaded. - */ - public void eject() { - currentTape = null; - } - - /** - * Get how many tapes are currently in the changer. - * - * @return How many tapes are currently in the changer. - */ - public int tapeCount() { - return tapes.size(); - } - - @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 TapeChanger<?>)) return false; - - final TapeChanger<?> other = (TapeChanger<?>) 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("TapeChanger [tapes=%s, currentTape='%s']", tapes, currentTape); - } -} diff --git a/base/src/main/java/bjc/utils/esodata/TapeLibrary.java b/base/src/main/java/bjc/utils/esodata/TapeLibrary.java deleted file mode 100644 index 4ebfd6c..0000000 --- a/base/src/main/java/bjc/utils/esodata/TapeLibrary.java +++ /dev/null @@ -1,267 +0,0 @@ -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); - } -} diff --git a/base/src/main/java/bjc/utils/esodata/todos.txt b/base/src/main/java/bjc/utils/esodata/todos.txt index 5382ade..28af9b7 100644 --- a/base/src/main/java/bjc/utils/esodata/todos.txt +++ b/base/src/main/java/bjc/utils/esodata/todos.txt @@ -1,6 +1,3 @@ -@TODO 10/11/17 Ben Culkin :TapeRefactor - Refactor double-sidedness into its own interface. - @TODO 10/11/17 Ben Culkin :CursorHands Add cursored list/tree data structures with the ability to pick/put the current node. |
