-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Argument transforms are now done by the Args utility
- Loading branch information
Showing
9 changed files
with
117 additions
and
32 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
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,33 @@ | ||
package com.obsidiandynamics.zerolog; | ||
|
||
import java.util.function.*; | ||
|
||
/** | ||
* Utilities for lazily-invoked argument transformations. | ||
*/ | ||
public final class Args { | ||
private Args() {} | ||
|
||
/** | ||
* Forms a supplier of the given object reference. | ||
* | ||
* @param <T> Value type. | ||
* @param value The value to supply. | ||
* @return A {@link Supplier} of the given value. | ||
*/ | ||
public static <T> Supplier<T> ref(T value) { | ||
return () -> value; | ||
} | ||
|
||
/** | ||
* Maps the value returned by the given supplier to any type via the given transform. | ||
* | ||
* @param <T> Value type. | ||
* @param supplier The source of the value to transform. | ||
* @param transform The transform function. | ||
* @return A {@link Supplier} that will perform the transform when invoked. | ||
*/ | ||
public static <T> Supplier<?> map(Supplier<? extends T> supplier, Function<? super T, ?> transform) { | ||
return () -> transform.apply(supplier.get()); | ||
} | ||
} |
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
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
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
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 com.obsidiandynamics.zerolog; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.function.*; | ||
|
||
import org.junit.*; | ||
|
||
import com.obsidiandynamics.assertion.*; | ||
import com.obsidiandynamics.func.*; | ||
|
||
public final class ArgsTest { | ||
@Test | ||
public void testConformance() { | ||
Assertions.assertUtilityClassWellDefined(Args.class); | ||
} | ||
|
||
@Test | ||
public void testRef() { | ||
final Object obj = "obj"; | ||
assertSame(obj, Args.ref(obj).get()); | ||
} | ||
|
||
@Test | ||
public void testMap() { | ||
final Supplier<String> supplier = Classes.cast(mock(Supplier.class)); | ||
when(supplier.get()).thenReturn("str"); | ||
|
||
final Function<String, Object> transform = Classes.cast(mock(Function.class)); | ||
when(transform.apply(any())).thenReturn("transformed"); | ||
|
||
final Supplier<?> x = Args.map(supplier, transform); | ||
verifyNoMoreInteractions(supplier); | ||
verifyNoMoreInteractions(transform); | ||
|
||
assertEquals("transformed", x.get()); | ||
verify(supplier).get(); | ||
verify(transform).apply(eq("str")); | ||
} | ||
} |
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
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
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