From 338b2b16b6934d27dc4cd536fb5527b71985de4f Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 17 Mar 2017 19:46:55 -0400 Subject: Add more toString/hashCode/equals --- .../src/main/java/bjc/utils/esodata/Directory.java | 4 +- .../main/java/bjc/utils/esodata/DoubleTape.java | 43 +++++++++++++++++- .../main/java/bjc/utils/esodata/PushdownMap.java | 36 +++++++++++++++ .../main/java/bjc/utils/esodata/QueueStack.java | 41 +++++++++++++++-- .../java/bjc/utils/esodata/SimpleDirectory.java | 43 ++++++++++++++++++ .../main/java/bjc/utils/esodata/SimpleStack.java | 49 ++++++++++++++++---- .../main/java/bjc/utils/esodata/SingleTape.java | 45 +++++++++++++++++- .../java/bjc/utils/esodata/SpaghettiStack.java | 30 ++++++++++++ .../main/java/bjc/utils/esodata/TapeChanger.java | 53 +++++++++++++++++++--- .../main/java/bjc/utils/esodata/TapeLibrary.java | 52 +++++++++++++++++++-- .../java/bjc/utils/esodata/UnifiedDirectory.java | 41 +++++++++++++++++ 11 files changed, 409 insertions(+), 28 deletions(-) (limited to 'BJC-Utils2/src') diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java index 79ae031..05f686a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java @@ -6,9 +6,9 @@ package bjc.utils.esodata; * What's useful about this is that you can hand sub-directories to people and * be able to ensure that they can't write outside of it. * - * @param K + * @param * The key type of the map. - * @param V + * @param * The value type of the map. */ public interface Directory { diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java index 6e256b6..ec24431 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java @@ -19,7 +19,7 @@ package bjc.utils.esodata; * * Flip refers to the entire tape for 'obvious' reasons. * - * @param T + * @param * The element type of the tape. * @author bjculkin */ @@ -217,4 +217,45 @@ public class DoubleTape implements Tape { public boolean isDoubleSided() { return true; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((back == null) ? 0 : back.hashCode()); + result = prime * result + ((front == null) ? 0 : front.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + DoubleTape other = (DoubleTape) obj; + + if(back == null) { + if(other.back != null) return false; + } else if(!back.equals(other.back)) return false; + + if(front == null) { + if(other.front != null) return false; + } else if(!front.equals(other.front)) return false; + + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("DoubleTape [front="); + builder.append(front); + builder.append(", back="); + builder.append(back); + builder.append("]"); + + return builder.toString(); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java index 4b758e3..7587b86 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java @@ -117,4 +117,40 @@ public class PushdownMap implements IMap public IList valueList() { return backing.valueList().map(stk -> stk.top()); } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + result = prime * result + ((backing == null) ? 0 : backing.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + PushdownMap other = (PushdownMap) obj; + + if(backing == null) { + if(other.backing != null) return false; + } else if(!backing.equals(other.backing)) return false; + + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("PushdownMap [backing="); + builder.append(backing); + builder.append("]"); + + return builder.toString(); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java index a2c2f32..005c90e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java @@ -29,14 +29,14 @@ public class QueueStack extends Stack { @Override public T pop() { if(backing.isEmpty()) throw new StackUnderflowException(); - + return backing.remove(); } @Override public T top() { if(backing.isEmpty()) throw new StackUnderflowException(); - + return backing.peek(); } @@ -49,15 +49,46 @@ public class QueueStack extends Stack { public boolean empty() { return backing.size() == 0; } - + @Override public String toString() { - return backing.toString(); + StringBuilder builder = new StringBuilder(); + + builder.append("QueueStack [backing="); + builder.append(backing); + builder.append("]"); + + return builder.toString(); } - + @SuppressWarnings("unchecked") @Override public T[] toArray() { return (T[]) backing.toArray(); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((backing == null) ? 0 : backing.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + QueueStack other = (QueueStack) obj; + + if(backing == null) { + if(other.backing != null) return false; + } else if(!backing.equals(other.backing)) return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java index 3c1ced5..741b33d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java @@ -57,4 +57,47 @@ public class SimpleDirectory implements Directory { public V putKey(K key, V val) { return data.put(key, val); } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + result = prime * result + ((children == null) ? 0 : children.hashCode()); + result = prime * result + ((data == null) ? 0 : data.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + SimpleDirectory other = (SimpleDirectory) obj; + + if(children == null) { + if(other.children != null) return false; + } else if(!children.equals(other.children)) return false; + + if(data == null) { + if(other.data != null) return false; + } else if(!data.equals(other.data)) return false; + + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("SimpleDirectory [children="); + builder.append(children); + builder.append(", data="); + builder.append(data); + builder.append("]"); + + return builder.toString(); + } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java index ec01280..1de06a6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java @@ -6,7 +6,7 @@ import java.util.LinkedList; /** * Simple implementation of a stack. * - * @param T + * @param * The datatype stored in the stack. * @author Ben Culkin */ @@ -29,14 +29,14 @@ public class SimpleStack extends Stack { @Override public T pop() { if(backing.isEmpty()) throw new StackUnderflowException(); - + return backing.pop(); } @Override public T top() { if(backing.isEmpty()) throw new StackUnderflowException(); - + return backing.peek(); } @@ -49,14 +49,45 @@ public class SimpleStack extends Stack { public boolean empty() { return backing.size() == 0; } - - @Override - public String toString() { - return backing.toString(); - } - + @SuppressWarnings("unchecked") public T[] toArray() { return (T[]) backing.toArray(); } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + result = prime * result + ((backing == null) ? 0 : backing.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + SimpleStack other = (SimpleStack) obj; + + if(backing == null) { + if(other.backing != null) return false; + } else if(!backing.equals(other.backing)) return false; + + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("SimpleStack [backing="); + builder.append(backing); + builder.append("]"); + + return builder.toString(); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java index 22b869e..056cab4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java @@ -11,11 +11,12 @@ import java.util.ArrayList; * unbounded to the right, but in practice bounded by available memory. * * You can choose whether or not you want the tape to automatically extend - * itself to the right with null elements by specifiying its auto-extension + * itself to the right with null elements by specifying its auto-extension * policy. * - * @param T + * @param * The element type of the tape. + * * @author bjculkin */ public class SingleTape implements Tape { @@ -204,4 +205,44 @@ public class SingleTape implements Tape { public boolean isDoubleSided() { return false; } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + result = prime * result + ((backing == null) ? 0 : backing.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + SingleTape other = (SingleTape) obj; + + if(backing == null) { + if(other.backing != null) return false; + } else if(!backing.equals(other.backing)) return false; + + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("SingleTape [backing="); + builder.append(backing); + builder.append(", pos="); + builder.append(pos); + builder.append(", autoExtend="); + builder.append(autoExtend); + builder.append("]"); + + return builder.toString(); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java index fdf9cc8..c4c469f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java @@ -66,4 +66,34 @@ class SpaghettiStack extends Stack { public T[] toArray() { return (T[]) Stream.concat(Arrays.stream(parent.toArray()), Arrays.stream(backing.toArray())).toArray(); } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + result = prime * result + ((backing == null) ? 0 : backing.hashCode()); + result = prime * result + ((parent == null) ? 0 : parent.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + SpaghettiStack other = (SpaghettiStack) obj; + + if(backing == null) { + if(other.backing != null) return false; + } else if(!backing.equals(other.backing)) return false; + + if(parent == null) { + if(other.parent != null) return false; + } else if(!parent.equals(other.parent)) return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java index fa3e61c..64c0460 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java @@ -9,7 +9,7 @@ package bjc.utils.esodata; * If there is no tape currently loaded into the changer, all the methods will * either return null/false. * - * @param T + * @param * The element type of the tapes. */ public class TapeChanger implements Tape { @@ -26,9 +26,9 @@ public class TapeChanger implements Tape { /** * Create a new tape changer with the specified tapes. * - * The first tape in the list will be mounted. - * - * @param taps + * @param current + * The tape to mount first. + * @param others * The tapes to put in this tape changer. */ @SafeVarargs @@ -275,8 +275,8 @@ public class TapeChanger implements Tape { * * The specified tape is loaded. * - * @param The - * tape to insert and load. + * @param tp + * The tape to insert and load. */ public void insertTape(Tape tp) { tapes.insertAfter(tp); @@ -320,4 +320,45 @@ public class TapeChanger implements Tape { public int tapeCount() { return tapes.size(); } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("TapeChanger [tapes="); + builder.append(tapes); + builder.append(", currentTape="); + builder.append(currentTape); + builder.append("]"); + + return builder.toString(); + } + + @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(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + 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; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java index 732f579..0bae303 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java @@ -12,7 +12,7 @@ import java.util.Map; * If there is no tape currently loaded into the changer, all the methods will * either return null/false. * - * @param T + * @param * The element type of the tapes. */ public class TapeLibrary implements Tape { @@ -240,9 +240,12 @@ public class TapeLibrary implements Tape { * The specified tape is loaded. * * Adding a duplicate tape will overwrite any existing types. + * + * @param label + * The label of the tape to add. * - * @param The - * tape to insert and load. + * @param tp + * The tape to insert and load. */ public void insertTape(String label, Tape tp) { tapes.put(label, tp); @@ -293,4 +296,47 @@ public class TapeLibrary implements Tape { public boolean hasTape(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(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + 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() { + StringBuilder builder = new StringBuilder(); + + builder.append("TapeLibrary [tapes="); + builder.append(tapes); + builder.append(", currentTape="); + builder.append(currentTape); + builder.append("]"); + + return builder.toString(); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java index d4c5081..61ec72b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java @@ -63,4 +63,45 @@ public class UnifiedDirectory implements Directory { return data.put(key, val); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((children == null) ? 0 : children.hashCode()); + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + UnifiedDirectory other = (UnifiedDirectory) obj; + + if(children == null) { + if(other.children != null) return false; + } else if(!children.equals(other.children)) return false; + + if(data == null) { + if(other.data != null) return false; + } else if(!data.equals(other.data)) return false; + + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("UnifiedDirectory [children="); + builder.append(children); + builder.append(", data="); + builder.append(data); + builder.append("]"); + + return builder.toString(); + } } \ No newline at end of file -- cgit v1.2.3