mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-18 07:16:45 +00:00
Initial commit
This commit is contained in:
42
futur-reactive-streams/.gitignore
vendored
Normal file
42
futur-reactive-streams/.gitignore
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
.gradle
|
||||
build/
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
out/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
bin/
|
||||
!**/src/main/**/bin/
|
||||
!**/src/test/**/bin/
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
35
futur-reactive-streams/build.gradle.kts
Normal file
35
futur-reactive-streams/build.gradle.kts
Normal file
@@ -0,0 +1,35 @@
|
||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||
|
||||
plugins {
|
||||
id("java")
|
||||
id("com.github.johnrengelman.shadow") version "7.1.2"
|
||||
}
|
||||
|
||||
group = "dev.tommyjs"
|
||||
version = "1.0.0"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains:annotations:24.1.0")
|
||||
implementation(project(mapOf("path" to ":futur-api")))
|
||||
compileOnly("org.reactivestreams:reactive-streams:1.0.4")
|
||||
testImplementation(platform("org.junit:junit-bom:5.9.1"))
|
||||
testImplementation("org.junit.jupiter:junit-jupiter")
|
||||
}
|
||||
|
||||
tasks {
|
||||
build {
|
||||
dependsOn(shadowJar)
|
||||
}
|
||||
|
||||
withType<ShadowJar> {
|
||||
exclude("META-INF/**")
|
||||
}
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package dev.tommyjs.futur.reactivestreams;
|
||||
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
public class ReactiveTransformer {
|
||||
|
||||
public static <T> @NotNull Promise<T> wrapPublisher(@NotNull Publisher<T> publisher) {
|
||||
SingleAccumulatorSubscriber<T> subscriber = SingleAccumulatorSubscriber.create();
|
||||
publisher.subscribe(subscriber);
|
||||
return subscriber.getPromise();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package dev.tommyjs.futur.reactivestreams;
|
||||
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
|
||||
public class SingleAccumulatorSubscriber<T> implements Subscriber<T> {
|
||||
|
||||
private final Promise<T> promise;
|
||||
|
||||
public SingleAccumulatorSubscriber(Promise<T> promise) {
|
||||
this.promise = promise;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
s.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
promise.complete(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
promise.completeExceptionally(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
// ignore
|
||||
}
|
||||
|
||||
public Promise<T> getPromise() {
|
||||
return promise;
|
||||
}
|
||||
|
||||
public static <T> SingleAccumulatorSubscriber<T> create(Promise<T> promise) {
|
||||
return new SingleAccumulatorSubscriber<>(promise);
|
||||
}
|
||||
|
||||
public static <T> SingleAccumulatorSubscriber<T> create() {
|
||||
return create(new Promise<>());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user