-
Notifications
You must be signed in to change notification settings - Fork 0
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
30 changed files
with
3,845 additions
and
28 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 |
---|---|---|
|
@@ -19,7 +19,7 @@ buildscript { | |
} | ||
|
||
group 'io.onixlabs' | ||
version '1.0.0' | ||
version '2.0.0' | ||
|
||
allprojects { | ||
repositories { | ||
|
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
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/io/onixlabs/kotlin/core/typeconverters/BigDecimalTypeConverter.kt
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,47 @@ | ||
/** | ||
* Copyright 2020 Matthew Layton | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.onixlabs.kotlin.core.typeconverters | ||
|
||
import io.onixlabs.kotlin.core.reflection.kotlinClass | ||
import java.math.BigDecimal | ||
import java.math.BigInteger | ||
|
||
/** | ||
* Provides a mechanism to safely convert the specified value value to the [BigDecimal] type. | ||
*/ | ||
class BigDecimalTypeConverter : TypeConverter<BigDecimal>() { | ||
|
||
/** | ||
* Converts the specified value to the [BigDecimal] type. | ||
* | ||
* @param value The value to convert. | ||
* @return Returns a [BigDecimal] representation of the specified value. | ||
*/ | ||
override fun convert(value: Any): BigDecimal = when (value) { | ||
is Boolean -> if (value) BigDecimal.ONE else BigDecimal.ZERO | ||
is Byte -> BigDecimal.valueOf(value.toLong()) | ||
is Short -> BigDecimal.valueOf(value.toLong()) | ||
is Int -> BigDecimal.valueOf(value.toLong()) | ||
is Long -> BigDecimal.valueOf(value) | ||
is BigInteger -> BigDecimal(value) | ||
is Double -> value.toBigDecimal() | ||
is BigDecimal -> value | ||
is String -> value.toBigDecimal().stripTrailingZeros() | ||
is Char -> BigDecimal.valueOf(value.toLong()) | ||
else -> throw IllegalTypeConversionException(value.kotlinClass, BigDecimal::class) | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/kotlin/io/onixlabs/kotlin/core/typeconverters/BigIntegerTypeConverter.kt
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,84 @@ | ||
/** | ||
* Copyright 2020 Matthew Layton | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.onixlabs.kotlin.core.typeconverters | ||
|
||
import io.onixlabs.kotlin.core.isInteger | ||
import io.onixlabs.kotlin.core.math.isInteger | ||
import io.onixlabs.kotlin.core.reflection.kotlinClass | ||
import io.onixlabs.kotlin.core.typeconverters.IllegalTypeConversionException.Companion.NUMERIC_LOSS_OF_PRECISION | ||
import java.math.BigDecimal | ||
import java.math.BigInteger | ||
|
||
/** | ||
* Provides a mechanism to safely convert the specified value value to the [BigInteger] type. | ||
*/ | ||
class BigIntegerTypeConverter : TypeConverter<BigInteger>() { | ||
|
||
/** | ||
* Converts the specified value to the [BigInteger] type. | ||
* | ||
* @param value The value to convert. | ||
* @return Returns an [BigInteger] representation of the specified value. | ||
*/ | ||
override fun convert(value: Any): BigInteger = when (value) { | ||
is Boolean -> if (value) BigInteger.ONE else BigInteger.ZERO | ||
is Byte -> BigInteger.valueOf(value.toLong()) | ||
is Short -> BigInteger.valueOf(value.toLong()) | ||
is Int -> BigInteger.valueOf(value.toLong()) | ||
is Long -> BigInteger.valueOf(value) | ||
is BigInteger -> value | ||
is Float -> value.toBigIntegerChecked() | ||
is Double -> value.toBigIntegerChecked() | ||
is BigDecimal -> value.toBigIntegerChecked() | ||
is String -> value.toBigInteger() | ||
is Char -> BigInteger.valueOf(value.toLong()) | ||
else -> throw IllegalTypeConversionException(value.kotlinClass, BigInteger::class) | ||
} | ||
|
||
/** | ||
* Performs a checked conversion from [Float] to [BigInteger]. | ||
* | ||
* @return Returns a [BigInteger] representation of the specified [Float]. | ||
* @throws IllegalTypeConversionException if the value cannot be safely converted to [BigInteger]. | ||
*/ | ||
private fun Float.toBigIntegerChecked(): BigInteger = when { | ||
isInteger() -> BigInteger.valueOf(toLong()) | ||
else -> throw NUMERIC_LOSS_OF_PRECISION | ||
} | ||
|
||
/** | ||
* Performs a checked conversion from [Double] to [BigInteger]. | ||
* | ||
* @return Returns a [BigInteger] representation of the specified [Double]. | ||
* @throws IllegalTypeConversionException if the value cannot be safely converted to [BigInteger]. | ||
*/ | ||
private fun Double.toBigIntegerChecked(): BigInteger = when { | ||
isInteger() -> BigInteger.valueOf(toLong()) | ||
else -> throw NUMERIC_LOSS_OF_PRECISION | ||
} | ||
|
||
/** | ||
* Performs a checked conversion from [BigDecimal] to [BigInteger]. | ||
* | ||
* @return Returns a [BigInteger] representation of the specified [BigDecimal]. | ||
* @throws IllegalTypeConversionException if the value cannot be safely converted to [BigInteger]. | ||
*/ | ||
private fun BigDecimal.toBigIntegerChecked(): BigInteger = when { | ||
isInteger() -> toBigInteger() | ||
else -> throw NUMERIC_LOSS_OF_PRECISION | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/kotlin/io/onixlabs/kotlin/core/typeconverters/BooleanTypeConverter.kt
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,81 @@ | ||
/** | ||
* Copyright 2020 Matthew Layton | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.onixlabs.kotlin.core.typeconverters | ||
|
||
import io.onixlabs.kotlin.core.reflection.kotlinClass | ||
|
||
/** | ||
* Provides a mechanism to safely convert the specified value value to the [Boolean] type. | ||
*/ | ||
class BooleanTypeConverter : TypeConverter<Boolean>() { | ||
|
||
private companion object { | ||
const val EX_NUM_TO_BOOL = "Illegal type conversion. Numeric value cannot be converted to Boolean." | ||
const val EX_STR_TO_BOOL = "Illegal type conversion. String value cannot be converted to Boolean." | ||
const val EX_CHR_TO_BOOL = "Illegal type conversion. Char value cannot be converted to Boolean." | ||
} | ||
|
||
/** | ||
* Converts the specified value to the [Boolean] type. | ||
* | ||
* @param value The value to convert. | ||
* @return Returns a [Boolean] representation of the specified value. | ||
*/ | ||
override fun convert(value: Any): Boolean = when (value) { | ||
is Boolean -> value | ||
is Number -> value.toBooleanChecked() | ||
is String -> value.toBooleanChecked() | ||
is Char -> value.toBooleanChecked() | ||
else -> throw IllegalTypeConversionException(value.kotlinClass, Boolean::class) | ||
} | ||
|
||
/** | ||
* Performs a checked conversion from [Number] to [Boolean]. | ||
* | ||
* @return Returns a [Boolean] representation of the specified [Number]. | ||
* @throws IllegalTypeConversionException if the value cannot be safely converted to [Boolean]. | ||
*/ | ||
private fun Number.toBooleanChecked(): Boolean = when (this) { | ||
1 -> true | ||
0 -> false | ||
else -> throw IllegalTypeConversionException(EX_NUM_TO_BOOL) | ||
} | ||
|
||
/** | ||
* Performs a checked conversion from [String] to [Boolean]. | ||
* | ||
* @return Returns a [Boolean] representation of the specified [String]. | ||
* @throws IllegalTypeConversionException if the value cannot be safely converted to [Boolean]. | ||
*/ | ||
private fun String.toBooleanChecked(): Boolean = when (this.toLowerCase()) { | ||
"true", "yes", "y", "1" -> true | ||
"false", "no", "n", "0" -> false | ||
else -> throw IllegalTypeConversionException(EX_STR_TO_BOOL) | ||
} | ||
|
||
/** | ||
* Performs a checked conversion from [Char] to [Boolean]. | ||
* | ||
* @return Returns a [Boolean] representation of the specified [Char]. | ||
* @throws IllegalTypeConversionException if the value cannot be safely converted to [Boolean]. | ||
*/ | ||
private fun Char.toBooleanChecked(): Boolean = when (this.toLowerCase()) { | ||
'y', '1' -> true | ||
'n', '0' -> false | ||
else -> throw IllegalTypeConversionException(EX_CHR_TO_BOOL) | ||
} | ||
} |
Oops, something went wrong.