Skip to content

Commit b736cda

Browse files
author
nicolaiparlog
committed
Make 'EqHash' a value-based class
1 parent 2d3113a commit b736cda

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/main/java/org/codefx/libfx/collection/transform/EqHash.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class EqHash<E> {
2424
private final BiPredicate<? super E, ? super E> equals;
2525
private final ToIntFunction<? super E> hash;
2626

27-
public EqHash(E element, BiPredicate<? super E, ? super E> equals, ToIntFunction<? super E> hash) {
27+
private EqHash(E element, BiPredicate<? super E, ? super E> equals, ToIntFunction<? super E> hash) {
2828
// null is allowed as an element
2929
assert equals != null : "The argument 'equals' must not be null.";
3030
assert hash != null : "The argument 'hash' must not be null.";
@@ -34,6 +34,22 @@ public EqHash(E element, BiPredicate<? super E, ? super E> equals, ToIntFunction
3434
this.hash = hash;
3535
}
3636

37+
/**
38+
* @param <E>
39+
* the type of the wrapped elements
40+
* @param element
41+
* the wrapped element; may be null
42+
* @param equals
43+
* the function computing equality of two elements
44+
* @param hash
45+
* the function computing the hash code of the element
46+
* @return an instance of {@link EqHash}
47+
*/
48+
public static <E> EqHash<E> create(
49+
E element, BiPredicate<? super E, ? super E> equals, ToIntFunction<? super E> hash) {
50+
return new EqHash<E>(element, equals, hash);
51+
}
52+
3753
/**
3854
* @return the wrapped element
3955
*/
@@ -63,4 +79,8 @@ public boolean equals(Object obj) {
6379
return equals.test(this.element, other.element);
6480
}
6581

82+
@Override
83+
public String toString() {
84+
return "EqHash [" + element + "]";
85+
}
6686
}

src/main/java/org/codefx/libfx/collection/transform/EqualityTransformingMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected boolean isOuterKey(Object object) {
104104

105105
@Override
106106
protected EqHash<K> transformToInnerKey(K outerKey) throws ClassCastException {
107-
return new EqHash<>(outerKey, equals, hash);
107+
return EqHash.create(outerKey, equals, hash);
108108
}
109109

110110
@Override

src/main/java/org/codefx/libfx/collection/transform/EqualityTransformingSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected boolean isOuterElement(Object object) {
9191

9292
@Override
9393
protected EqHash<E> transformToInner(E outerElement) throws ClassCastException {
94-
return new EqHash<E>(outerElement, equals, hash);
94+
return EqHash.create(outerElement, equals, hash);
9595
}
9696

9797
}

0 commit comments

Comments
 (0)