Skip to content

Commit 636b425

Browse files
committed
Refactoring customizers for contextual promises & blocking IO; fixing NIO classes
1 parent 30cea1a commit 636b425

22 files changed

+505
-175
lines changed

.settings/org.eclipse.jdt.core.prefs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
33
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
44
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
55
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6-
org.eclipse.jdt.core.compiler.compliance=9
6+
org.eclipse.jdt.core.compiler.compliance=1.8
77
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
88
org.eclipse.jdt.core.compiler.debug.localVariable=generate
99
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
1010
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
1112
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
1213
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
14+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
15+
org.eclipse.jdt.core.compiler.release=disabled
1316
org.eclipse.jdt.core.compiler.source=1.8

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Maven Central](https://img.shields.io/maven-central/v/net.tascalate/net.tascalate.concurrent.svg)](https://search.maven.org/artifact/net.tascalate/net.tascalate.concurrent/0.9.5/jar) [![GitHub release](https://img.shields.io/github/release/vsilaev/tascalate-concurrent.svg)](https://github.com/vsilaev/tascalate-concurrent/releases/tag/0.9.5) [![license](https://img.shields.io/github/license/vsilaev/tascalate-concurrent.svg)](http://www.apache.org/licenses/LICENSE-2.0.txt)
1+
[![Maven Central](https://img.shields.io/maven-central/v/net.tascalate/net.tascalate.concurrent.svg)](https://search.maven.org/artifact/net.tascalate/net.tascalate.concurrent/0.9.6/jar) [![GitHub release](https://img.shields.io/github/release/vsilaev/tascalate-concurrent.svg)](https://github.com/vsilaev/tascalate-concurrent/releases/tag/0.9.6) [![license](https://img.shields.io/github/license/vsilaev/tascalate-concurrent.svg)](http://www.apache.org/licenses/LICENSE-2.0.txt)
22
# tascalate-concurrent
33
The library provides an implementation of the [CompletionStage](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionStage.html) interface and related classes these are designed to support long-running blocking tasks (typically, I/O bound). This functionality augments the sole Java 8 built-in implementation, [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html), that is primarily supports computational tasks. Also, the library helps with numerous asynchronous programing challenges like handling timeouts, retry/poll functionality, orchestrating results of multiple concurrent computations and similar.
44

@@ -12,7 +12,7 @@ New name:
1212
<dependency>
1313
<groupId>net.tascalate</groupId>
1414
<artifactId>net.tascalate.concurrent</artifactId>
15-
<version>0.9.5</version> <!-- Any version above 0.8.0, the latest one is recommended -->
15+
<version>0.9.6</version> <!-- Any version above 0.8.0, the latest one is recommended -->
1616
</dependency>
1717
```
1818
Old Name
@@ -38,7 +38,7 @@ To use a library you have to add a single Maven dependency
3838
<dependency>
3939
<groupId>net.tascalate</groupId>
4040
<artifactId>net.tascalate.concurrent</artifactId>
41-
<version>0.9.5</version>
41+
<version>0.9.6</version>
4242
</dependency>
4343
```
4444
# What is inside?

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.tascalate</groupId>
88
<artifactId>net.tascalate.concurrent</artifactId>
9-
<version>0.9.5</version>
9+
<version>0.9.6</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Tascalate Concurrent</name>

src/main/java/net/tascalate/concurrent/decorators/CompletableFutureDecorator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.concurrent.CompletionStage;
2222

2323
import net.tascalate.concurrent.Promise;
24-
import net.tascalate.concurrent.decorators.AbstractFutureDecorator;
2524

2625
public class CompletableFutureDecorator<T>
2726
extends AbstractFutureDecorator<T, CompletableFuture<T>>

src/main/java/net/tascalate/concurrent/io/AbstractAsyncFileChannel.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,25 @@ public void force(boolean metaData) throws IOException {
6262
}
6363

6464
public Promise<FileLock> lock(boolean shared) {
65-
return lock(0, Long.MAX_VALUE, shared);
65+
return doLock(0, Long.MAX_VALUE, shared);
6666
}
6767

6868
@Override
6969
public Promise<FileLock> lock(long position, long size, boolean shared) {
70-
Promise<FileLock> promise = (Promise<FileLock>)delegate.lock(position, size, shared);
71-
return promise.dependent()
72-
.thenApply(this::upgradeLock, true)
73-
.unwrap();
70+
return doLock(position, size, shared);
7471
}
7572

7673
public final <A> void lock(boolean shared, A attachment,
7774
CompletionHandler<FileLock, ? super A> handler) {
7875
lock(0, Long.MAX_VALUE, shared, attachment, handler);
7976
}
8077

78+
protected Promise<FileLock> doLock(long position, long size, boolean shared) {
79+
AsyncResult<FileLock> asyncResult = new AsyncResult<>();
80+
lock(position, size, shared, null, asyncResult.handler);
81+
return asyncResult;
82+
}
83+
8184
@Override
8285
public <A> void lock(long position, long size, boolean shared, A attachment,
8386
CompletionHandler<FileLock, ? super A> handler) {
@@ -114,7 +117,9 @@ public <A> void read(ByteBuffer dst, long position, A attachment, CompletionHand
114117

115118
@Override
116119
public Promise<Integer> read(ByteBuffer dst, long position) {
117-
return (Promise<Integer>)delegate.read(dst, position);
120+
AsyncResult<Integer> asyncResult = new AsyncResult<>();
121+
read(dst, position, null, asyncResult.handler);
122+
return asyncResult;
118123
}
119124

120125
@Override
@@ -124,7 +129,9 @@ public <A> void write(ByteBuffer src, long position, A attachment, CompletionHan
124129

125130
@Override
126131
public Promise<Integer> write(ByteBuffer src, long position) {
127-
return (Promise<Integer>)delegate.write(src, position);
132+
AsyncResult<Integer> asyncResult = new AsyncResult<>();
133+
write(src, position, null, asyncResult.handler);
134+
return asyncResult;
128135
}
129136

130137
protected FileLock upgradeLock(FileLock delegate) {

src/main/java/net/tascalate/concurrent/io/AbstractAsyncServerSocketChannel.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ public Promise<AsynchronousSocketChannel> accept() {
8888
*/
8989

9090
public Promise<C> accept() {
91-
Promise<AsynchronousSocketChannel> promise = (Promise<AsynchronousSocketChannel>)delegate.accept();
92-
return promise.dependent()
93-
.thenApply(this::wrap, true)
94-
.unwrap();
91+
AsyncResult<C> asyncResult = new AsyncResult<>();
92+
accept(null, asyncResult.handler);
93+
return asyncResult;
9594
}
9695

9796
/*

src/main/java/net/tascalate/concurrent/io/AbstractAsyncSocketChannel.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,16 @@ public <A> void connect(SocketAddress remote, A attachment, CompletionHandler<Vo
9393

9494
@Override
9595
public Promise<Void> connect(SocketAddress remote) {
96-
return (Promise<Void>)delegate.connect(remote);
96+
AsyncResult<Void> asyncResult = new AsyncResult<>();
97+
connect(remote, null, asyncResult.handler);
98+
return asyncResult;
9799
}
98100

99101
@Override
100102
public Promise<Integer> read(ByteBuffer dst) {
101-
return (Promise<Integer>)delegate.read(dst);
103+
AsyncResult<Integer> asyncResult = new AsyncResult<>();
104+
read(dst, null, asyncResult.handler);
105+
return asyncResult;
102106
}
103107

104108
@Override
@@ -115,7 +119,9 @@ public <A> void read(ByteBuffer[] dsts, int offset, int length, long timeout, Ti
115119

116120
@Override
117121
public Promise<Integer> write(ByteBuffer src) {
118-
return (Promise<Integer>)delegate.write(src);
122+
AsyncResult<Integer> asyncResult = new AsyncResult<>();
123+
write(src, null, asyncResult.handler);
124+
return asyncResult;
119125
}
120126

121127
@Override

src/main/java/net/tascalate/concurrent/io/AsyncChannelGroup.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/main/java/net/tascalate/concurrent/io/AsyncFileChannel.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424
import java.util.HashSet;
2525
import java.util.Objects;
2626
import java.util.Set;
27-
28-
import net.tascalate.concurrent.TaskExecutorService;
27+
import java.util.concurrent.ExecutorService;
2928

3029
public class AsyncFileChannel extends AbstractAsyncFileChannel<AsyncFileChannel> {
3130

3231
protected AsyncFileChannel(AsynchronousFileChannel delegate) {
3332
super(delegate);
3433
}
3534

36-
public static AsyncFileChannel open(Path file, TaskExecutorService executor, OpenOption... options) throws IOException {
35+
public static AsyncFileChannel open(Path file, ExecutorService executor, OpenOption... options) throws IOException {
3736
Set<OpenOption> set;
3837
if (options.length == 0) {
3938
set = Collections.emptySet();
@@ -45,7 +44,7 @@ public static AsyncFileChannel open(Path file, TaskExecutorService executor, Ope
4544
}
4645

4746
public static AsyncFileChannel open(Path file,
48-
TaskExecutorService executor,
47+
ExecutorService executor,
4948
Set<? extends OpenOption> options,
5049
FileAttribute<?>... attrs) throws IOException {
5150
Objects.requireNonNull(executor, "Executor should be specified");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2015-2020 Valery Silaev (http://vsilaev.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.tascalate.concurrent.io;
17+
18+
import java.nio.channels.CompletionHandler;
19+
20+
import net.tascalate.concurrent.CompletablePromise;
21+
22+
class AsyncResult<V> extends CompletablePromise<V> {
23+
24+
final CompletionHandler<V, Object> handler = new CompletionHandler<V, Object>() {
25+
@Override
26+
public void completed(final V result, final Object attachment) {
27+
success(result);
28+
}
29+
30+
@Override
31+
public void failed(Throwable exc, Object attachment) {
32+
failure(exc);
33+
}
34+
};
35+
36+
AsyncResult() {
37+
}
38+
39+
}

0 commit comments

Comments
 (0)