Skip to content

Commit 2b436a6

Browse files
committed
update dictionary API
1 parent 4e5d7d0 commit 2b436a6

9 files changed

Lines changed: 126 additions & 198 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.function.Supplier;
6+
7+
@FunctionalInterface
8+
public interface NotNullSupplier<T> extends Supplier<T> {
9+
10+
@Override
11+
@NotNull T get();
12+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ss.rlib.common.util.dictionary;
22

3+
import com.ss.rlib.common.function.NotNullFunction;
34
import com.ss.rlib.common.util.ArrayUtils;
45
import com.ss.rlib.common.util.array.Array;
56
import com.ss.rlib.common.util.pools.PoolFactory;
@@ -86,7 +87,7 @@ protected AbstractDictionary(float loadFactor, int initCapacity) {
8687
}
8788

8889
@Override
89-
public final void apply(@NotNull Function<? super V, V> function) {
90+
public final void apply(@NotNull NotNullFunction<? super V, V> function) {
9091
for (var entry : entries()) {
9192
while (entry != null) {
9293
entry.setValue(function.apply(entry.getValue()));

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package com.ss.rlib.common.util.dictionary;
22

3-
import com.ss.rlib.common.function.FourObjectConsumer;
4-
import com.ss.rlib.common.function.TripleConsumer;
3+
import com.ss.rlib.common.function.*;
54
import com.ss.rlib.common.util.ClassUtils;
65
import com.ss.rlib.common.util.array.Array;
7-
import com.ss.rlib.common.util.array.UnsafeArray;
86
import org.jetbrains.annotations.NotNull;
97
import org.jetbrains.annotations.Nullable;
108

119
import java.util.Iterator;
12-
import java.util.function.BiConsumer;
13-
import java.util.function.BiFunction;
14-
import java.util.function.Function;
15-
import java.util.function.Supplier;
1610

1711
/**
1812
* The base implementation of the {@link ObjectDictionary}.
@@ -80,7 +74,7 @@ public final boolean containsKey(@NotNull K key) {
8074
}
8175

8276
@Override
83-
public @NotNull V getOrCompute(@NotNull K key, @NotNull Supplier<@NotNull V> factory) {
77+
public @NotNull V getOrCompute(@NotNull K key, @NotNull NotNullSupplier<V> factory) {
8478

8579
var entry = getEntry(key);
8680

@@ -98,7 +92,7 @@ public final boolean containsKey(@NotNull K key) {
9892
}
9993

10094
@Override
101-
public @NotNull V getOrCompute(@NotNull K key, @NotNull Function<@NotNull K, @NotNull V> factory) {
95+
public @NotNull V getOrCompute(@NotNull K key, @NotNull NotNullFunction<K, V> factory) {
10296

10397
var entry = getEntry(key);
10498

@@ -116,11 +110,7 @@ public final boolean containsKey(@NotNull K key) {
116110
}
117111

118112
@Override
119-
public <T> @NotNull V getOrCompute(
120-
@NotNull K key,
121-
@NotNull T argument,
122-
@NotNull Function<@NotNull T, @NotNull V> factory
123-
) {
113+
public <T> @NotNull V getOrCompute(@NotNull K key, @NotNull T argument, @NotNull NotNullFunction<T, V> factory) {
124114

125115
var entry = getEntry(key);
126116

@@ -141,7 +131,7 @@ public final boolean containsKey(@NotNull K key) {
141131
public <T> @NotNull V getOrCompute(
142132
@NotNull K key,
143133
@NotNull T argument,
144-
@NotNull BiFunction<@NotNull K, @NotNull T, @NotNull V> factory
134+
@NotNull NotNullBiFunction<K, T, V> factory
145135
) {
146136

147137
var entry = getEntry(key);
@@ -321,7 +311,7 @@ public final String toString() {
321311
}
322312

323313
@Override
324-
public void forEach(@NotNull BiConsumer<? super K, ? super V> consumer) {
314+
public void forEach(@NotNull NotNullBiConsumer<? super K, ? super V> consumer) {
325315
for (var entry : entries()) {
326316
while (entry != null) {
327317
consumer.accept(entry.getKey(), entry.getValue());
@@ -333,7 +323,7 @@ public void forEach(@NotNull BiConsumer<? super K, ? super V> consumer) {
333323
@Override
334324
public <T> void forEach(
335325
@NotNull T argument,
336-
@NotNull TripleConsumer<@NotNull ? super T, @NotNull ? super K, @NotNull ? super V> consumer
326+
@NotNull NotNullTripleConsumer<? super T, ? super K, ? super V> consumer
337327
) {
338328
for (var entry : entries()) {
339329
while (entry != null) {

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

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.ss.rlib.common.util.dictionary;
22

3-
import com.ss.rlib.common.function.TripleConsumer;
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;
47
import org.jetbrains.annotations.NotNull;
58
import org.jetbrains.annotations.Nullable;
69

7-
import java.util.function.BiConsumer;
8-
import java.util.function.BiFunction;
9-
import java.util.function.Consumer;
10-
1110
/**
1211
* The interface with methods for supporting thread-safe for the {@link ObjectDictionary}.
1312
*
@@ -25,7 +24,7 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
2524
* @return the new concurrent object dictionary.
2625
*/
2726
static <T> @NotNull ObjectDictionary<T, T> ofType(@NotNull Class<? super T> keyValueType) {
28-
return DictionaryFactory.newConcurrentAtomicObjectDictionary();
27+
return DictionaryFactory.newConcurrentStampedLockObjectDictionary();
2928
}
3029

3130
/**
@@ -41,17 +40,17 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
4140
@NotNull Class<? super K> keyType,
4241
@NotNull Class<? super V> valueType
4342
) {
44-
return DictionaryFactory.newConcurrentAtomicObjectDictionary();
43+
return DictionaryFactory.newConcurrentStampedLockObjectDictionary();
4544
}
4645

4746
/**
48-
* Execute the function for this dictionary in the block {@link ConcurrentObjectDictionary#writeLock()}.
47+
* Execute a function for this dictionary under block {@link ConcurrentObjectDictionary#writeLock()}.
4948
*
5049
* @param consumer the function.
5150
* @return this dictionary.
5251
*/
5352
default @NotNull ConcurrentObjectDictionary<K, V> runInWriteLock(
54-
@NotNull Consumer<@NotNull ConcurrentObjectDictionary<K, V>> consumer
53+
@NotNull NotNullConsumer<ConcurrentObjectDictionary<K, V>> consumer
5554
) {
5655

5756
var stamp = writeLock();
@@ -65,20 +64,21 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
6564
}
6665

6766
/**
68-
* Execute the function for this dictionary in the block {@link ConcurrentObjectDictionary#writeLock()}.
67+
* Execute a function for this dictionary under block {@link ConcurrentObjectDictionary#writeLock()}.
6968
*
70-
* @param key the key value.
69+
* @param argument the argument.
7170
* @param consumer the function.
71+
* @param <A> the argument's type.
7272
* @return this dictionary.
7373
*/
74-
default @NotNull ConcurrentObjectDictionary<K, V> runInWriteLock(
75-
@NotNull K key,
76-
@NotNull BiConsumer<@NotNull ConcurrentObjectDictionary<K, V>, @NotNull K> consumer
74+
default <A> @NotNull ConcurrentObjectDictionary<K, V> runInWriteLock(
75+
@NotNull A argument,
76+
@NotNull NotNullBiConsumer<ConcurrentObjectDictionary<K, V>, A> consumer
7777
) {
7878

7979
var stamp = writeLock();
8080
try {
81-
consumer.accept(this, key);
81+
consumer.accept(this, argument);
8282
} finally {
8383
writeUnlock(stamp);
8484
}
@@ -87,23 +87,24 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
8787
}
8888

8989
/**
90-
* Execute the function for this dictionary in the block {@link ConcurrentObjectDictionary#writeLock()}.
90+
* Execute a function for this dictionary under block {@link ConcurrentObjectDictionary#writeLock()}.
9191
*
92-
* @param key the key value.
93-
* @param argument the additional argument,
92+
* @param first the first argument.
93+
* @param second the second argument.
9494
* @param consumer the function.
95-
* @param <F> the argument's type.
95+
* @param <F> the first argument's type.
96+
* @param <S> the second argument's type.
9697
* @return this dictionary.
9798
*/
98-
default <F> @NotNull ConcurrentObjectDictionary<K, V> runInWriteLock(
99-
@NotNull K key,
100-
@NotNull F argument,
101-
@NotNull TripleConsumer<@NotNull ConcurrentObjectDictionary<K, V>, @NotNull K, @NotNull F> consumer
99+
default <F, S> @NotNull ConcurrentObjectDictionary<K, V> runInWriteLock(
100+
@NotNull F first,
101+
@NotNull S second,
102+
@NotNull NotNullTripleConsumer<ConcurrentObjectDictionary<K, V>, F, S> consumer
102103
) {
103104

104105
var stamp = writeLock();
105106
try {
106-
consumer.accept(this, key, argument);
107+
consumer.accept(this, first, second);
107108
} finally {
108109
writeUnlock(stamp);
109110
}
@@ -112,40 +113,40 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
112113
}
113114

114115
/**
115-
* Get the value using a function from a dictionary in the block {@link
116-
* ConcurrentObjectDictionary#readLock()}.
116+
* Get the value from a function for this dictionary under block {@link ConcurrentObjectDictionary#readLock()}.
117117
*
118-
* @param key the key value.
118+
* @param argument the argument.
119119
* @param function the function.
120+
* @param <A> the argument's type.
120121
* @return the result of the function.
121122
*/
122-
default @Nullable V getInReadLock(
123-
@NotNull K key,
124-
@NotNull BiFunction<ConcurrentObjectDictionary<K, V>, @NotNull K, @NotNull V> function
123+
default <A> @Nullable V getInReadLock(
124+
@NotNull A argument,
125+
@NotNull NotNullNullableBiFunction<ConcurrentObjectDictionary<K, V>, A, V> function
125126
) {
126127
var stamp = readLock();
127128
try {
128-
return function.apply(this, key);
129+
return function.apply(this, argument);
129130
} finally {
130131
readUnlock(stamp);
131132
}
132133
}
133134

134135
/**
135-
* Get the value using a function from a dictionary in the block {@link
136-
* ConcurrentObjectDictionary#writeLock()}.
136+
* Get the value from a function for this dictionary under block {@link ConcurrentObjectDictionary#writeLock()}.
137137
*
138-
* @param key the key value.
138+
* @param argument the argument.
139139
* @param function the function.
140+
* @param <A> the argument's type.
140141
* @return the result of the function.
141142
*/
142-
default @Nullable V getInWriteLock(
143-
@NotNull K key,
144-
@NotNull BiFunction<ConcurrentObjectDictionary<K, V>, @NotNull K, @NotNull V> function
143+
default <A> @Nullable V getInWriteLock(
144+
@NotNull A argument,
145+
@NotNull NotNullNullableBiFunction<ConcurrentObjectDictionary<K, V>, A, V> function
145146
) {
146147
var stamp = writeLock();
147148
try {
148-
return function.apply(this, key);
149+
return function.apply(this, argument);
149150
} finally {
150151
writeUnlock(stamp);
151152
}
@@ -156,7 +157,7 @@ public interface ConcurrentObjectDictionary<K, V> extends ObjectDictionary<K, V>
156157
*
157158
* @param consumer the consumer.
158159
*/
159-
default void forEachInReadLock(@NotNull BiConsumer<@NotNull ? super K, @NotNull ? super V> consumer) {
160+
default void forEachInReadLock(@NotNull NotNullBiConsumer<? super K, ? super V> consumer) {
160161
var stamp = readLock();
161162
try {
162163
forEach(consumer);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ss.rlib.common.util.dictionary;
22

3+
import com.ss.rlib.common.function.NotNullFunction;
34
import com.ss.rlib.common.util.array.Array;
45
import com.ss.rlib.common.util.array.ArrayFactory;
56
import com.ss.rlib.common.util.pools.Reusable;
@@ -21,7 +22,7 @@ public interface Dictionary<K, V> extends Iterable<V>, Reusable {
2122
*
2223
* @param function the function.
2324
*/
24-
default void apply(@NotNull Function<@NotNull ? super V, @NotNull V> function) {
25+
default void apply(@NotNull NotNullFunction<? super V, V> function) {
2526
throw new UnsupportedOperationException();
2627
}
2728

0 commit comments

Comments
 (0)