From 5832ef0286430d04484b70d49c73e081a80ec9c7 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 17 Mar 2017 19:28:59 -0400 Subject: Add more toString/hashCode/equals --- .../src/main/java/bjc/utils/data/LazyPair.java | 29 ++++++++++++---- .../src/main/java/bjc/utils/data/ListHolder.java | 36 +++++++++++++++++++ .../src/main/java/bjc/utils/data/Option.java | 40 ++++++++++++++++++++++ BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 28 +++++++++++++++ .../main/java/bjc/utils/data/SingleIterator.java | 14 ++++++++ .../main/java/bjc/utils/data/SingleSupplier.java | 34 ++++++++++++++++-- .../bjc/utils/data/TopDownTransformIterator.java | 5 +++ BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 36 +++++++++++-------- 8 files changed, 198 insertions(+), 24 deletions(-) (limited to 'BJC-Utils2') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java index d19eed9..ea49b4c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -204,10 +204,12 @@ public class LazyPair implements IPair public int hashCode() { final int prime = 31; int result = 1; + result = prime * result + (leftMaterialized ? 1231 : 1237); result = prime * result + ((leftValue == null) ? 0 : leftValue.hashCode()); result = prime * result + (rightMaterialized ? 1231 : 1237); result = prime * result + ((rightValue == null) ? 0 : rightValue.hashCode()); + return result; } @@ -216,15 +218,28 @@ public class LazyPair implements IPair if(this == obj) return true; if(obj == null) return false; if(getClass() != obj.getClass()) return false; - LazyPair other = (LazyPair) obj; + + LazyPair other = (LazyPair) obj; + if(leftMaterialized != other.leftMaterialized) return false; - if(leftValue == null) { - if(other.leftValue != null) return false; - } else if(!leftValue.equals(other.leftValue)) return false; + + if(leftMaterialized) { + if(leftValue == null) { + if(other.leftValue != null) return false; + } else if(!leftValue.equals(other.leftValue)) return false; + } else { + return false; + } + if(rightMaterialized != other.rightMaterialized) return false; - if(rightValue == null) { - if(other.rightValue != null) return false; - } else if(!rightValue.equals(other.rightValue)) return false; + if(rightMaterialized) { + if(rightValue == null) { + if(other.rightValue != null) return false; + } else if(!rightValue.equals(other.rightValue)) return false; + } else { + return false; + } + return true; } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java index 84ea01d..f460941 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -71,4 +71,40 @@ public class ListHolder implements IHolder { public UnwrappedType unwrap(Function unwrapper) { return unwrapper.apply(heldValues.randItem()); } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("ListHolder [heldValues="); + builder.append(heldValues); + builder.append("]"); + + return builder.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((heldValues == null) ? 0 : heldValues.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; + + ListHolder other = (ListHolder) obj; + + if(heldValues == null) { + if(other.heldValues != null) return false; + } else if(!heldValues.equals(other.heldValues)) return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java index 052e78a..fa25b54 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java @@ -60,4 +60,44 @@ public class Option implements IHolder { return unwrapper.apply(held); } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("Option ["); + + if(held != null) { + builder.append("held="); + builder.append(held); + } + + builder.append("]"); + return builder.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + result = prime * result + ((held == null) ? 0 : held.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; + + Option other = (Option) obj; + + if(held == null) { + if(other.held != null) return false; + } else if(!held.equals(other.held)) return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index 77afa4a..48d3aca 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -100,4 +100,32 @@ public class Pair implements IPair { public String toString() { return "pair[l=" + leftValue.toString() + ", r=" + rightValue.toString() + "]"; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((leftValue == null) ? 0 : leftValue.hashCode()); + result = prime * result + ((rightValue == null) ? 0 : rightValue.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; + + Pair other = (Pair) obj; + + if(leftValue == null) { + if(other.leftValue != null) return false; + } else if(!leftValue.equals(other.leftValue)) return false; + + if(rightValue == null) { + if(other.rightValue != null) return false; + } else if(!rightValue.equals(other.rightValue)) return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java index 4d79b92..0ddb324 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java @@ -2,11 +2,25 @@ package bjc.utils.data; import java.util.Iterator; +/** + * An iterator that will only ever yield one item. + * + * @author EVE + * + * @param + * The type of the item. + */ public class SingleIterator implements Iterator { private T itm; private boolean yielded; + /** + * Create a iterator that yields a single item. + * + * @param item + * The item to yield. + */ public SingleIterator(T item) { itm = item; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java index 76c9be2..0bf1a93 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java @@ -10,6 +10,7 @@ import java.util.function.Supplier; * @author ben * * @param + * The supplied type */ public class SingleSupplier implements Supplier { private static long nextID = 0; @@ -20,8 +21,10 @@ public class SingleSupplier implements Supplier { private long id; - // This is bad practice, but I want to know where the single - // instantiation was, in case of duplicate initiations + /* + * This is bad practice, but I want to know where the single + * instantiation was, in case of duplicate initiations. + */ private Exception instSite; /** @@ -59,4 +62,31 @@ public class SingleSupplier implements Supplier { return source.get(); } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("SingleSupplier ["); + + if(source != null) { + builder.append("source="); + builder.append(source); + builder.append(", "); + } + + builder.append("gotten="); + builder.append(gotten); + builder.append(", id="); + builder.append(id); + builder.append(", "); + + if(instSite != null) { + builder.append("instSite="); + builder.append(instSite); + } + + builder.append("]"); + + return builder.toString(); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java index 2463df9..14548a3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java @@ -10,6 +10,11 @@ import java.util.function.Function; import static bjc.utils.data.TopDownTransformResult.RTRANSFORM; + +/* + * FIXME something is broken in here. fix it. + */ +@SuppressWarnings("javadoc") public class TopDownTransformIterator implements Iterator> { private Function picker; private BiFunction, Consumer>>, ITree> transform; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java index 52414d2..68da7ac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -301,28 +301,34 @@ public class Tree implements ITree { } @Override - public boolean equals(Object other) { - if(!(other instanceof Tree)) return false; + public int hashCode() { + final int prime = 31; + int result = 1; - @SuppressWarnings("unchecked") - Tree otr = (Tree) other; + result = prime * result + childCount; + result = prime * result + ((children == null) ? 0 : children.hashCode()); + result = prime * result + ((data == null) ? 0 : data.hashCode()); - if(!otr.data.equals(data)) return false; - - if(children == null && otr.children == null) return true; + return result; + } - if(children == null && otr.children != null) return false; - if(children != null && otr.children == null) return false; + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; - if(children.getSize() != otr.children.getSize()) return false; + Tree other = (Tree) obj; - int childNo = 0; + if(data == null) { + if(other.data != null) return false; + } else if(!data.equals(other.data)) return false; - for(ITree child : children) { - if(!otr.children.getByIndex(childNo).equals(child)) return false; + if(childCount != other.childCount) return false; - childNo += 1; - } + if(children == null) { + if(other.children != null) return false; + } else if(!children.equals(other.children)) return false; return true; } -- cgit v1.2.3