Skip to content

Commit 97c6432

Browse files
committed
update concurrent dictionary API
1 parent 09eaad6 commit 97c6432

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
@FunctionalInterface
7+
public interface NotNullNullableTripleFunction<F, S, T, R> extends TripleFunction<F, S, T, R> {
8+
9+
@Override
10+
@Nullable R apply(@NotNull F first, @NotNull S second, @NotNull T third);
11+
}

rlib-common/src/main/java/com/ss/rlib/common/util/dictionary/ConcurrentObjectDictionary.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package 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.*;
74
import org.jetbrains.annotations.NotNull;
85
import 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

Comments
 (0)