+doc 18737: Code snippets for migration guide (Java)

This commit is contained in:
Endre Sándor Varga 2015-11-03 13:58:03 +01:00
parent 100fc83a3d
commit 82e1d3dcec
3 changed files with 188 additions and 28 deletions

View file

@ -0,0 +1,125 @@
package docs;
import akka.japi.Pair;
import akka.stream.*;
import akka.stream.javadsl.*;
import scala.Option;
import scala.concurrent.Promise;
import scala.runtime.BoxedUnit;
public class MigrationsJava {
// This is compile-only code, no need for actually running anything.
public static ActorMaterializer mat = null;
public static void main(String[] args) {
Outlet<Integer> outlet = null;
Outlet<Integer> outlet1 = null;
Outlet<Integer> outlet2 = null;
Inlet<Integer> inlet = null;
Inlet<Integer> inlet1 = null;
Inlet<Integer> inlet2 = null;
Flow<Integer, Integer, BoxedUnit> flow = Flow.of(Integer.class);
Flow<Integer, Integer, BoxedUnit> flow1 = Flow.of(Integer.class);
Flow<Integer, Integer, BoxedUnit> flow2 = Flow.of(Integer.class);
Promise<Option<Integer>> promise = null;
{
Graph<SourceShape<Integer>, BoxedUnit> graphSource = null;
Graph<SinkShape<Integer>, BoxedUnit> graphSink = null;
Graph<FlowShape<Integer, Integer>, BoxedUnit> graphFlow = null;
//#flow-wrap
Source<Integer, BoxedUnit> source = Source.fromGraph(graphSource);
Sink<Integer, BoxedUnit> sink = Sink.fromGraph(graphSink);
Flow<Integer, Integer, BoxedUnit> aflow = Flow.fromGraph(graphFlow);
Flow.fromSinkAndSource(Sink.<Integer>head(), Source.single(0));
Flow.fromSinkAndSourceMat(Sink.<Integer>head(), Source.single(0), Keep.left());
//#flow-wrap
Graph<BidiShape<Integer, Integer, Integer, Integer>, BoxedUnit> bidiGraph = null;
//#bidi-wrap
BidiFlow<Integer, Integer, Integer, Integer, BoxedUnit> bidiFlow =
BidiFlow.fromGraph(bidiGraph);
BidiFlow.fromFlows(flow1, flow2);
BidiFlow.fromFlowsMat(flow1, flow2, Keep.both());
//#bidi-wrap
}
{
//#graph-create
FlowGraph.create(builder -> {
//...
return ClosedShape.getInstance();
});
FlowGraph.create(builder -> {
//...
return new FlowShape(inlet, outlet);
});
//#graph-create
}
{
//#graph-create-2
FlowGraph.create(builder -> {
//...
return new SourceShape<>(outlet);
});
FlowGraph.create(builder -> {
//...
return new SinkShape<>(inlet);
});
FlowGraph.create(builder -> {
//...
return new FlowShape<>(inlet, outlet);
});
FlowGraph.create(builder -> {
//...
return new BidiShape<>(inlet1, outlet1, inlet2, outlet2);
});
//#graph-create-2
}
{
//#graph-builder
FlowGraph.create(builder -> {
builder.from(outlet).toInlet(inlet);
builder.from(outlet).via(builder.add(flow)).toInlet(inlet);
builder.from(builder.add(Source.single(0))).to(builder.add(Sink.head()));
//...
return ClosedShape.getInstance();
});
//#graph-builder
}
//#source-creators
Source<Integer, Promise<Option<Integer>>> src = Source.<Integer>maybe();
// Complete the promise with an empty option to emulate the old lazyEmpty
promise.trySuccess(scala.Option.empty());
//#source-creators
//#empty-flow
Flow<Integer, Integer, BoxedUnit> emptyFlow = Flow.<Integer>create();
// or
Flow<Integer, Integer, BoxedUnit> emptyFlow2 = Flow.of(Integer.class);
//#empty-flow
//#flattenConcat
Flow.<Source<Integer, BoxedUnit>>create().flattenConcat();
//#flattenConcat
}
}

View file

