/** * 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 ) ); }
/** * 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 ) ); }
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(); } }
@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 ); }
/** * 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 ) ); }
/** * 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 ) ); }
when( mockFunction.apply( o3 ) ).thenReturn(o3a); Supplier<Object> supplier = Suppliers.adapted( mockSupplier, mockFunction );