-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(simpleBufferTrigger): add global name registry.
- Loading branch information
1 parent
f1ac778
commit 0505af0
Showing
5 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/phantomthief/collection/impl/NameRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/java/com/github/phantomthief/collection/impl/NameRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |