Skip to content

Commit

Permalink
feat(simpleBufferTrigger): add global name registry.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomThief committed Feb 4, 2021
1 parent f1ac778 commit 0505af0
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<properties>
<slf4j-api.version>1.7.21</slf4j-api.version>
<guava.version>28.1-jre</guava.version>
<more-lambdas.version>0.1.36</more-lambdas.version>
<more-lambdas.version>0.1.53</more-lambdas.version>

<junit.version>5.7.0</junit.version>
<logback-classic.version>1.1.8</logback-classic.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.phantomthief.collection.impl;

import static com.github.phantomthief.util.MoreReflection.getCallerPlace;

import javax.annotation.Nullable;

import org.apache.commons.lang3.StringUtils;

import com.github.phantomthief.collection.BufferTrigger;

/**
* @author w.vela
* Created on 2021-02-04.
*/
public interface NameRegistry {

/**
* 注意,当前还只支持 {@link BufferTrigger#simple()} 方式构建的命名获取
*/
static NameRegistry autoRegistry() {
return () -> {
StackTraceElement callerPlace = getCallerPlace(SimpleBufferTriggerBuilder.class);
if (callerPlace != null && !StringUtils.equals("ReflectionUtils.java", callerPlace.getFileName())) {
return callerPlace.getFileName() + ":" + callerPlace.getLineNumber();
} else {
return null;
}
};
}

@Nullable
String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,8 @@ public void run() {
public static void setupGlobalBackPressure(GlobalBackPressureListener listener) {
BackPressureHandler.setupGlobalBackPressureListener(listener);
}

public static void setupGlobalNameRegistry(NameRegistry nameRegistry) {
SimpleBufferTriggerBuilder.setupGlobalNameRegistry(nameRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
public class SimpleBufferTriggerBuilder<E, C> {

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

private boolean maxBufferCountWasSet = false;

Expand Down Expand Up @@ -317,6 +318,9 @@ public SimpleBufferTriggerBuilder<E, C> name(String name) {
*/
public <E1> BufferTrigger<E1> build() {
check();
if (globalNameRegistry != null && name == null) {
name = globalNameRegistry.name();
}
return new LazyBufferTrigger<>(() -> {
ensure();
SimpleBufferTriggerBuilder<E1, C> builder =
Expand Down Expand Up @@ -365,4 +369,8 @@ private ScheduledExecutorService makeScheduleExecutor() {
.setDaemon(true)
.build());
}

static void setupGlobalNameRegistry(NameRegistry registry) {
globalNameRegistry = registry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.phantomthief.collection.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.concurrent.atomic.AtomicLong;

import org.junit.jupiter.api.Test;

import com.github.phantomthief.collection.BufferTrigger;

/**
* @author w.vela
* Created on 2021-02-04.
*/
class NameRegistryTest {

private static final String[] NAME = {null};

static {
SimpleBufferTrigger.setupGlobalNameRegistry(() -> {
String name1 = NameRegistry.autoRegistry().name();
NAME[0] = name1;
return name1;
});
}

private BufferTrigger<String> buffer1 = BufferTrigger.<String, AtomicLong> simple()
.setContainer(AtomicLong::new, (counter, _2) -> {
counter.incrementAndGet();
return true;
})
.consumer(it -> {})
.build();

@Test
void test() {
assertEquals("NameRegistryTest.java:34", NAME[0]); // 是 buffer1 声明的行数

BufferTrigger<String> buffer2 = BufferTrigger.simpleTrigger()
.setContainer(AtomicLong::new, (counter, _2) -> {
counter.incrementAndGet();
return true;
})
.consumer(it -> {})
.build();
assertNull(NAME[0]);
}
}

0 comments on commit 0505af0

Please sign in to comment.