Skip to content

Commit 0505af0

Browse files
committed
feat(simpleBufferTrigger): add global name registry.
1 parent f1ac778 commit 0505af0

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<properties>
1212
<slf4j-api.version>1.7.21</slf4j-api.version>
1313
<guava.version>28.1-jre</guava.version>
14-
<more-lambdas.version>0.1.36</more-lambdas.version>
14+
<more-lambdas.version>0.1.53</more-lambdas.version>
1515

1616
<junit.version>5.7.0</junit.version>
1717
<logback-classic.version>1.1.8</logback-classic.version>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.phantomthief.collection.impl;
2+
3+
import static com.github.phantomthief.util.MoreReflection.getCallerPlace;
4+
5+
import javax.annotation.Nullable;
6+
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
import com.github.phantomthief.collection.BufferTrigger;
10+
11+
/**
12+
* @author w.vela
13+
* Created on 2021-02-04.
14+
*/
15+
public interface NameRegistry {
16+
17+
/**
18+
* 注意,当前还只支持 {@link BufferTrigger#simple()} 方式构建的命名获取
19+
*/
20+
static NameRegistry autoRegistry() {
21+
return () -> {
22+
StackTraceElement callerPlace = getCallerPlace(SimpleBufferTriggerBuilder.class);
23+
if (callerPlace != null && !StringUtils.equals("ReflectionUtils.java", callerPlace.getFileName())) {
24+
return callerPlace.getFileName() + ":" + callerPlace.getLineNumber();
25+
} else {
26+
return null;
27+
}
28+
};
29+
}
30+
31+
@Nullable
32+
String name();
33+
}

src/main/java/com/github/phantomthief/collection/impl/SimpleBufferTrigger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,8 @@ public void run() {
334334
public static void setupGlobalBackPressure(GlobalBackPressureListener listener) {
335335
BackPressureHandler.setupGlobalBackPressureListener(listener);
336336
}
337+
338+
public static void setupGlobalNameRegistry(NameRegistry nameRegistry) {
339+
SimpleBufferTriggerBuilder.setupGlobalNameRegistry(nameRegistry);
340+
}
337341
}

src/main/java/com/github/phantomthief/collection/impl/SimpleBufferTriggerBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
public class SimpleBufferTriggerBuilder<E, C> {
4141

4242
private static final Logger logger = LoggerFactory.getLogger(SimpleBufferTriggerBuilder.class);
43+
private static NameRegistry globalNameRegistry;
4344

4445
private boolean maxBufferCountWasSet = false;
4546

@@ -317,6 +318,9 @@ public SimpleBufferTriggerBuilder<E, C> name(String name) {
317318
*/
318319
public <E1> BufferTrigger<E1> build() {
319320
check();
321+
if (globalNameRegistry != null && name == null) {
322+
name = globalNameRegistry.name();
323+
}
320324
return new LazyBufferTrigger<>(() -> {
321325
ensure();
322326
SimpleBufferTriggerBuilder<E1, C> builder =
@@ -365,4 +369,8 @@ private ScheduledExecutorService makeScheduleExecutor() {
365369
.setDaemon(true)
366370
.build());
367371
}
372+
373+
static void setupGlobalNameRegistry(NameRegistry registry) {
374+
globalNameRegistry = registry;
375+
}
368376
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.phantomthief.collection.impl;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import java.util.concurrent.atomic.AtomicLong;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import com.github.phantomthief.collection.BufferTrigger;
11+
12+
/**
13+
* @author w.vela
14+
* Created on 2021-02-04.
15+
*/
16+
class NameRegistryTest {
17+
18+
private static final String[] NAME = {null};
19+
20+
static {
21+
SimpleBufferTrigger.setupGlobalNameRegistry(() -> {
22+
String name1 = NameRegistry.autoRegistry().name();
23+
NAME[0] = name1;
24+
return name1;
25+
});
26+
}
27+
28+
private BufferTrigger<String> buffer1 = BufferTrigger.<String, AtomicLong> simple()
29+
.setContainer(AtomicLong::new, (counter, _2) -> {
30+
counter.incrementAndGet();
31+
return true;
32+
})
33+
.consumer(it -> {})
34+
.build();
35+
36+
@Test
37+
void test() {
38+
assertEquals("NameRegistryTest.java:34", NAME[0]); // 是 buffer1 声明的行数
39+
40+
BufferTrigger<String> buffer2 = BufferTrigger.simpleTrigger()
41+
.setContainer(AtomicLong::new, (counter, _2) -> {
42+
counter.incrementAndGet();
43+
return true;
44+
})
45+
.consumer(it -> {})
46+
.build();
47+
assertNull(NAME[0]);
48+
}
49+
}

0 commit comments

Comments
 (0)