@@ -3,29 +3,127 @@ package dev.slne.surf.surfapi.core.api.collection
33import it.unimi.dsi.fastutil.objects.ObjectIterator
44import it.unimi.dsi.fastutil.objects.ObjectSet
55
6+ /* *
7+ * A specialized implementation of [ObjectSet] that transforms elements between two types, `O` and `M`, using
8+ * the provided transformation functions. This enables a dynamic view of a set where elements are transformed
9+ * on-the-fly during iteration or when performing operations.
10+ *
11+ * @param O The type of the elements in the underlying set.
12+ * @param M The type of the transformed elements in this set.
13+ * @property fromSet The underlying set containing elements of type `O`.
14+ * @property toTransformer A function that transforms elements of type `O` to type `M`. Can return null to filter out elements.
15+ * @property fromTransformer A function that transforms elements of type `M` to type `O`. Can return null to filter out elements.
16+ */
617class TransformingObjectSet <O , M >(
718 private val fromSet : ObjectSet <O >,
819 private val toTransformer : (O ) -> M ? ,
920 private val fromTransformer : (M ) -> O ? ,
1021) : ObjectSet<M> {
22+
23+ /* *
24+ * Returns an iterator that transforms elements from the underlying set during iteration.
25+ *
26+ * @return An iterator over the transformed elements of type `M`.
27+ */
1128 override fun iterator () = object : ObjectIterator <M > {
1229 private val iterator = fromSet.iterator()
1330 override fun remove () = iterator.remove()
1431 override fun next (): M ? = toTransformer(iterator.next())
1532 override fun hasNext () = iterator.hasNext()
1633 }
1734
35+ /* *
36+ * Adds a transformed element to the underlying set.
37+ *
38+ * @param element The element of type `M` to add.
39+ * @return `true` if the underlying set was modified, `false` otherwise.
40+ */
1841 override fun add (element : M ? ) = fromSet.add(transformFrom(element))
42+
43+ /* *
44+ * Removes a transformed element from the underlying set.
45+ *
46+ * @param element The element of type `M` to remove.
47+ * @return `true` if the element was removed, `false` otherwise.
48+ */
1949 override fun remove (element : M ) = fromSet.remove(transformFrom(element))
20- override fun addAll (elements : Collection <M ?>) = fromSet.addAll(elements.mapNotNull(::transformFrom))
21- override fun removeAll (elements : Collection <M ?>) = fromSet.removeAll(elements.mapNotNull(::transformFrom))
22- override fun retainAll (elements : Collection <M ?>) = fromSet.retainAll(elements.mapNotNull(::transformFrom))
50+
51+ /* *
52+ * Adds all transformed elements from the given collection to the underlying set.
53+ *
54+ * @param elements A collection of elements of type `M`.
55+ * @return `true` if the underlying set was modified, `false` otherwise.
56+ */
57+ override fun addAll (elements : Collection <M ?>) =
58+ fromSet.addAll(elements.mapNotNull(::transformFrom))
59+
60+ /* *
61+ * Removes all transformed elements in the given collection from the underlying set.
62+ *
63+ * @param elements A collection of elements of type `M`.
64+ * @return `true` if the underlying set was modified, `false` otherwise.
65+ */
66+ override fun removeAll (elements : Collection <M ?>) =
67+ fromSet.removeAll(elements.mapNotNull(::transformFrom))
68+
69+ /* *
70+ * Retains only the transformed elements in the given collection in the underlying set.
71+ *
72+ * @param elements A collection of elements of type `M`.
73+ * @return `true` if the underlying set was modified, `false` otherwise.
74+ */
75+ override fun retainAll (elements : Collection <M ?>) =
76+ fromSet.retainAll(elements.mapNotNull(::transformFrom))
77+
78+ /* *
79+ * Removes all elements from the underlying set.
80+ */
2381 override fun clear () = fromSet.clear()
82+
83+ /* *
84+ * Returns the number of elements in the transformed set.
85+ *
86+ * @return The size of the underlying set.
87+ */
2488 override val size: Int get() = fromSet.size
89+
90+ /* *
91+ * Checks if the transformed set is empty.
92+ *
93+ * @return `true` if the underlying set is empty, `false` otherwise.
94+ */
2595 override fun isEmpty () = fromSet.isEmpty()
96+
97+ /* *
98+ * Checks if the transformed set contains the specified element.
99+ *
100+ * @param element The element of type `M` to check.
101+ * @return `true` if the element is in the transformed set, `false` otherwise.
102+ */
26103 override fun contains (element : M ? ) = fromSet.contains(transformFrom(element))
27- override fun containsAll (elements : Collection <M ?>) = fromSet.containsAll(elements.mapNotNull(::transformFrom))
28104
105+ /* *
106+ * Checks if the transformed set contains all elements in the specified collection.
107+ *
108+ * @param elements A collection of elements of type `M`.
109+ * @return `true` if all elements are in the transformed set, `false` otherwise.
110+ */
111+ override fun containsAll (elements : Collection <M ?>) =
112+ fromSet.containsAll(elements.mapNotNull(::transformFrom))
113+
114+ /* *
115+ * Transforms an element of type `M` to type `O` using the provided [fromTransformer].
116+ *
117+ * @param element The element of type `M`.
118+ * @return The transformed element of type `O`, or `null` if the transformation is not possible.
119+ */
29120 private fun transformFrom (element : M ? ) = if (element != null ) fromTransformer(element) else null
121+
122+ /* *
123+ * Transforms an element of type `O` to type `M` using the provided [toTransformer].
124+ *
125+ * @param element The element of type `O`.
126+ * @return The transformed element of type `M`, or `null` if the transformation is not possible.
127+ */
30128 private fun transformTo (element : O ? ) = if (element != null ) toTransformer(element) else null
31- }
129+ }
0 commit comments