Skip to content

Commit

Permalink
#761 TimedFunc introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedran authored and Vedran committed Apr 8, 2018
1 parent f08a43d commit 9e611e7
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/main/java/org/cactoos/func/TimedFunc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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.util.concurrent.TimeUnit;
import org.cactoos.Func;
import org.cactoos.Proc;

/**
* Function that gets interrupted after a certain time has passed.
*
* @author Vedran Vatavuk (123vgv@gmail.com)
* @version $Id$
* @param <X> Type of input
* @param <Y> Type of output
* @since 0.29.3
*/
public final class TimedFunc<X, Y> implements Func<X, Y> {

/**
* Origin function.
*/
private final Func<X, Y> origin;

/**
* Milliseconds.
*/
private final long time;

/**
* Ctor.
* @param proc Proc
* @param milliseconds Milliseconds
*/
public TimedFunc(final Proc<X> proc, final long milliseconds) {
this(new FuncOf<>(proc), milliseconds);
}

/**
* Ctor.
* @param function Origin function
* @param milliseconds Milliseconds
*/
public TimedFunc(final Func<X, Y> function, final long milliseconds) {
this.origin = function;
this.time = milliseconds;
}

@Override
public Y apply(final X input) throws Exception {
return new AsyncFunc<>(this.origin).apply(input)
.get(this.time, TimeUnit.MILLISECONDS);
}
}
80 changes: 80 additions & 0 deletions src/test/java/org/cactoos/func/TimedFuncTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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.util.concurrent.TimeoutException;
import org.cactoos.iterable.Endless;
import org.cactoos.scalar.And;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link TimedFunc}.
*
* @author Vedran Vatavuk (123vgv@gmail.com)
* @version $Id$
* @since 0.29.3
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class TimedFuncTest {

@Test(expected = TimeoutException.class)
public void functionGetsInterrupted() throws Exception {
final long period = 100L;
new TimedFunc<Boolean, Boolean>(
input -> {
return new And(
new Endless<>(() -> input)
).value();
},
period
).apply(true);
}

@Test(expected = TimeoutException.class)
public void procGetsInterrupted() throws Exception {
final long period = 100L;
new TimedFunc<Boolean, Boolean>(
input -> {
new And(
new Endless<>(() -> input)
).value();
},
period
).apply(true);
}

@Test
public void functionGetsExecuted() throws Exception {
final long period = 3000L;
MatcherAssert.assertThat(
new TimedFunc<Boolean, Boolean>(
input -> true,
period
).apply(true),
Matchers.equalTo(true)
);
}
}

0 comments on commit 9e611e7

Please sign in to comment.