Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4639 from cleishm/future-factory
Add factory methods for Future instances
- Loading branch information
Showing
2 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
community/primitive-collections/src/main/java/org/neo4j/concurrent/Futures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) 2002-2015 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.concurrent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
/** | ||
* Constructors for basic {@link Future} types | ||
*/ | ||
public class Futures | ||
{ | ||
/** | ||
* Combine multiple @{link Future} instances into a single Future | ||
* | ||
* @param futures the @{link Future} instances to combine | ||
* @param <V> The result type returned by this Future's get method | ||
* @return A new @{link Future} representing the combination | ||
*/ | ||
@SafeVarargs | ||
public static <V> Future<List<V>> combine( final Future<? extends V>... futures ) | ||
{ | ||
return combine( Arrays.asList( futures ) ); | ||
} | ||
|
||
/** | ||
* Combine multiple @{link Future} instances into a single Future | ||
* | ||
* @param futures the @{link Future} instances to combine | ||
* @param <V> The result type returned by this Future's get method | ||
* @return A new @{link Future} representing the combination | ||
*/ | ||
public static <V> Future<List<V>> combine( final Iterable<? extends Future<? extends V>> futures ) | ||
{ | ||
return new Future<List<V>>() | ||
{ | ||
@Override | ||
public boolean cancel( boolean mayInterruptIfRunning ) | ||
{ | ||
boolean result = false; | ||
for ( Future<? extends V> future : futures ) | ||
{ | ||
result |= future.cancel( mayInterruptIfRunning ); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean isCancelled() | ||
{ | ||
boolean result = false; | ||
for ( Future<? extends V> future : futures ) | ||
{ | ||
result |= future.isCancelled(); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean isDone() | ||
{ | ||
boolean result = false; | ||
for ( Future<? extends V> future : futures ) | ||
{ | ||
result |= future.isDone(); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public List<V> get() throws InterruptedException, ExecutionException | ||
{ | ||
List<V> result = new ArrayList<>(); | ||
for ( Future<? extends V> future : futures ) | ||
{ | ||
result.add( future.get() ); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public List<V> get( long timeout, TimeUnit unit ) throws InterruptedException, ExecutionException, TimeoutException | ||
{ | ||
List<V> result = new ArrayList<>(); | ||
for ( Future<? extends V> future : futures ) | ||
{ | ||
long before = System.nanoTime(); | ||
result.add( future.get( timeout, unit ) ); | ||
timeout -= unit.convert( System.nanoTime() - before, TimeUnit.NANOSECONDS ); | ||
} | ||
return result; | ||
} | ||
}; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
community/primitive-collections/src/test/java/org/neo4j/concurrent/FuturesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2002-2015 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.concurrent; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.FutureTask; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static org.hamcrest.Matchers.contains; | ||
import static org.junit.Assert.*; | ||
|
||
public class FuturesTest | ||
{ | ||
private static final Runnable NOOP = new Runnable() | ||
{ | ||
@Override | ||
public void run() | ||
{ | ||
} | ||
}; | ||
|
||
@Test | ||
public void combinedFutureShouldGetResultsAfterAllComplete() throws Exception | ||
{ | ||
FutureTask<String> task1 = new FutureTask<>( NOOP, "1" ); | ||
FutureTask<String> task2 = new FutureTask<>( NOOP, "2" ); | ||
FutureTask<String> task3 = new FutureTask<>( NOOP, "3" ); | ||
|
||
Future<List<String>> combined = Futures.combine( task1, task2, task3 ); | ||
|
||
try | ||
{ | ||
combined.get( 10, TimeUnit.MILLISECONDS ); | ||
fail( "should have timedout" ); | ||
} catch ( TimeoutException e ) | ||
{ | ||
// continue | ||
} | ||
|
||
task3.run(); | ||
task2.run(); | ||
|
||
try | ||
{ | ||
combined.get( 10, TimeUnit.MILLISECONDS ); | ||
fail( "should have timedout" ); | ||
} catch ( TimeoutException e ) | ||
{ | ||
// continue | ||
} | ||
|
||
task1.run(); | ||
|
||
List<String> result = combined.get(); | ||
assertThat( result, contains( "1", "2", "3" ) ); | ||
} | ||
} |