com.github.dm.jrt.Main Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jroutine Show documentation
Show all versions of jroutine Show documentation
Parallel programming on the go
package com.github.dm.jrt;
import com.github.dm.jrt.builder.RoutineBuilder;
import com.github.dm.jrt.channel.IOChannel;
import com.github.dm.jrt.channel.InvocationChannel;
import com.github.dm.jrt.channel.OutputChannel;
import com.github.dm.jrt.channel.ResultChannel;
import com.github.dm.jrt.channel.RoutineException;
import com.github.dm.jrt.channel.TemplateOutputConsumer;
import com.github.dm.jrt.core.ByteChannel;
import com.github.dm.jrt.core.ByteChannel.BufferInputStream;
import com.github.dm.jrt.core.ByteChannel.BufferOutputStream;
import com.github.dm.jrt.core.ByteChannel.ByteBuffer;
import com.github.dm.jrt.core.Channels;
import com.github.dm.jrt.core.Channels.Selectable;
import com.github.dm.jrt.core.JRoutine;
import com.github.dm.jrt.function.BiFunction;
import com.github.dm.jrt.function.Consumer;
import com.github.dm.jrt.function.Function;
import com.github.dm.jrt.function.FunctionalRoutine;
import com.github.dm.jrt.function.Functions;
import com.github.dm.jrt.invocation.InvocationException;
import com.github.dm.jrt.invocation.TemplateInvocation;
import com.github.dm.jrt.runner.Runners;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URLConnection;
import java.util.Map;
import static com.github.dm.jrt.function.Functions.onOutput;
import static com.github.dm.jrt.invocation.Invocations.factoryOf;
import static com.github.dm.jrt.util.TimeDuration.seconds;
public class Main {
private static final int DATA_INDEX = 0;
private static final int SIGNAL_INDEX = 1;
public static void main(final String[] args) {
Functions.builder().buildRoutine().thenParallelMap(new Function() {
public Integer apply(final String s) {
return Integer.parseInt(s);
}
}).thenParallelMap(new Function() {
public Integer apply(final Integer integer) {
return integer * integer;
}
}).thenSyncMap(new Function() {
public SumData apply(final Integer integer) {
return new SumData(integer, 1);
}
}).thenAsyncAccumulate(new BiFunction() {
public SumData apply(final SumData data1, final SumData data2) {
return new SumData(data1.sum + data2.sum, data1.count + data2.count);
}
}).thenSyncMap(new Function() {
public Double apply(final SumData sumData) {
return Math.sqrt((double) sumData.sum / sumData.count);
}
}).asyncCall(args).passTo(onOutput(new Consumer() {
public void accept(final Double rms) {
System.out.println(rms);
}
})).afterMax(seconds(1)).checkComplete();
final FunctionalRoutine meanRoutine = //
Functions.builder()
.buildRoutine()
.thenParallelMap(new Function() {
public Integer apply(final String s) {
return Integer.parseInt(s);
}
})
.thenParallelMap(new Function() {
public Integer apply(final Integer integer) {
return integer * integer;
}
})
.thenSyncMap(new Function() {
public SumData apply(final Integer integer) {
return new SumData(integer, 1);
}
})
.thenAsyncAccumulate(new BiFunction() {
public SumData apply(final SumData data1, final SumData data2) {
return new SumData(data1.sum + data2.sum,
data1.count + data2.count);
}
})
.thenSyncMap(new Function() {
public Double apply(final SumData sumData) {
return Math.sqrt((double) sumData.sum / sumData.count);
}
});
final Double rms = meanRoutine.asyncCall(args).afterMax(seconds(1)).next();
System.out.println(rms);
final String url =
"https://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png";
final File outFile = new File(System.getProperty("java.io.tmpdir"), "test.png");
final InvocationChannel, Selectable