Skip to content

Commit

Permalink
#146: RepeatedFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 15, 2017
1 parent a258e38 commit 3bf1975
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 24 deletions.
73 changes: 73 additions & 0 deletions src/main/java/org/cactoos/func/RepeatedFunc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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.func;

import org.cactoos.Func;

/**
* Func that repeats its calculation a few times before
* returning the last result.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of input
* @param <Y> Type of output
* @since 0.6
*/
public final class RepeatedFunc<X, Y> implements Func<X, Y> {

/**
* Original func.
*/
private final Func<X, Y> func;

/**
* How many times to run.
*/
private final int times;

/**
* Ctor.
*
* <p>If {@code max} is equal or less than zero {@link #apply(Object)}
* will return {@code null}.</p>
*
* @param fnc Func original
* @param max How many times
*/
public RepeatedFunc(final Func<X, Y> fnc, final int max) {
this.func = fnc;
this.times = max;
}

@Override
public Y apply(final X input) throws Exception {
Y result = null;
for (int idx = 0; idx < this.times; ++idx) {
result = this.func.apply(input);
}
return result;
}

}
4 changes: 4 additions & 0 deletions src/main/java/org/cactoos/func/StickyFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
* Func that caches previously calculated values and doesn't
* recalculate again.
*
* <p>This {@link Func} decorator technically is an in-memory
* cache.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of input
* @param <Y> Type of output
* @see StickyScalar
* @since 0.1
*/
public final class StickyFunc<X, Y> implements Func<X, Y> {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/cactoos/func/StickyScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@
/**
* Cached version of a Scalar.
*
* <p>This {@link Scalar} decorator technically is an in-memory
* cache.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Tim Hinkes (timmeey@timmeey.de)
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <T> Type of result
* @see StickyFunc
* @since 0.3
*/
public final class StickyScalar<T> implements Scalar<T> {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/cactoos/io/StickyInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class StickyInput implements Input {
/**
* The cache.
*/
private final Scalar<InputStream> cache;
private final Scalar<byte[]> cache;

/**
* Ctor.
Expand All @@ -62,14 +62,16 @@ public StickyInput(final Input input) {
new OutputStreamAsOutput(baos)
)
).asValue();
return new ByteArrayInputStream(baos.toByteArray());
return baos.toByteArray();
}
);
}

@Override
public InputStream stream() throws IOException {
return new IoCheckedScalar<>(this.cache).asValue();
return new ByteArrayInputStream(
new IoCheckedScalar<>(this.cache).asValue()
);
}

}
54 changes: 54 additions & 0 deletions src/test/java/org/cactoos/func/RepeatedFuncTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.func;

import java.security.SecureRandom;
import org.cactoos.Func;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link RepeatedFunc}.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.6
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class RepeatedFuncTest {

@Test
public void runsFuncMultipleTimes() throws Exception {
final Func<Boolean, Integer> func = new RepeatedFunc<>(
input -> new SecureRandom().nextInt(),
2
);
MatcherAssert.assertThat(
func.apply(true),
Matchers.not(Matchers.equalTo(func.apply(true)))
);
}

}
30 changes: 9 additions & 21 deletions src/test/java/org/cactoos/io/StickyInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
package org.cactoos.io;

import org.cactoos.func.FuncAsMatcher;
import org.cactoos.func.ProcAsFunc;
import org.cactoos.func.RepeatedFunc;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
Expand All @@ -43,28 +42,17 @@ public void readsFileContent() {
MatcherAssert.assertThat(
"Can't read bytes from a file",
new StickyInput(
new UrlAsInput(
this.getClass().getResource(
"/org/cactoos/io/UrlAsInput.class"
)
new ResourceAsInput(
"org/cactoos/large-text.txt"
)
),
new FuncAsMatcher<>(
new ProcAsFunc<>(
input -> {
MatcherAssert.assertThat(
new InputAsBytes(
new TeeInput(input, new DeadOutput())
).asBytes().length,
Matchers.greaterThan(0)
);
MatcherAssert.assertThat(
new InputAsBytes(
new TeeInput(input, new DeadOutput())
).asBytes().length,
Matchers.greaterThan(0)
);
}
new RepeatedFunc<>(
input -> new InputAsBytes(
new TeeInput(input, new DeadOutput())
// @checkstyle MagicNumber (1 line)
).asBytes().length == 73471,
2
)
)
);
Expand Down

0 comments on commit 3bf1975

Please sign in to comment.