-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
670 additions
and
31 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
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,103 @@ | ||
package pro.fessional.mirana.best; | ||
|
||
|
||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* <pre> | ||
* Usage: mark value and its ref type. | ||
* {@code | ||
* Map<String, Integer> map = new HashMap<>(); | ||
* TypedRef<String, Integer> ref = new TypedRef<>("key"); | ||
* map.put("key", 42); | ||
* Integer result = ref.get(map, false); | ||
* } | ||
* </pre> | ||
* | ||
* @author trydofor | ||
* @since 2025-01-21 | ||
*/ | ||
public class TypedRef<V, R> { | ||
@NotNull | ||
public final V value; | ||
|
||
public TypedRef(@NotNull V value) { | ||
this.value = value; | ||
} | ||
|
||
public void set(@NotNull BiConsumer<V, R> fun, R refer) { | ||
fun.accept(value, refer); | ||
} | ||
|
||
public void set(@NotNull Map<V, ? super R> map, R refer) { | ||
map.put(value, refer); | ||
} | ||
|
||
@Contract("_,true->!null") | ||
public R get(@NotNull Function<V, R> fun, boolean nonnull) { | ||
R obj = fun.apply(value); | ||
if (obj == null && nonnull) { | ||
throw new NullPointerException("cannot be null, value=" + value); | ||
} | ||
return obj; | ||
} | ||
|
||
@Contract("_,true->!null") | ||
public R get(@NotNull Map<V, ? extends R> map, boolean nonnull) { | ||
R obj = map.get(value); | ||
if (obj == null && nonnull) { | ||
throw new NullPointerException("cannot be null, value=" + value); | ||
} | ||
return obj; | ||
} | ||
|
||
@Contract("_,!null ->!null") | ||
public R getOr(@NotNull Function<V, R> fun, R elze) { | ||
final R obj = fun.apply(value); | ||
return obj != null ? obj : elze; | ||
} | ||
|
||
@Contract("_,!null ->!null") | ||
public R getOr(@NotNull Map<V, ? extends R> map, R elze) { | ||
final R obj = map.get(value); | ||
return obj != null ? obj : elze; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Contract("_,!null ->!null") | ||
public R tryOr(@NotNull Function<V, ?> fun, R elze) throws ClassCastException { | ||
final Object obj = fun.apply(value); | ||
return obj != null ? (R) obj : elze; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Contract("_,!null ->!null") | ||
public R tryOr(@NotNull Map<?, ?> map, R elze) throws ClassCastException { | ||
final Object obj = map.get(value); | ||
return obj != null ? (R) obj : elze; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
TypedRef<?, ?> typedRef = (TypedRef<?, ?>) o; | ||
return Objects.equals(value, typedRef.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value.toString(); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
src/test/java/pro/fessional/mirana/best/DummyBlockTest.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,64 @@ | ||
package pro.fessional.mirana.best; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class DummyBlockTest { | ||
|
||
@Test | ||
void testIgnore() { | ||
AtomicReference<Throwable> captured = new AtomicReference<>(); | ||
DummyBlock.TweakIgnore.tweakGlobal(captured::set); | ||
|
||
Throwable testException = new RuntimeException("Test exception"); | ||
DummyBlock.ignore(testException); | ||
|
||
assertEquals(testException, captured.get()); | ||
|
||
DummyBlock.TweakIgnore.resetGlobal(); | ||
} | ||
|
||
@Test | ||
void testIgnoreWithoutHandler() { | ||
// Test ignore with no handler set | ||
Throwable testException = new RuntimeException("Test exception"); | ||
assertDoesNotThrow(() -> DummyBlock.ignore(testException)); | ||
} | ||
|
||
@Test | ||
void testEmpty() { | ||
assertDoesNotThrow(DummyBlock::empty); | ||
} | ||
|
||
@Test | ||
void testNever() { | ||
IllegalStateException exception = assertThrows(IllegalStateException.class, DummyBlock::never); | ||
assertEquals("should NOT invoke NEVER", exception.getMessage()); | ||
|
||
exception = assertThrows(IllegalStateException.class, () -> DummyBlock.never("Custom message")); | ||
assertEquals("should NOT invoke NEVER:Custom message", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void testTodo() { | ||
IllegalStateException exception = assertThrows(IllegalStateException.class, DummyBlock::todo); | ||
assertEquals("should NOT invoke TODO", exception.getMessage()); | ||
|
||
exception = assertThrows(IllegalStateException.class, () -> DummyBlock.todo("Custom message")); | ||
assertEquals("should NOT invoke TODO:Custom message", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void testFixme() { | ||
IllegalStateException exception = assertThrows(IllegalStateException.class, DummyBlock::fixme); | ||
assertEquals("should NOT invoke FIXME", exception.getMessage()); | ||
|
||
exception = assertThrows(IllegalStateException.class, () -> DummyBlock.fixme("Custom message")); | ||
assertEquals("should NOT invoke FIXME:Custom message", exception.getMessage()); | ||
} | ||
} |
Oops, something went wrong.