public DynamicTaskExecutor( int initialProcessorCount, int maxProcessorCount, int maxQueueSize, ParkStrategy parkStrategy, String processorThreadNamePrefix ) { this( initialProcessorCount, maxProcessorCount, maxQueueSize, parkStrategy, processorThreadNamePrefix, Suppliers.singleton( null ) ); }
/** * Creates a {@link FormattedLog} instance that writes messages to {@link OutputStream}s obtained from the specified * {@link Supplier}. The OutputStream is obtained from the Supplier before every log message is written. * * @param outSupplier A supplier for an output stream to write to * @return A {@link FormattedLog} instance */ public FormattedLog toOutputStream( Supplier<OutputStream> outSupplier ) { return toPrintWriter( Suppliers.adapted( outSupplier, OUTPUT_STREAM_CONVERTER ) ); }
public static TransactionalContextFactory create( GraphDatabaseFacade.SPI spi, ThreadToStatementContextBridge txBridge, PropertyContainerLocker locker ) { Supplier<GraphDatabaseQueryService> queryService = lazySingleton( spi::queryService ); Supplier<Kernel> kernel = lazySingleton( spi::kernel ); Neo4jTransactionalContext.Creator contextCreator = ( tx, initialStatement, executingQuery ) -> new Neo4jTransactionalContext( queryService.get(), txBridge, locker, tx, initialStatement, executingQuery, kernel.get() ); return new Neo4jTransactionalContextFactory( txBridge, contextCreator ); }
public static <TYPE, EXCEPTION extends Exception> TYPE awaitEx( ThrowingSupplier<TYPE,EXCEPTION> supplier, ThrowingPredicate<TYPE,EXCEPTION> predicate, long timeout, TimeUnit timeoutUnit, long pollInterval, TimeUnit pollUnit ) throws TimeoutException, EXCEPTION { Suppliers.ThrowingCapturingSupplier<TYPE,EXCEPTION> composed = Suppliers.compose( supplier, predicate ); awaitEx( composed, timeout, timeoutUnit, pollInterval, pollUnit ); return composed.lastInput(); }
@Test public void concurrentTransactionAppendingTest() throws Exception { int threads = 10; Callable<Long> runner = new Builder() .with( untilTimeExpired( 10, SECONDS ) ) .withWorkingDirectory( directory.databaseLayout() ) .withNumThreads( threads ) .build(); long appendedTxs = runner.call(); assertEquals( new TransactionIdChecker( directory.databaseLayout().databaseDirectory() ).parseAllTxLogs(), appendedTxs ); }
public static <TYPE, EXCEPTION extends Exception> TYPE awaitEx( ThrowingSupplier<TYPE,? extends EXCEPTION> supplier, ThrowingPredicate<TYPE, ? extends EXCEPTION> predicate, long timeout, TimeUnit timeoutUnit ) throws TimeoutException, EXCEPTION { Suppliers.ThrowingCapturingSupplier<TYPE,EXCEPTION> composed = Suppliers.compose( supplier, predicate ); awaitEx( composed, timeout, timeoutUnit ); return composed.lastInput(); }
@Override public void bulk( @Nonnull Consumer<Log> consumer ) { PrintWriter writer; synchronized ( this ) { writer = writerSupplier.get(); consumer.accept( new ConsistencyReportLog( Suppliers.singleton( writer ), lock, false ) ); } } }
private static Suppliers.Lazy<PrintWriter> getReportWriterSupplier( FileSystemAbstraction fileSystem, File reportFile ) { return Suppliers.lazySingleton( () -> { try { return new PrintWriter( createOrOpenAsOutputStream( fileSystem, reportFile, true ) ); } catch ( IOException e ) { throw new RuntimeException( e ); } } ); }
/** * Creates a {@link FormattedLogProvider} instance that writes messages to {@link OutputStream}s obtained from the specified * {@link Supplier}. The OutputStream is obtained from the Supplier before every log message is written. * * @param outSupplier A supplier for an output stream to write to * @return A {@link FormattedLogProvider} instance */ public FormattedLogProvider toOutputStream( Supplier<OutputStream> outSupplier ) { return toPrintWriter( Suppliers.adapted( outSupplier, OUTPUT_STREAM_CONVERTER ) ); }
public static <TYPE> void await( Supplier<TYPE> supplier, Predicate<TYPE> predicate, long timeout, TimeUnit unit ) throws TimeoutException, InterruptedException { await( Suppliers.compose( supplier, predicate ), timeout, unit ); }
/** * Creates a {@link FormattedLogProvider} instance that writes messages to a {@link PrintWriter}. * * @param writer A {@link PrintWriter} to write to * @return A {@link FormattedLogProvider} instance that writes to the specified PrintWriter */ public FormattedLogProvider toPrintWriter( PrintWriter writer ) { return toPrintWriter( Suppliers.singleton( writer ) ); }
@Test void lazySingletonSupplierShouldOnlyRequestInstanceWhenRequired() { Object o = new Object(); Supplier<Object> mockSupplier = mock( Supplier.class ); when( mockSupplier.get() ).thenReturn( o ); Supplier<Object> supplier = Suppliers.lazySingleton( mockSupplier ); verifyZeroInteractions( mockSupplier ); assertThat( supplier.get(), sameInstance( o ) ); assertThat( supplier.get(), sameInstance( o ) ); assertThat( supplier.get(), sameInstance( o ) ); verify( mockSupplier ).get(); verifyNoMoreInteractions( mockSupplier ); }
private void writeLines( Supplier<OutputStream> outputStreamSupplier, int count ) { Supplier<PrintWriter> printWriterSupplier = Suppliers.adapted( outputStreamSupplier, OUTPUT_STREAM_CONVERTER ); for ( ; count >= 0; --count ) { printWriterSupplier.get().println( "We are what we repeatedly do. Excellence, then, is not an act, but a habit." ); Thread.yield(); } }
public static <TYPE, EXCEPTION extends Exception> TYPE awaitEx( ThrowingSupplier<TYPE,? extends EXCEPTION> supplier, ThrowingPredicate<TYPE, ? extends EXCEPTION> predicate, long timeout, TimeUnit timeoutUnit ) throws TimeoutException, EXCEPTION { Suppliers.ThrowingCapturingSupplier<TYPE,EXCEPTION> composed = Suppliers.compose( supplier, predicate ); awaitEx( composed, timeout, timeoutUnit ); return composed.lastInput(); }
/** * Creates a {@link FormattedLogProvider} instance that writes messages to an {@link OutputStream}. * * @param out An {@link OutputStream} to write to * @return A {@link FormattedLogProvider} instance that writes to the specified OutputStream */ public FormattedLogProvider toOutputStream( OutputStream out ) { return toPrintWriter( Suppliers.singleton( OUTPUT_STREAM_CONVERTER.apply( out ) ) ); }
@Test void correctlyReportNotInitialisedSuppliers() { Suppliers.Lazy<Object> lazySingleton = Suppliers.lazySingleton( Object::new ); assertFalse( lazySingleton.isInitialised() ); }
@Test void adaptedSupplierShouldOnlyCallAdaptorOnceForEachNewInstance() { Object o1 = new Object(); Object o1a = new Object(); Object o2 = new Object(); Object o2a = new Object(); Object o3 = new Object(); Object o3a = new Object(); Supplier<Object> mockSupplier = mock( Supplier.class ); when( mockSupplier.get() ).thenReturn( o1, o1, o1, o2, o3, o3 ); Function<Object,Object> mockFunction = mock( Function.class ); when( mockFunction.apply( o1 ) ).thenReturn( o1a ); when( mockFunction.apply( o2 ) ).thenReturn( o2a ); when( mockFunction.apply( o3 ) ).thenReturn( o3a ); Supplier<Object> supplier = Suppliers.adapted( mockSupplier, mockFunction ); assertThat( supplier.get(), sameInstance( o1a ) ); assertThat( supplier.get(), sameInstance( o1a ) ); assertThat( supplier.get(), sameInstance( o1a ) ); assertThat( supplier.get(), sameInstance( o2a ) ); assertThat( supplier.get(), sameInstance( o3a ) ); assertThat( supplier.get(), sameInstance( o3a ) ); verify( mockFunction ).apply( o1 ); verify( mockFunction ).apply( o2 ); verify( mockFunction ).apply( o3 ); verifyNoMoreInteractions( mockFunction ); }
public static <TYPE, EXCEPTION extends Exception> TYPE awaitEx( ThrowingSupplier<TYPE,EXCEPTION> supplier, ThrowingPredicate<TYPE,EXCEPTION> predicate, long timeout, TimeUnit timeoutUnit, long pollInterval, TimeUnit pollUnit ) throws TimeoutException, EXCEPTION { Suppliers.ThrowingCapturingSupplier<TYPE,EXCEPTION> composed = Suppliers.compose( supplier, predicate ); awaitEx( composed, timeout, timeoutUnit, pollInterval, pollUnit ); return composed.lastInput(); }
/** * Creates a {@link FormattedLog} instance that writes messages to an {@link OutputStream}. * * @param out An {@link OutputStream} to write to * @return A {@link FormattedLog} instance that writes to the specified OutputStream */ public FormattedLog toOutputStream( OutputStream out ) { return toPrintWriter( Suppliers.singleton( OUTPUT_STREAM_CONVERTER.apply( out ) ) ); }
@Test void correctlyReportInitialisedSuppliers() { Suppliers.Lazy<Object> lazySingleton = Suppliers.lazySingleton( Object::new ); lazySingleton.get(); assertTrue( lazySingleton.isInitialised() ); } }