@ -1,4 +1,4 @@
.. _migration-2.0:
.. _migration-2.0-java:
############################
Migration Guide 1.0 to 2.x
@ -51,15 +51,15 @@ Example
::
Graph<SourceShape<Integer>, BoxedUnit> graphSource = null;
Graph<SourceShape<Integer>, BoxedUnit> graphSource = ...;
// This no longer works!
Source<Integer, BoxedUnit> source = Source.wrap(graphSource);
Graph<SinkShape<Integer>, BoxedUnit> graphSink = null;
Graph<SinkShape<Integer>, BoxedUnit> graphSink = ...;
// This no longer works!
Sink<Integer, BoxedUnit> sink = Sink.wrap(graphSink);
Graph<FlowShape<Integer, Integer>, BoxedUnit> graphFlow = null;
Graph<FlowShape<Integer, Integer>, BoxedUnit> graphFlow = ...;
// This no longer works!
Flow<Integer, Integer, BoxedUnit> flow = Flow.wrap(graphFlow);
@ -68,13 +68,13 @@ Example
should be replaced by
TODO
.. includecode:: code/docs/MigrationsJava.java#flow-wrap
and
::
Graph<BidiShape<Integer, Integer, Integer, Integer>, BoxedUnit> bidiGraph = null;
Graph<BidiShape<Integer, Integer, Integer, Integer>, BoxedUnit> bidiGraph = ...;
// This no longer works!
BidiFlow<Integer, Integer, Integer, Integer, BoxedUnit> bidiFlow = BidiFlow.wrap(bidiGraph);
@ -84,7 +84,7 @@ and
Should be replaced by
TODO
.. includecode:: code/docs/MigrationsJava.java#bidi-wrap
FlowGraph builder methods have been renamed
===========================================
@ -117,7 +117,7 @@ Example
should be replaced by
TODO
.. includecode:: code/docs/MigrationsJava.java#graph-create
Methods that create Source, Sink, Flow from Graphs have been removed
====================================================================
@ -174,24 +174,37 @@ Example
should be replaced by
TODO
.. includecode:: code/docs/MigrationsJava.java#graph-create-2
Some graph Builder methods have been removed
============================================
Due to the high number of overloads Java 8 type inference suffered, and it was also hard to figure out which time
to use which method. Therefore various redundant methods have been removed.
to use which method. Therefore various redundant methods have been removed. As a consequence, every ``Sink``, ``Source``
and ``Flow`` needs to be explicitly added via ``builder.add()``.
Update procedure
----------------
1. All uses of ``builder.addEdge(Outlet, Inlet)`` should be replaced by the alternative ``builder.from(…).to(…)``
2. All uses of ``builder.addEdge(Outlet, FlowShape, Inlet)`` should be replaced by ``builder.from(…).via(…).to(…)``
3. All uses of ``builder.source`` should be replaced by ``builder.from(…).via(…).to(…)``
4. All uses of ``builder.flow`` should be replaced by ``builder.from(…).via(…).to(…)``
5. All uses of ``builder.sink`` should be replaced by ``builder.from(…).via(…).to(…)``
1. All uses of ``builder.edge(outlet,inlet)`` should be replaced by the alternative ``builder.from(outlet).toInlet(inlet)``
3. All uses of ``builder.source`` should be replaced by ``builder.from(builder.add(source))``
4. All uses of ``builder.flow`` should be replaced by ``builder.….via(builder.add(flow))``
5. All uses of ``builder.sink`` should be replaced by ``builder.….to(builder.add(sink)))``
TODO: code example
::
FlowGraph.factory().closed(builder -> {
// These no longer work
builder.edge(outlet, inlet);
builder.flow(outlet, flow, inlet);
builder.source(Source.single(0));
builder.sink(Sink.<Integer>head());
//...
});
should be replaced by
.. includecode:: code/docs/MigrationsJava.java#graph-builder
Source constructor name changes
===============================
@ -215,7 +228,16 @@ Update procedure
Example
^^^^^^^
TODO
::
// This no longer works!
Source<Integer, Promise<BoxedUnit>> src = Source.lazyEmpty();
//...
promise.trySuccess(BoxedUnit.UNIT);
should be replaced by
.. includecode:: code/docs/MigrationsJava.java#source-creators
``Flow.empty()`` have been removed
==================================
@ -228,7 +250,14 @@ Update procedure
1. Replace all uses of ``Flow.empty()`` with ``Flow.create``.
TODO: code example
::
// This no longer works!
Flow<Integer, Integer, BoxedUnit> emptyFlow = Flow.<Integer>empty();
should be replaced by
.. includecode:: code/docs/MigrationsJava.java#empty-flow
``flatten(FlattenStrategy)`` has been replaced by named counterparts
====================================================================
@ -244,7 +273,13 @@ Update procedure
Example
^^^^^^^
TODO
::
Flow.<Source<Integer, BoxedUnit>>create().flatten(FlattenStrategy.concat());
should be replaced by
.. includecode:: code/docs/MigrationsJava.java#flattenConcat
FlexiMerge an FlexiRoute has been replaced by GraphStage
========================================================

View file

@ -1,4 +1,4 @@
.. _migration-2.0:
.. _migration-2.0-scala:
############################
Migration Guide 1.0 to 2.x
@ -71,7 +71,7 @@ Example
should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#flow-wrap
.. includecode:: code/docs/Migrations.scala#flow-wrap
and
@ -90,7 +90,7 @@ and
Should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#bidiflow-wrap
.. includecode:: code/docs/Migrations.scala#bidiflow-wrap
FlowGraph builder methods have been renamed
===========================================
@ -123,7 +123,7 @@ Example
should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#graph-create
.. includecode:: code/docs/Migrations.scala#graph-create
Methods that create Source, Sink, Flow from Graphs have been removed
====================================================================
@ -180,7 +180,7 @@ Example
should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#graph-create-2
.. includecode:: code/docs/Migrations.scala#graph-create-2
Several Graph builder methods have been removed
===============================================
@ -213,7 +213,7 @@ Example
should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#graph-edges
.. includecode:: code/docs/Migrations.scala#graph-edges
Source constructor name changes
===============================
@ -249,7 +249,7 @@ Example
should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#source-creators
.. includecode:: code/docs/Migrations.scala#source-creators
``flatten(FlattenStrategy)`` has been replaced by named counterparts
====================================================================
@ -272,7 +272,7 @@ Example
should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#flatten
.. includecode:: code/docs/Migrations.scala#flatten
FlexiMerge an FlexiRoute has been replaced by GraphStage
========================================================
@ -408,4 +408,4 @@ Example
should be replaced by
.. includecode:: /scala/code/docs/Migrations.scala#port-async
.. includecode:: code/docs/Migrations.scala#port-async