From 3cf92616f3bbb2c017c92255577de71ea5693de9 Mon Sep 17 00:00:00 2001 From: g4s8 Date: Tue, 18 Jul 2017 20:33:16 +0400 Subject: [PATCH 1/4] Synchronization locks in constructor arguments --- src/main/java/org/cactoos/func/SyncFunc.java | 18 ++++++++++++++++-- src/main/java/org/cactoos/func/SyncProc.java | 18 ++++++++++++++++-- src/main/java/org/cactoos/func/SyncScalar.java | 17 ++++++++++++++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/func/SyncFunc.java b/src/main/java/org/cactoos/func/SyncFunc.java index ccc8d80f41..267e6d5854 100644 --- a/src/main/java/org/cactoos/func/SyncFunc.java +++ b/src/main/java/org/cactoos/func/SyncFunc.java @@ -41,19 +41,33 @@ public final class SyncFunc implements Func { */ private final Func func; + /** + * Sync lock. + */ + private final Object lck; + /** * Ctor. * @param fnc Func original */ public SyncFunc(final Func fnc) { + this(fnc, fnc); + } + + /** + * Ctor. + * @param fnc Func original + * @param lck Sync lock + */ + public SyncFunc(final Func fnc, final Object lck) { this.func = fnc; + this.lck = lck; } @Override public Y apply(final X input) throws Exception { - synchronized (this.func) { + synchronized (this.lck) { return this.func.apply(input); } } - } diff --git a/src/main/java/org/cactoos/func/SyncProc.java b/src/main/java/org/cactoos/func/SyncProc.java index ed9de046ef..c9874cd278 100644 --- a/src/main/java/org/cactoos/func/SyncProc.java +++ b/src/main/java/org/cactoos/func/SyncProc.java @@ -40,19 +40,33 @@ public final class SyncProc implements Proc { */ private final Proc proc; + /** + * Sync lock. + */ + private final Object lck; + /** * Ctor. * @param prc Func original */ public SyncProc(final Proc prc) { + this(prc, prc); + } + + /** + * Ctor. + * @param prc Func original + * @param lck Sync lock + */ + public SyncProc(final Proc prc, final Object lck) { this.proc = prc; + this.lck = lck; } @Override public void exec(final X input) throws Exception { - synchronized (this.proc) { + synchronized (this.lck) { this.proc.exec(input); } } - } diff --git a/src/main/java/org/cactoos/func/SyncScalar.java b/src/main/java/org/cactoos/func/SyncScalar.java index fe44b4e127..7789cff73b 100644 --- a/src/main/java/org/cactoos/func/SyncScalar.java +++ b/src/main/java/org/cactoos/func/SyncScalar.java @@ -40,17 +40,32 @@ public final class SyncScalar implements Scalar { */ private final Scalar source; + /** + * Sync lock. + */ + private final Object lck; + /** * Ctor. * @param src The Scalar to cache */ public SyncScalar(final Scalar src) { + this(src, src); + } + + /** + * Ctor. + * @param src The Scalar to cache + * @param lck Sync lock + */ + public SyncScalar(final Scalar src, final Object lck) { this.source = src; + this.lck = lck; } @Override public T value() throws Exception { - synchronized (this.source) { + synchronized (this.lck) { return this.source.value(); } } From 222767151f1550be272ca6484b4414defe5a1f6f Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 19 Jul 2017 19:44:40 -0300 Subject: [PATCH 2/4] executor service as a field --- src/main/java/org/cactoos/func/AsyncFunc.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/func/AsyncFunc.java b/src/main/java/org/cactoos/func/AsyncFunc.java index 210931a202..abc461b5fb 100644 --- a/src/main/java/org/cactoos/func/AsyncFunc.java +++ b/src/main/java/org/cactoos/func/AsyncFunc.java @@ -23,6 +23,7 @@ */ package org.cactoos.func; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; @@ -55,9 +56,9 @@ public final class AsyncFunc implements Func> { private final Func func; /** - * The threads. + * The executor service. */ - private final ThreadFactory factory; + private final ExecutorService executor; /** * Ctor. @@ -91,12 +92,12 @@ public AsyncFunc(final Proc proc, final ThreadFactory fct) { */ public AsyncFunc(final Func fnc, final ThreadFactory fct) { this.func = fnc; - this.factory = fct; + this.executor = Executors.newSingleThreadExecutor(fct); } @Override public Future apply(final X input) { - return Executors.newSingleThreadExecutor(this.factory).submit( + return this.executor.submit( () -> this.func.apply(input) ); } From b13153779fa68840b5b3db45c27d103cb7fc2b12 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 20 Jul 2017 17:03:37 +0300 Subject: [PATCH 3/4] #333: ReaderAsInput --- .../java/org/cactoos/io/ReaderAsInput.java | 106 ++++++++++++++++++ .../org/cactoos/io/ReaderAsInputTest.java | 58 ++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/main/java/org/cactoos/io/ReaderAsInput.java create mode 100644 src/test/java/org/cactoos/io/ReaderAsInputTest.java diff --git a/src/main/java/org/cactoos/io/ReaderAsInput.java b/src/main/java/org/cactoos/io/ReaderAsInput.java new file mode 100644 index 0000000000..94bb513da2 --- /dev/null +++ b/src/main/java/org/cactoos/io/ReaderAsInput.java @@ -0,0 +1,106 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import org.cactoos.Input; + +/** + * Reader as {@link Input}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.12 + */ +public final class ReaderAsInput implements Input { + + /** + * The reader. + */ + private final Reader reader; + + /** + * The charset. + */ + private final Charset charset; + + /** + * The buffer size. + */ + private final int size; + + /** + * Ctor. + * @param rdr Reader + */ + public ReaderAsInput(final Reader rdr) { + this(rdr, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * @param rdr Reader + * @param cset Charset + */ + public ReaderAsInput(final Reader rdr, final Charset cset) { + // @checkstyle MagicNumber (1 line) + this(rdr, cset, 16 << 10); + } + + /** + * Ctor. + * @param rdr Reader + * @param cset Charset + * @param max Buffer size + */ + public ReaderAsInput(final Reader rdr, final Charset cset, final int max) { + this.reader = rdr; + this.charset = cset; + this.size = max; + } + + @Override + public InputStream stream() throws IOException { + final char[] buffer = new char[this.size]; + final StringBuilder builder = new StringBuilder(); + while (true) { + final int done = this.reader.read(buffer, 0, buffer.length); + if (done < 0) { + break; + } + builder.append(buffer, 0, done); + } + return new ByteArrayInputStream( + builder.toString().getBytes(this.charset) + ); + } + +} diff --git a/src/test/java/org/cactoos/io/ReaderAsInputTest.java b/src/test/java/org/cactoos/io/ReaderAsInputTest.java new file mode 100644 index 0000000000..bf60979c65 --- /dev/null +++ b/src/test/java/org/cactoos/io/ReaderAsInputTest.java @@ -0,0 +1,58 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.StringReader; +import org.cactoos.text.BytesAsText; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link ReaderAsInput}. + * + * @author Kirill (g4s8.public@gmail.com) + * @version $Id$ + * @since 0.12 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ReaderAsInputTest { + + @Test + public void readsString() throws Exception { + final String source = "hello, друг!"; + MatcherAssert.assertThat( + "Can't read string through a reader", + new BytesAsText( + new InputAsBytes( + new ReaderAsInput( + new StringReader(source) + ) + ) + ).asString(), + Matchers.equalTo(source) + ); + } + +} From b7786f366c66ebc6661fae63e801a1032ed25981 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 20 Jul 2017 18:00:11 -0300 Subject: [PATCH 4/4] cr --- src/main/java/org/cactoos/func/AsyncFunc.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/func/AsyncFunc.java b/src/main/java/org/cactoos/func/AsyncFunc.java index abc461b5fb..551f7260bb 100644 --- a/src/main/java/org/cactoos/func/AsyncFunc.java +++ b/src/main/java/org/cactoos/func/AsyncFunc.java @@ -91,8 +91,17 @@ public AsyncFunc(final Proc proc, final ThreadFactory fct) { * @param fct Factory */ public AsyncFunc(final Func fnc, final ThreadFactory fct) { + this(fnc, Executors.newSingleThreadExecutor(fct)); + } + + /** + * Ctor. + * @param fnc The func + * @param exec Executor Service + */ + public AsyncFunc(final Func fnc, final ExecutorService exec) { this.func = fnc; - this.executor = Executors.newSingleThreadExecutor(fct); + this.executor = exec; } @Override