Não consegui seguir a linha de raciocínio para implementar o filtro poderia me mostrar como ficaria.
package br.com.alura.processor;
import br.com.alura.model.NotaFiscal;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;
public class NotaFiscalFiltro extends SubmissionPublisher implements Flow.Processor {
private Flow.Subscription subscription;
@Override
public void onSubscribe(Flow.Subscription subscription) {
System.out.println("Chamando o onSubscribe");
this.subscription = subscription;
}
@Override
public void onNext(Object item) {
NotaFiscal nf = (NotaFiscal)item;
if(nf.getValor() > 0.0) {
submit(nf);
} else {
System.out.println("Nota fiscal com o valor menor ou igual a zero");
}
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
@Override
public void onComplete() {
this.subscription.request(1);
}
}
Fiz isso porem ele sempre para na primeira execução de uma nota fiscal
package br.com.alura;
import java.time.LocalDate;
import java.util.Scanner;
import java.util.concurrent.SubmissionPublisher;
import br.com.alura.model.NotaFiscal;
import br.com.alura.processor.NotaFiscalFiltro;
import br.com.alura.subscriber.NotaFiscalSubscriber;
public class TesteEnvioNaoBloqueante {
public static void main(String[] args) {
NotaFiscal primeiraNotaFiscal = new NotaFiscal("João", 39.99, LocalDate.now());
NotaFiscal segundaNotaFiscal = new NotaFiscal("Renata", 39.99, LocalDate.now());
NotaFiscal terceiraNotaFiscal = new NotaFiscal("Paulo", 39.99, LocalDate.now());
SubmissionPublisher<NotaFiscal> publisher = new SubmissionPublisher<>();
NotaFiscalFiltro filtro = new NotaFiscalFiltro();
NotaFiscalSubscriber subscriber = new NotaFiscalSubscriber();
publisher.subscribe(filtro);
filtro.subscribe(subscriber);
publisher.submit(primeiraNotaFiscal);
publisher.submit(segundaNotaFiscal);
publisher.submit(terceiraNotaFiscal);
System.out.println("Você irá receber a nota fiscal no seu e-mail");
Scanner scan = new Scanner(System.in);
scan.nextLine();
scan.close();
publisher.close();
}
}