summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-03-17 19:28:59 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-03-17 19:28:59 -0400
commit5832ef0286430d04484b70d49c73e081a80ec9c7 (patch)
tree04ab56561978c14415f71b934f7d8a3aaa1a037e /BJC-Utils2/src
parentc8593896ba0d93c9512a71c2e70d1d503627b1b0 (diff)
Add more toString/hashCode/equals
Diffstat (limited to 'BJC-Utils2/src')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java29
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java36
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Option.java40
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Pair.java28
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java14
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java34
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java5
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Tree.java36
8 files changed, 198 insertions, 24 deletions
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<LeftType, RightType> implements IPair<LeftType, RightType>
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<LeftType, RightType> implements IPair<LeftType, RightType>
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<ContainedType> implements IHolder<ContainedType> {
public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> 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<ContainedType> implements IHolder<ContainedType> {
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<LeftType, RightType> implements IPair<LeftType, RightType> {
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 <T>
+ * The type of the item.
+ */
public class SingleIterator<T> implements Iterator<T> {
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 <T>
+ * The supplied type
*/
public class SingleSupplier<T> implements Supplier<T> {
private static long nextID = 0;
@@ -20,8 +21,10 @@ public class SingleSupplier<T> implements Supplier<T> {
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<T> implements Supplier<T> {
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<ContainedType> implements Iterator<ITree<ContainedType>> {
private Function<ContainedType, TopDownTransformResult> picker;
private BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> 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<ContainedType> implements ITree<ContainedType> {
}
@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<ContainedType> otr = (Tree<ContainedType>) 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<ContainedType> 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;
}