-
Notifications
You must be signed in to change notification settings - Fork 1
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
9 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
43 changes: 43 additions & 0 deletions
43
honey-common/src/dev/shiza/honey/conversion/ImplicitConversion.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,43 @@ | ||
package dev.shiza.honey.conversion; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public final class ImplicitConversion { | ||
|
||
private final Map<Class<?>, Function<Object, Object>> conversions; | ||
|
||
ImplicitConversion(final Set<ImplicitConversionUnit> units) { | ||
this.conversions = | ||
units.stream() | ||
.collect( | ||
Collectors.toMap(ImplicitConversionUnit::from, ImplicitConversionUnit::conversion)); | ||
} | ||
|
||
public static ImplicitConversion create(final Set<ImplicitConversionUnit> units) { | ||
return new ImplicitConversion(units); | ||
} | ||
|
||
public <T> T convert(final Object value) { | ||
Object currentValue = value; | ||
|
||
boolean conversionFound; | ||
do { | ||
conversionFound = false; | ||
for (final Map.Entry<Class<?>, Function<Object, Object>> entry : conversions.entrySet()) { | ||
if (entry.getKey().isAssignableFrom(currentValue.getClass())) { | ||
final Object convertedValue = entry.getValue().apply(currentValue); | ||
if (convertedValue != null) { | ||
currentValue = convertedValue; | ||
conversionFound = true; | ||
break; | ||
} | ||
} | ||
} | ||
} while (conversionFound); | ||
|
||
return (T) currentValue; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
honey-common/src/dev/shiza/honey/conversion/ImplicitConversionException.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,8 @@ | ||
package dev.shiza.honey.conversion; | ||
|
||
public final class ImplicitConversionException extends IllegalStateException { | ||
|
||
public ImplicitConversionException(final String message) { | ||
super(message); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
honey-common/src/dev/shiza/honey/conversion/ImplicitConversionUnit.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,21 @@ | ||
package dev.shiza.honey.conversion; | ||
|
||
import java.util.function.Function; | ||
|
||
public record ImplicitConversionUnit( | ||
Class<?> from, Class<?> into, Function<Object, Object> conversion) { | ||
|
||
public static <T, R> ImplicitConversionUnit unchecked( | ||
final Class<T> from, final Class<R> into, final Function<T, R> conversion) { | ||
if (from == null || into == null || conversion == null) { | ||
throw new ImplicitConversionException( | ||
"Cannot create an implicit conversion unit with null values."); | ||
} | ||
|
||
if (from.equals(into)) { | ||
throw new ImplicitConversionException("Cannot create an recursive conversion unit."); | ||
} | ||
|
||
return new ImplicitConversionUnit(from, into, (Function<Object, Object>) conversion); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
honey-common/src/dev/shiza/honey/conversion/package-info.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,4 @@ | ||
@Experimental | ||
package dev.shiza.honey.conversion; | ||
|
||
import org.jetbrains.annotations.ApiStatus.Experimental; |