-
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
451 additions
and
24 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,72 @@ | ||
package pro.fessional.mirana.best; | ||
|
||
|
||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* <pre> | ||
* Usage: mark value and its ref type. | ||
* {@code | ||
* Map<String, Integer> map = new HashMap<>(); | ||
* map.put("key", 42); | ||
* TypedRef<String, Integer> ref = new TypedRef<>("key"); | ||
* Integer result = ref.get(map); | ||
* } | ||
* </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; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Contract("_,true->!null") | ||
public R get(@NotNull Function<V, ?> map, boolean nonnull) throws ClassCastException { | ||
Object obj = map.apply(value); | ||
if (obj == null && nonnull) { | ||
throw new ClassCastException("null cast to nonnull"); | ||
} | ||
return (R) obj; | ||
} | ||
|
||
@Contract("_,!null ->!null") | ||
public R getOr(@NotNull Function<V, ?> map, R elze) throws ClassCastException { | ||
final R obj = get(map, false); | ||
return obj != null ? obj : elze; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Contract("_,!null ->!null") | ||
public R tryOr(@Nullable Object obj, R elze) throws ClassCastException { | ||
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()); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/test/java/pro/fessional/mirana/best/TypedKeyTest.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,113 @@ | ||
package pro.fessional.mirana.best; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* @author trydofor | ||
* @since 2025-01-22 | ||
*/ | ||
class TypedKeyTest { | ||
static class TestTypedKey extends TypedKey<String> {} | ||
|
||
@Test | ||
void testConstructor() { | ||
TypedKey<String> key = new TestTypedKey(); | ||
assertNotNull(key.regType); | ||
assertNotNull(key.valType); | ||
assertEquals(String.class, key.valType); | ||
} | ||
|
||
@Test | ||
void testGet() { | ||
TypedKey<String> key = new TestTypedKey(); | ||
Map<TypedKey<?>, Object> map = new HashMap<>(); | ||
map.put(key, "value"); | ||
|
||
String value = key.get(map::get, true); | ||
assertEquals("value", value); | ||
} | ||
|
||
@Test | ||
void testGetOr() { | ||
TypedKey<String> key = new TestTypedKey(); | ||
Map<TypedKey<?>, Object> map = new HashMap<>(); | ||
|
||
// Test with no value in the map | ||
String defaultValue = "default"; | ||
String value = key.getOr(map::get, defaultValue); | ||
assertEquals(defaultValue, value); | ||
|
||
// Test with a value in the map | ||
map.put(key, "value"); | ||
value = key.getOr(map::get, defaultValue); | ||
assertEquals("value", value); | ||
} | ||
|
||
@Test | ||
void testTryOr() { | ||
TypedKey<String> key = new TestTypedKey(); | ||
|
||
// Test with null object | ||
String defaultValue = "default"; | ||
String value = key.tryOr(null, defaultValue); | ||
assertEquals(defaultValue, value); | ||
|
||
// Test with non-null object | ||
value = key.tryOr("value", defaultValue); | ||
assertEquals("value", value); | ||
} | ||
|
||
@Test | ||
void testSerializeDeserialize() { | ||
TypedKey<String> key = new TestTypedKey(); | ||
String serialized = key.serialize(); | ||
|
||
TypedKey<String> deserializedKey = TypedKey.deserialize(serialized); | ||
assertNotNull(deserializedKey); | ||
assertEquals(key.regType, deserializedKey.regType); | ||
} | ||
|
||
@Test | ||
void testDeserializeNonNull() { | ||
TypedKey<String> key = new TestTypedKey(); | ||
String serialized = key.serialize(); | ||
|
||
TypedKey<String> deserializedKey = TypedKey.deserialize(serialized, true); | ||
assertNotNull(deserializedKey); | ||
assertSame(key, deserializedKey); | ||
} | ||
|
||
@Test | ||
void testDeserializeNullable() { | ||
TypedKey<String> deserializedKey = TypedKey.deserialize("nonexistent", false); | ||
assertNull(deserializedKey); | ||
} | ||
|
||
@Test | ||
void testEqualsAndHashCode() { | ||
TypedKey<String> key1 = new TestTypedKey(); | ||
TypedKey<String> key2 = new TestTypedKey(); | ||
|
||
assertEquals(key1, key1); // Reflexive | ||
assertNotEquals(key1, key2); // Different instances | ||
assertEquals(key1.hashCode(), key1.hashCode()); | ||
} | ||
|
||
@Test | ||
void testToString() { | ||
TypedKey<String> key = new TestTypedKey(); | ||
String toString = key.toString(); | ||
assertTrue(toString.contains("regType")); | ||
assertTrue(toString.contains("valType")); | ||
} | ||
} |
Oops, something went wrong.