Merge pull request #19476 from drewhk/wip-19441-java-optional-in-javadsl-drewhk

Wip 19441 java optional in javadsl drewhk
This commit is contained in:
Konrad Malawski 2016-01-18 13:54:27 +01:00
commit 1caf4b2502
59 changed files with 350 additions and 316 deletions

View file

@ -4,13 +4,14 @@
package docs.http.javadsl;
import akka.japi.Option;
import akka.util.ByteString;
import org.junit.Test;
//#import-model
import akka.http.javadsl.model.*;
import akka.http.javadsl.model.headers.*;
import java.util.Optional;
//#import-model
@SuppressWarnings("unused")
@ -79,12 +80,12 @@ public class ModelDocTest {
//#headers
// a method that extracts basic HTTP credentials from a request
private Option<BasicHttpCredentials> getCredentialsOfRequest(HttpRequest request) {
Option<Authorization> auth = request.getHeader(Authorization.class);
if (auth.isDefined() && auth.get().credentials() instanceof BasicHttpCredentials)
return Option.some((BasicHttpCredentials) auth.get().credentials());
private Optional<BasicHttpCredentials> getCredentialsOfRequest(HttpRequest request) {
Optional<Authorization> auth = request.getHeader(Authorization.class);
if (auth.isPresent() && auth.get().credentials() instanceof BasicHttpCredentials)
return Optional.of((BasicHttpCredentials) auth.get().credentials());
else
return Option.none();
return Optional.empty();
}
//#headers
}

View file

@ -202,7 +202,7 @@ public class HttpServerExampleDocTest {
.withEntity(ContentTypes.TEXT_HTML_UTF8,
"<html><body>Hello world!</body></html>");
else if (uri.path().equals("/hello")) {
String name = Util.getOrElse(uri.query().get("name"), "Mister X");
String name = uri.query().get("name").orElse("Mister X");
return
HttpResponse.create()

View file

@ -4,6 +4,7 @@
package docs.stream;
import java.util.Arrays;
import java.util.Optional;
import akka.stream.ClosedShape;
import org.junit.AfterClass;
@ -213,21 +214,21 @@ public class CompositionDocTest {
//#mat-combine-4a
static class MyClass {
private Promise<Option<Integer>> p;
private Promise<Optional<Integer>> p;
private OutgoingConnection conn;
public MyClass(Promise<Option<Integer>> p, OutgoingConnection conn) {
public MyClass(Promise<Optional<Integer>> p, OutgoingConnection conn) {
this.p = p;
this.conn = conn;
}
public void close() {
p.success(Option.empty());
p.success(Optional.empty());
}
}
static class Combiner {
static Future<MyClass> f(Promise<Option<Integer>> p,
static Future<MyClass> f(Promise<Optional<Integer>> p,
Pair<Future<OutgoingConnection>, Future<String>> rest) {
return rest.first().map(new Mapper<OutgoingConnection, MyClass>() {
public MyClass apply(OutgoingConnection c) {
@ -242,13 +243,13 @@ public class CompositionDocTest {
public void materializedValues() throws Exception {
//#mat-combine-1
// Materializes to Promise<BoxedUnit> (red)
final Source<Integer, Promise<Option<Integer>>> source = Source.<Integer>maybe();
final Source<Integer, Promise<Optional<Integer>>> source = Source.<Integer>maybe();
// Materializes to BoxedUnit (black)
final Flow<Integer, Integer, BoxedUnit> flow1 = Flow.of(Integer.class).take(100);
// Materializes to Promise<Option<>> (red)
final Source<Integer, Promise<Option<Integer>>> nestedSource =
final Source<Integer, Promise<Optional<Integer>>> nestedSource =
source.viaMat(flow1, Keep.left()).named("nestedSource");
//#mat-combine-1

View file

@ -5,10 +5,7 @@ package docs.stream;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@ -225,7 +222,7 @@ public class FlowDocTest {
//#flow-mat-combine
// An empty source that can be shut down explicitly from the outside
Source<Integer, Promise<Option<Integer>>> source = Source.<Integer>maybe();
Source<Integer, Promise<Optional<Integer>>> source = Source.<Integer>maybe();
// A flow that internally throttles elements to 1/second, and returns a Cancellable
// which can be used to shut down the stream
@ -236,7 +233,7 @@ public class FlowDocTest {
// By default, the materialized value of the leftmost stage is preserved
RunnableGraph<Promise<Option<Integer>>> r1 = source.via(flow).to(sink);
RunnableGraph<Promise<Optional<Integer>>> r1 = source.via(flow).to(sink);
// Simple selection of materialized values by using Keep.right
RunnableGraph<Cancellable> r2 = source.viaMat(flow, Keep.right()).to(sink);
@ -245,17 +242,17 @@ public class FlowDocTest {
// Using runWith will always give the materialized values of the stages added
// by runWith() itself
Future<Integer> r4 = source.via(flow).runWith(sink, mat);
Promise<Option<Integer>> r5 = flow.to(sink).runWith(source, mat);
Pair<Promise<Option<Integer>>, Future<Integer>> r6 = flow.runWith(source, sink, mat);
Promise<Optional<Integer>> r5 = flow.to(sink).runWith(source, mat);
Pair<Promise<Optional<Integer>>, Future<Integer>> r6 = flow.runWith(source, sink, mat);
// Using more complext combinations
RunnableGraph<Pair<Promise<Option<Integer>>, Cancellable>> r7 =
RunnableGraph<Pair<Promise<Optional<Integer>>, Cancellable>> r7 =
source.viaMat(flow, Keep.both()).to(sink);
RunnableGraph<Pair<Promise<Option<Integer>>, Future<Integer>>> r8 =
RunnableGraph<Pair<Promise<Optional<Integer>>, Future<Integer>>> r8 =
source.via(flow).toMat(sink, Keep.both());
RunnableGraph<Pair<Pair<Promise<Option<Integer>>, Cancellable>, Future<Integer>>> r9 =
RunnableGraph<Pair<Pair<Promise<Optional<Integer>>, Cancellable>, Future<Integer>>> r9 =
source.viaMat(flow, Keep.both()).toMat(sink, Keep.both());
RunnableGraph<Pair<Cancellable, Future<Integer>>> r10 =
@ -267,7 +264,7 @@ public class FlowDocTest {
RunnableGraph<Cancellable> r11 =
r9.mapMaterializedValue( (nestedTuple) -> {
Promise<Option<Integer>> p = nestedTuple.first().first();
Promise<Optional<Integer>> p = nestedTuple.first().first();
Cancellable c = nestedTuple.first().second();
Future<Integer> f = nestedTuple.second();

View file

@ -28,6 +28,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.nio.charset.Charset;
@ -148,7 +149,7 @@ public class MigrationsJava {
}
//#source-creators
Source<Integer, Promise<Option<Integer>>> src = Source.<Integer>maybe();
Source<Integer, Promise<Optional<Integer>>> src = Source.<Integer>maybe();
// Complete the promise with an empty option to emulate the old lazyEmpty
promise.trySuccess(scala.Option.empty());
@ -208,11 +209,11 @@ public class MigrationsJava {
Uri uri = null;
//#raw-query
final akka.japi.Option<String> theRawQueryString = uri.rawQueryString();
final Optional<String> theRawQueryString = uri.rawQueryString();
//#raw-query
//#query-param
final akka.japi.Option<String> aQueryParam = uri.query().get("a");
final Optional<String> aQueryParam = uri.query().get("a");
//#query-param
//#file-source-sink

View file

@ -14,10 +14,10 @@ The ``RequestVal`` builder is made up of 2 steps, initially you need to pick whi
match if the header is not present in the request). This is done using one of the below depicted methods::
RequestVal<T> instance()
RequestVal<<Option<T>> optionalInstance()
RequestVal<<Optional<T>> optionalInstance()
RequestVal<String> value()
RequestVal<Option<String>> optionalValue()
RequestVal<Optional<String>> optionalValue()
Examples
--------

View file

@ -127,7 +127,7 @@ Illegal stream elements
In accordance to the Reactive Streams specification (`Rule 2.13 <https://github.com/reactive-streams/reactive-streams-jvm#2.13>`_)
Akka Streams do not allow ``null`` to be passed through the stream as an element. In case you want to model the concept
of absence of a value we recommend using ``akka.japi.Option`` (for Java 6 and 7) or ``java.util.Optional`` which is available since Java 8.
of absence of a value we recommend using ``java.util.Optional`` which is available since Java 8.
.. _back-pressure-explained-java: