11package com .ss .rlib .common .util .dictionary ;
22
3- import com .ss .rlib .common .function .NotNullBiConsumer ;
4- import com .ss .rlib .common .function .NotNullConsumer ;
5- import com .ss .rlib .common .function .NotNullNullableBiFunction ;
6- import com .ss .rlib .common .function .NotNullTripleConsumer ;
3+ import com .ss .rlib .common .function .*;
74import org .jetbrains .annotations .NotNull ;
85import org .jetbrains .annotations .Nullable ;
96
@@ -156,6 +153,30 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
156153 }
157154 }
158155
156+ /**
157+ * Get the value from a function for this dictionary under block {@link ConcurrentObjectDictionary#readLock()}.
158+ *
159+ * @param first the first argument.
160+ * @param second the second argument.
161+ * @param function the function.
162+ * @param <F> the first argument's type.
163+ * @param <S> the second argument's type.
164+ * @param <R> the result's type.
165+ * @return the result of the function.
166+ */
167+ default <F , S , R > @ Nullable R getInReadLock (
168+ @ NotNull F first ,
169+ @ NotNull S second ,
170+ @ NotNull NotNullNullableTripleFunction <ConcurrentObjectDictionary <K , V >, F , S , R > function
171+ ) {
172+ var stamp = readLock ();
173+ try {
174+ return function .apply (this , first , second );
175+ } finally {
176+ readUnlock (stamp );
177+ }
178+ }
179+
159180 /**
160181 * Get the value from a function for this dictionary under block {@link ConcurrentObjectDictionary#writeLock()}.
161182 *
@@ -177,6 +198,30 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
177198 }
178199 }
179200
201+ /**
202+ * Get the value from a function for this dictionary under block {@link ConcurrentObjectDictionary#writeLock()}.
203+ *
204+ * @param first the first argument.
205+ * @param second the second argument.
206+ * @param function the function.
207+ * @param <F> the first argument's type.
208+ * @param <S> the second argument's type.
209+ * @param <R> the result's type.
210+ * @return the result of the function.
211+ */
212+ default <F , S , R > @ Nullable R getInWriteLock (
213+ @ NotNull F first ,
214+ @ NotNull S second ,
215+ @ NotNull NotNullNullableTripleFunction <ConcurrentObjectDictionary <K , V >, F , S , R > function
216+ ) {
217+ var stamp = writeLock ();
218+ try {
219+ return function .apply (this , first , second );
220+ } finally {
221+ writeUnlock (stamp );
222+ }
223+ }
224+
180225 /**
181226 * Performs the given action for each key-value pair of this dictionary.
182227 *
0 commit comments