Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMatthewLayton committed Feb 26, 2021
2 parents 5e0b87d + 81aafb4 commit 21e10af
Show file tree
Hide file tree
Showing 30 changed files with 3,845 additions and 28 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@

This document serves as the change log for the ONIXLabs Kotlin Core API.

## Version 2.0.0

#### TypeConverter (abstract class)

Represents the base class for implementing type converters. Type converters represent a mechanism to convert from one type to another, using type-safe convensions.

The following type converters are implemented:

- BigDecimalTypeConverter
- BigIntegerTypeConverter
- BooleanTypeConverter
- ByteTypeConverter
- CharTypeConverter
- DoubleTypeConverter
- FloatTypeConverter
- IntTypeConverter
- LongTypeConverter
- ShortTypeConverter
- StringTypeConverter
- UUIDTypeConverter

### Extensions

#### KType Extensions

- Renamed `name` to `formattedSimpleName`.
- Renamed `fullName` to `formattedQualifiedName`.

## Version 1.0.0

#### ConsoleColor (enum)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ buildscript {
}

group 'io.onixlabs'
version '1.0.0'
version '2.0.0'

allprojects {
repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ import kotlin.reflect.jvm.jvmErasure
/**
* Gets the formatted simple name of the type, including any generic type arguments.
*/
val KType.name: String get() = "${jvmErasure.simpleName}${getFormattedArguments(false)}$nullableToken"
val KType.formattedSimpleName: String
get() = "${jvmErasure.simpleName}${getFormattedArguments(false)}$nullableToken"

/**
* Gets the formatted qualified name of the type, including any generic type arguments.
*/
val KType.fullName: String get() = "${jvmErasure.qualifiedName}${getFormattedArguments(true)}$nullableToken"
val KType.formattedQualifiedName: String
get() = "${jvmErasure.qualifiedName}${getFormattedArguments(true)}$nullableToken"

/**
* Gets a nullable token for the specified string, or an empty string if the type is non-nullable.
*/
private val KType.nullableToken: String get() = if (isMarkedNullable) "?" else String.EMPTY
private val KType.nullableToken: String
get() = if (isMarkedNullable) "?" else String.EMPTY

/**
* Gets a formatted list of generic type argument names.
Expand Down
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)
}
}
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
}
}
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)
}
}
Loading

0 comments on commit 21e10af

Please sign in to comment.