Skip to content

Commit eca17ca

Browse files
author
nicolaiparlog
committed
Improve names and fix comments in equality transforming collections
1 parent 0cdfa09 commit eca17ca

File tree

5 files changed

+35
-32
lines changed

5 files changed

+35
-32
lines changed

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class EqualityTransformingCollectionBuilder<E> {
4141
private BiPredicate<? super E, ? super E> equals;
4242
private ToIntFunction<? super E> hash;
4343

44+
// #begin CONSTRUCTION
45+
4446
private EqualityTransformingCollectionBuilder(Class<? super E> outerKeyTypeToken) {
4547
this.outerKeyTypeToken = outerKeyTypeToken;
4648
// note that the methods from 'Objects' already implement the contract for null-safety
@@ -49,45 +51,43 @@ private EqualityTransformingCollectionBuilder(Class<? super E> outerKeyTypeToken
4951
this.hash = Objects::hashCode;
5052
}
5153

52-
// #begin SET PROPERTIES
53-
5454
/**
55-
* Returns a new builder.
55+
* Returns a new builder for the specified element type.
5656
* <p>
57-
* This method can be called if no type token for the elements can be provided (if it can be, call the preferable
58-
* {@link #forKeyType(Class)} instead). A call might look as follows (for a generic type {@code T}):
59-
*
60-
* <pre>
61-
* EqualityTransformingBuilder.&lt;T&gt; forUnspecifiedKeyType();
62-
* </pre>
57+
* If a type token for the elements can not be provided, call {@link #forKeyTypeUnknown()} instead.
6358
*
6459
* @param <E>
65-
* the type of elements contained in the set created by the builder
60+
* the type of elements contained in the created set
61+
* @param keyTypeToken
62+
* a type token for the elements contained in the created set
6663
* @return a new builder
6764
*/
68-
public static <E> EqualityTransformingCollectionBuilder<E> forUnspecifiedKeyType() {
69-
return new EqualityTransformingCollectionBuilder<>(Object.class);
65+
public static <E> EqualityTransformingCollectionBuilder<E> forKeyType(Class<? super E> keyTypeToken) {
66+
Objects.requireNonNull(keyTypeToken, "The argument 'keyTypeToken' must not be null.");
67+
return new EqualityTransformingCollectionBuilder<>(keyTypeToken);
7068
}
7169

7270
/**
73-
* Returns a new builder to create equality transforming sets for elements of the specified type.
71+
* Returns a new builder for an unknown key type.
7472
* <p>
75-
* If a type token for the keys can not be provided, call {@link #forUnspecifiedKeyType()} instead.
73+
* This is equivalent to calling {@link #forKeyType(Class) forKeyType(Object.class)}. To obtain a builder for
74+
* {@code <E>} you will have to call {@code EqualityTransformingCollectionBuilder.<E> forKeyTypeUnknown()}.
7675
*
7776
* @param <E>
7877
* the type of elements contained in the set created by the builder
79-
* @param keyTypeToken
80-
* a type token for the elements contained in the set created by the builder
8178
* @return a new builder
8279
*/
83-
public static <E> EqualityTransformingCollectionBuilder<E> forKeyType(Class<? super E> keyTypeToken) {
84-
Objects.requireNonNull(keyTypeToken, "The argument 'keyTypeToken' must not be null.");
85-
return new EqualityTransformingCollectionBuilder<>(keyTypeToken);
80+
public static <E> EqualityTransformingCollectionBuilder<E> forKeyTypeUnknown() {
81+
return new EqualityTransformingCollectionBuilder<>(Object.class);
8682
}
8783

84+
// #end CONSTRUCTION
85+
86+
// #begin SET PROPERTIES
87+
8888
/**
8989
* @param equals
90-
* a function determining equality of keys; might be called with null keys
90+
* a function determining equality of elements; might be called with null elements
9191
* @return this builder
9292
*/
9393
public EqualityTransformingCollectionBuilder<E> withEqualsHandlingNull(BiPredicate<? super E, ? super E> equals) {
@@ -98,7 +98,7 @@ public EqualityTransformingCollectionBuilder<E> withEqualsHandlingNull(BiPredica
9898

9999
/**
100100
* @param equals
101-
* a function determining equality of keys; will not be called with null keys
101+
* a function determining equality of elements; will not be called with null elements
102102
* @return this builder
103103
*/
104104
public EqualityTransformingCollectionBuilder<E> withEquals(BiPredicate<? super E, ? super E> equals) {
@@ -119,7 +119,7 @@ public EqualityTransformingCollectionBuilder<E> withEquals(BiPredicate<? super E
119119

120120
/**
121121
* @param hash
122-
* a function computing the hash code of a key; might be called with null keys
122+
* a function computing the hash code of an element; might be called with null elements
123123
* @return this builder
124124
*/
125125
public EqualityTransformingCollectionBuilder<E> withHashHandlingNull(ToIntFunction<? super E> hash) {
@@ -130,7 +130,7 @@ public EqualityTransformingCollectionBuilder<E> withHashHandlingNull(ToIntFuncti
130130

131131
/**
132132
* @param hash
133-
* a function computing the hash code of a key; will not be called with null keys
133+
* a function computing the hash code of an element; will not be called with null elements
134134
* @return this builder
135135
*/
136136
public EqualityTransformingCollectionBuilder<E> withHash(ToIntFunction<? super E> hash) {
@@ -162,7 +162,7 @@ public EqualityTransformingSet<E> buildSet() {
162162
* an empty set which is not otherwise referenced
163163
* @return a new instance of {@link EqualityTransformingSet}
164164
*/
165-
public EqualityTransformingSet<E> buildSetDecorating(Set<Object> emptySet) {
165+
public EqualityTransformingSet<E> buildSet(Set<Object> emptySet) {
166166
return new EqualityTransformingSet<>(emptySet, outerKeyTypeToken, equals, hash);
167167
}
168168

@@ -186,7 +186,7 @@ public <V> EqualityTransformingMap<E, V> buildMap() {
186186
* an empty map which is not otherwise referenced
187187
* @return a new instance of {@link EqualityTransformingMap}
188188
*/
189-
public <V> EqualityTransformingMap<E, V> buildMapDecorating(Map<Object, Object> emptyMap) {
189+
public <V> EqualityTransformingMap<E, V> buildMap(Map<Object, Object> emptyMap) {
190190
return new EqualityTransformingMap<>(emptyMap, outerKeyTypeToken, equals, hash);
191191
}
192192

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* <p>
2424
* The transformations used by this map preserve object identity of outer keys and values. This means if keys and values
2525
* are added to this map, an iteration over it will return the same instances.
26+
* <p>
27+
* {@code EqualityTransformingMap}s are created with a {@link EqualityTransformingCollectionBuilder}.
2628
*
2729
* @param <K>
2830
* the type of keys maintained by this map
@@ -53,7 +55,7 @@ public final class EqualityTransformingMap<K, V> extends AbstractTransformingMap
5355

5456
/**
5557
* Creates a new transforming map.
56-
*
58+
*
5759
* @param innerMap
5860
* the decorated map; must be empty
5961
* @param outerKeyTypeToken

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
* {@link Object#hashCode() hashCode} which are used by the set.
1111
* <p>
1212
* It does so by storing the entries in an inner set and providing a transforming view on them. See the
13-
* {@link org.codefx.libfx.collection.transform package} documentation for general comments on that. Note that instances
14-
* of {@code EqualityTransformingSet}s are created with a {@link EqualityTransformingCollectionBuilder builder}.
13+
* {@link org.codefx.libfx.collection.transform package} documentation for general comments on that.
1514
* <p>
1615
* This implementation mitigates the type safety problems by optionally using a token of the outer type to check
1716
* instances against them. This solves some of the critical situations but not all of them. In those other cases
@@ -23,6 +22,8 @@
2322
* <p>
2423
* The transformations used by this set preserve object identity of outer values. This means if values are added to this
2524
* set, an iteration over it will return the same instances.
25+
* <p>
26+
* {@code EqualityTransformingSet}s are created with a {@link EqualityTransformingCollectionBuilder}.
2627
*
2728
* @param <E>
2829
* the type of elements in this set

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private TransformingCollectionBuilder(Class<? super I> innerTypeToken, Class<? s
4343
}
4444

4545
/**
46-
* Creates a new builder for the specified inner and outer type.
46+
* Returns a new builder for the specified inner and outer type.
4747
*
4848
* @param <I>
4949
* the inner type of the created transforming collection, i.e. the type of the elements contained in the
@@ -63,7 +63,7 @@ public static <I, O> TransformingCollectionBuilder<I, O> forInnerAndOuterType(
6363
}
6464

6565
/**
66-
* Creates a new builder for unknown inner and outer types.
66+
* Returns a new builder for unknown inner and outer types.
6767
* <p>
6868
* This is equivalent to calling {@link #forInnerAndOuterType(Class, Class) forInnerAndOuterType(Object.class,
6969
* Object.class)}. To obtain a builder for {@code <I, O>} you will have to call

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private TransformingMapBuilder(
5656
}
5757

5858
/**
59-
* Creates a new builder for the specified inner and outer key and value types.
59+
* Returns a new builder for the specified inner and outer key and value types.
6060
*
6161
* @param <IK>
6262
* the inner key type of the created transforming map, i.e. the type of the keys contained in the
@@ -88,7 +88,7 @@ public static <IK, OK, IV, OV> TransformingMapBuilder<IK, OK, IV, OV> forTypes(
8888
}
8989

9090
/**
91-
* Creates a new builder for unknown inner and outer key and value types.
91+
* Returns a new builder for unknown inner and outer key and value types.
9292
* <p>
9393
* This is equivalent to calling {@link #forTypes(Class, Class, Class, Class) forTypes(Object.class, Object.class,
9494
* Object.class, Object.class)}. To obtain a builder for {@code <IK, OK, IV, OV>} you will have to call

0 commit comments

Comments
 (0)