Skip to content

Commit

Permalink
Merge remote-tracking branch 'yegor256/master' into Brevity#274-InputOf
Browse files Browse the repository at this point in the history
  • Loading branch information
ixmanuel committed Jul 21, 2017
2 parents 2a73c7c + 95a61af commit 1ecc280
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/main/java/org/cactoos/func/AsyncFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,9 +56,9 @@ public final class AsyncFunc<X, Y> implements Func<X, Future<Y>> {
private final Func<X, Y> func;

/**
* The threads.
* The executor service.
*/
private final ThreadFactory factory;
private final ExecutorService executor;

/**
* Ctor.
Expand Down Expand Up @@ -90,13 +91,22 @@ public AsyncFunc(final Proc<X> proc, final ThreadFactory fct) {
* @param fct Factory
*/
public AsyncFunc(final Func<X, Y> fnc, final ThreadFactory fct) {
this(fnc, Executors.newSingleThreadExecutor(fct));
}

/**
* Ctor.
* @param fnc The func
* @param exec Executor Service
*/
public AsyncFunc(final Func<X, Y> fnc, final ExecutorService exec) {
this.func = fnc;
this.factory = fct;
this.executor = exec;
}

@Override
public Future<Y> apply(final X input) {
return Executors.newSingleThreadExecutor(this.factory).submit(
return this.executor.submit(
() -> this.func.apply(input)
);
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/cactoos/func/SyncFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public final class SyncFunc<X, Y> implements Func<X, Y> {
*/
private final Func<X, Y> func;

/**
* Sync lock.
*/
private final Object lck;

/**
* Ctor.
* @param runnable Func original
Expand Down Expand Up @@ -75,14 +80,23 @@ public SyncFunc(final Proc<X> proc) {
* @param fnc Func original
*/
public SyncFunc(final Func<X, Y> fnc) {
this(fnc, fnc);
}

/**
* Ctor.
* @param fnc Func original
* @param lck Sync lock
*/
public SyncFunc(final Func<X, Y> 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);
}
}

}
17 changes: 16 additions & 1 deletion src/main/java/org/cactoos/func/SyncScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,32 @@ public final class SyncScalar<T> implements Scalar<T> {
*/
private final Scalar<T> source;

/**
* Sync lock.
*/
private final Object lck;

/**
* Ctor.
* @param src The Scalar to cache
*/
public SyncScalar(final Scalar<T> src) {
this(src, src);
}

/**
* Ctor.
* @param src The Scalar to cache
* @param lck Sync lock
*/
public SyncScalar(final Scalar<T> 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();
}
}
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/org/cactoos/io/ReaderAsInput.java
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>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)
);
}

}
58 changes: 58 additions & 0 deletions src/test/java/org/cactoos/io/ReaderAsInputTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}

}

0 comments on commit 1ecc280

Please sign in to comment.