Skip to content

Commit c144e6d

Browse files
committed
update dictionary API
1 parent 2b436a6 commit c144e6d

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
4343
return DictionaryFactory.newConcurrentStampedLockObjectDictionary();
4444
}
4545

46+
/**
47+
* Execute a function for this dictionary under block {@link ConcurrentObjectDictionary#readLock()}.
48+
*
49+
* @param argument the argument.
50+
* @param consumer the function.
51+
* @param <A> the argument's type.
52+
* @return this dictionary.
53+
*/
54+
default <A> @NotNull ConcurrentObjectDictionary<K, V> runInReadLock(
55+
@NotNull A argument,
56+
@NotNull NotNullBiConsumer<ConcurrentObjectDictionary<K, V>, A> consumer
57+
) {
58+
59+
var stamp = readLock();
60+
try {
61+
consumer.accept(this, argument);
62+
} finally {
63+
readUnlock(stamp);
64+
}
65+
66+
return this;
67+
}
68+
4669
/**
4770
* Execute a function for this dictionary under block {@link ConcurrentObjectDictionary#writeLock()}.
4871
*
@@ -118,11 +141,12 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
118141
* @param argument the argument.
119142
* @param function the function.
120143
* @param <A> the argument's type.
144+
* @param <R> the result's type.
121145
* @return the result of the function.
122146
*/
123-
default <A> @Nullable V getInReadLock(
147+
default <A, R> @Nullable R getInReadLock(
124148
@NotNull A argument,
125-
@NotNull NotNullNullableBiFunction<ConcurrentObjectDictionary<K, V>, A, V> function
149+
@NotNull NotNullNullableBiFunction<ConcurrentObjectDictionary<K, V>, A, R> function
126150
) {
127151
var stamp = readLock();
128152
try {
@@ -138,11 +162,12 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
138162
* @param argument the argument.
139163
* @param function the function.
140164
* @param <A> the argument's type.
165+
* @param <R> the result's type.
141166
* @return the result of the function.
142167
*/
143-
default <A> @Nullable V getInWriteLock(
168+
default <A, R> @Nullable R getInWriteLock(
144169
@NotNull A argument,
145-
@NotNull NotNullNullableBiFunction<ConcurrentObjectDictionary<K, V>, A, V> function
170+
@NotNull NotNullNullableBiFunction<ConcurrentObjectDictionary<K, V>, A, R> function
146171
) {
147172
var stamp = writeLock();
148173
try {

rlib-common/src/test/java/com/ss/rlib/common/test/util/dictionary/ConcurrentObjectDictionaryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,19 @@ void runInWriteLockTest() {
4444
assertType(arg2, Integer.class);
4545
});
4646
}
47+
48+
@Test
49+
void runInReadLockTest() {
50+
51+
var dictionary = ConcurrentObjectDictionary.ofType(
52+
String.class,
53+
Integer.class
54+
);
55+
56+
dictionary.runInReadLock("4", (dic, arg) -> {
57+
assertType(dic, ConcurrentObjectDictionary.class);
58+
assertType(arg, String.class);
59+
dic.get(arg);
60+
});
61+
}
4762
}

0 commit comments

Comments
 (0)