-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement issue #28 : add support to use java 8 Suppliers as Randomizers
- Loading branch information
1 parent
63b0398
commit b248b1e
Showing
4 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
random-beans/src/main/java/io/github/benas/randombeans/RandomizerProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.benas.randombeans; | ||
|
||
import io.github.benas.randombeans.api.Randomizer; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Dynamic proxy that adapts a {@link java.util.function.Supplier} to a {@link Randomizer}. | ||
* | ||
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) | ||
*/ | ||
class RandomizerProxy implements InvocationHandler { | ||
|
||
private Supplier<?> target; | ||
|
||
RandomizerProxy(final Supplier<?> target) { | ||
this.target = target; | ||
} | ||
|
||
/** | ||
* Create a dynamic proxy that adapts the given {@link Supplier} to a {@link Randomizer}. | ||
* @param supplier to adapt | ||
* @return the proxy randomizer | ||
*/ | ||
static Randomizer<?> asRandomizer(final Supplier<?> supplier) { | ||
return (Randomizer<?>) Proxy.newProxyInstance( | ||
Randomizer.class.getClassLoader(), | ||
new Class[]{Randomizer.class}, | ||
new RandomizerProxy(supplier)); | ||
} | ||
|
||
@Override | ||
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { | ||
if ("getRandomValue".equals(method.getName())) { | ||
return target.getClass().getMethod("get").invoke(target, args); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
random-beans/src/test/java/io/github/benas/randombeans/RandomizerProxyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.github.benas.randombeans; | ||
|
||
import io.github.benas.randombeans.api.Randomizer; | ||
import org.junit.Test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class RandomizerProxyTest { | ||
|
||
public static final String FOO = "foo"; | ||
|
||
@Test | ||
public void theRandomizerProxyShouldBehaveLikeTheSupplier() throws Exception { | ||
// Given | ||
MySupplier supplier = new MySupplier(); | ||
|
||
// When | ||
Randomizer<?> randomizer = RandomizerProxy.asRandomizer(supplier); | ||
|
||
// Then | ||
assertThat(randomizer.getRandomValue()).isInstanceOf(String.class).isEqualTo(FOO); | ||
} | ||
|
||
class MySupplier implements Supplier<String> { | ||
|
||
@Override | ||
public String get() { | ||
return FOO; | ||
} | ||
|
||
} | ||
} |