diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-17 09:57:54 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-17 09:57:54 -0400 |
| commit | 6ea2b6868eb496f39bbbfb2d13662cb397d4d799 (patch) | |
| tree | 343c6e0012e4c89bea75110171dafd08ad11d782 | |
| parent | 7f59d0b9de4536705b3122cb5a85d9c9f85846a3 (diff) | |
More toString/hashCode/equals
5 files changed, 153 insertions, 5 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java index 7131114..1dffbc1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java @@ -2,14 +2,42 @@ package bjc.utils.data; import java.util.Iterator; +/** + * An iterator that repeats elements from a provided iterable. + * + * @author EVE + * + * @param <E> + * The type of the iterable. + */ public class CircularIterator<E> implements Iterator<E> { + /* + * The iterable, and our current iterator into it. + */ private Iterable<E> source; private Iterator<E> curr; + /* + * Our current element. + */ private E curElm; + /* + * Should we actually get new iterators, or just repeat the last + * element? + */ private boolean doCircle; + /** + * Create a new circular iterator. + * + * @param src + * The iterable to iterate from. + * + * @param circ + * Should we actually do circular iteration, or just + * repeat the terminal element? + */ public CircularIterator(Iterable<E> src, boolean circ) { source = src; curr = source.iterator(); @@ -17,6 +45,12 @@ public class CircularIterator<E> implements Iterator<E> { doCircle = circ; } + /** + * Create a new circular iterator that does actual circular iteration. + * + * @param src + * The iterable to iterate from. + */ public CircularIterator(Iterable<E> src) { this(src, true); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java index d34f752..334beef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -132,4 +132,58 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { return merger.apply(leftVal, rightVal); } + + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + result = prime * result + (isLeft ? 1231 : 1237); + result = prime * result + ((leftVal == null) ? 0 : leftVal.hashCode()); + result = prime * result + ((rightVal == null) ? 0 : rightVal.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; + + Either<?, ?> other = (Either<?, ?>) obj; + + if(isLeft != other.isLeft) return false; + + if(leftVal == null) { + if(other.leftVal != null) return false; + } else if(!leftVal.equals(other.leftVal)) return false; + + if(rightVal == null) { + if(other.rightVal != null) return false; + } else if(!rightVal.equals(other.rightVal)) return false; + + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("Either ["); + + if(leftVal != null) { + builder.append("leftVal="); + builder.append(leftVal); + builder.append(", "); + } + + if(rightVal != null) { + builder.append("rightVal="); + builder.append(rightVal); + builder.append(", "); + } + + return builder.toString(); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java index 9c4ea45..c356e5a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -63,11 +63,6 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { return true; } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { final int prime = 31; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java index d4d350a..7cf16fd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -131,4 +131,41 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { return unwrapper.apply(heldValue); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((actions == null) ? 0 : actions.hashCode()); + result = prime * result + ((heldValue == null) ? 0 : heldValue.hashCode()); + result = prime * result + (valueMaterialized ? 1231 : 1237); + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + Lazy<?> other = (Lazy<?>) obj; + + if(valueMaterialized != other.valueMaterialized) return false; + + if(valueMaterialized) { + if(heldValue == null) { + if(other.heldValue != null) return false; + } else if(!heldValue.equals(other.heldValue)) return false; + } else { + return false; + } + + if(actions == null) { + if(other.actions != null) return false; + } else if(actions.getSize() > 0 || other.actions.getSize() > 0) return false; + + return true; + } } 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 59df1b1..d19eed9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -199,4 +199,32 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> return sb.toString(); } + + @Override + 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; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + 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(rightMaterialized != other.rightMaterialized) return false; + if(rightValue == null) { + if(other.rightValue != null) return false; + } else if(!rightValue.equals(other.rightValue)) return false; + return true; + } } |
