Skip to content

Commit

Permalink
Add kure-potlin
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSikora committed Jun 2, 2021
1 parent 82ed959 commit 1b5cb99
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 42 deletions.
9 changes: 0 additions & 9 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ apply plugin: libs.detekt.gradlePluginId
dependencies {
detekt libs.detekt.formatting
detekt libs.detekt.cli
detektPlugins libs.kurePotlin
}

tasks.withType(Detekt) {
Expand Down
26 changes: 26 additions & 0 deletions detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,29 @@ style:
active: true
WildcardImport:
active: true

impure:
active: true
LoopUsage:
active: true
ReturnStatement:
active: true
VariableUsage:
active: true
ReturnUnit:
active: true
ignoreFunctionType: false
ignoredAnnotations: [ ]
ignoreDsl: false
ClassDefinition:
active: false
AbstractClassDefinition:
active: false
ThrowExpression:
active: true
MutableCollections:
active: true
BranchStatement:
active: true
MissingElse:
active: false
1 change: 1 addition & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ext.libs = [
"formatting": "io.gitlab.arturbosch.detekt:detekt-formatting:${versions.detekt}",
"cli": "io.gitlab.arturbosch.detekt:detekt-cli:${versions.detekt}",
],
"kurePotlin": "pl.setblack:kure-potlin:0.5.0",
"gradleVersions": [
"gradlePlugin": "com.github.ben-manes:gradle-versions-plugin:0.39.0",
"gradlePluginId": "com.github.ben-manes.versions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import io.mehow.ruler.format.FormattingDriver
import io.mehow.ruler.format.withAndroidContext

public object RulerInitializer : Initializer<FormattingDriver> {
override fun create(context: Context): FormattingDriver {
val driver = FormattingDriver.Builder()
.withAndroidContext(context)
.build()
Ruler.driver = driver
return driver
}
override fun create(context: Context): FormattingDriver = FormattingDriver.Builder()
.withAndroidContext(context)
.build()
.also { Ruler.driver = it }

override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
}
32 changes: 18 additions & 14 deletions library/ruler/src/main/java/io/mehow/ruler/Distance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public class Distance private constructor(
/**
* Divides this distance by specified value.
*/
@Suppress("ThrowExpression")
public operator fun div(divisor: Long): Distance = when (divisor) {
0L -> throw ArithmeticException("Cannot divide by 0.")
1L -> this
Expand All @@ -161,6 +162,7 @@ public class Distance private constructor(
/**
* Divides this distance by specified value.
*/
@Suppress("ThrowExpression")
public operator fun div(divisor: Double): Distance = when (divisor) {
0.0 -> throw ArithmeticException("Cannot divide by 0.")
1.0 -> this
Expand All @@ -175,10 +177,10 @@ public class Distance private constructor(
/**
* Compares this distance based on the space quantity.
*/
override fun compareTo(other: Distance): Int {
val cmp = metersPart.compareTo(other.metersPart)
return if (cmp != 0) cmp else nanosPart.compareTo(other.nanosPart)
}
override fun compareTo(other: Distance): Int = metersPart
.compareTo(other.metersPart)
.takeIf { it != 0 }
?: nanosPart.compareTo(other.nanosPart)

override fun equals(other: Any?): Boolean = other is Distance &&
metersPart == other.metersPart &&
Expand Down Expand Up @@ -212,27 +214,29 @@ public class Distance private constructor(
*/
public val Epsilon: Distance = Distance(nanosPart = 1)

internal fun create(meters: BigDecimal): Distance {
@Suppress("ThrowExpression")
internal fun create(meters: BigDecimal): Distance = run {
val nanos = meters.movePointRight(9).toBigInteger()
val divRem = nanos.divideAndRemainder(nanosInMeterBig)
if (divRem[0].bitLength() > 63) {
throw ArithmeticException("Exceeded distance capacity: $nanos nm.")
}
val storedMeters = divRem[0].toLong()
val storedNanometers = divRem[1].toLong()
return create(storedMeters, storedNanometers)
create(storedMeters, storedNanometers)
}

@Suppress("ThrowExpression")
internal fun create(
meters: Long = 0,
nanometers: Long = 0,
): Distance {
var meterPart = nanometers / nanosInMeter
var nanoPart = nanometers % nanosInMeter
if (nanoPart < 0) {
nanoPart += nanosInMeter
meterPart--
}
): Distance = run {
val rawNanoPart = nanometers % nanosInMeter
val rawMeterPart = nanometers / nanosInMeter
val takeIfNoOverflow: Long.() -> Long? = { takeUnless { rawNanoPart < 0 } }

val nanoPart = rawNanoPart.takeIfNoOverflow() ?: rawNanoPart + nanosInMeter
val meterPart = rawMeterPart.takeIfNoOverflow() ?: rawMeterPart - 1

val totalMeters = meters.safeAdd(meterPart)
val totalNanometers = nanoPart
Expand All @@ -242,7 +246,7 @@ public class Distance private constructor(
throw ArithmeticException("Exceeded meters capacity: $totalMeters m")
}

return Distance(totalMeters, totalNanometers)
Distance(totalMeters, totalNanometers)
}

/**
Expand Down
6 changes: 2 additions & 4 deletions library/ruler/src/main/java/io/mehow/ruler/Length.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class Length<T : LengthUnit<T>> internal constructor(
/**
* Ensures that a unit is within a specified range.
*/
@Suppress("ThrowExpression")
public fun coerceUnitIn(range: ClosedRange<T>): Length<T> = when {
range.isEmpty() -> throw IllegalArgumentException("Range cannot be empty!")
unit > range.endInclusive -> Length(distance, range.endInclusive)
Expand Down Expand Up @@ -118,10 +119,7 @@ public class Length<T : LengthUnit<T>> internal constructor(
driver: FormattingDriver,
converter: LengthConverter?,
formatter: LengthFormatter,
): String {
val length = converter?.convert(this) ?: this
return formatter.format(length, driver)
}
): String = formatter.format(converter?.convert(this) ?: this, driver)

/**
* Returns a length whose value is the absolute value of this length.
Expand Down
2 changes: 2 additions & 0 deletions library/ruler/src/main/java/io/mehow/ruler/LongMath.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("ReturnStatement", "ThrowExpression")

/*
* Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
*
Expand Down
1 change: 1 addition & 0 deletions library/ruler/src/main/java/io/mehow/ruler/Ruler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.mehow.ruler.format.NoOpFormatter
* or [formatter factories][LengthFormatter.Factory]. If no factories are installed Ruler uses built-in factories
* to convert and format input.
*/
@Suppress("VariableDefinition")
public object Ruler : LengthConverter, LengthFormatter {
/**
* Default driver for lengths and distances formatting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class FormattingDriver internal constructor(
/**
* Formats length to a human-readable form using a supplied context.
*/
public fun format(length: Length<*>, context: FormattingContext = formattingContext): String {
public fun format(length: Length<*>, context: FormattingContext = formattingContext): String = run {
val value = measureFormatter.format(length.measure, MeasureContext(context.fractionalPrecision, translator.locale))
val separator = context.unitSeparator
val unit = translator.symbol(length.unit)
return value + separator + unit
value + separator + unit
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public class ImperialFormatter internal constructor(
private val partSeparator = builder.partSeparator
private val fallbackFormatter = builder.fallbackFormatter

override fun format(length: Length<*>, driver: FormattingDriver): String {
override fun format(length: Length<*>, driver: FormattingDriver): String = run {
val noFractionContext = driver.formattingContext.newBuilder().withPrecision(0).build()
val noFractionDriver = driver.newBuilder().withFormattingContext(noFractionContext).build()

val parts = length.formatUnitParts(noFractionDriver)
return when {
when {
parts.isEmpty() -> fallbackFormatter.format(length, noFractionDriver)
else -> parts.joinToString(partSeparator)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.mehow.ruler.SiLengthUnit.Micrometer
import io.mehow.ruler.SiLengthUnit.Millimeter
import io.mehow.ruler.SiLengthUnit.Nanometer

@Suppress("ReturnStatement")
internal class SampleActivity : Activity() {
override fun onCreate(inState: Bundle?) {
super.onCreate(inState)
Expand Down
8 changes: 4 additions & 4 deletions sample/src/main/java/io/mehow/ruler/sample/UnitAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.mehow.ruler.sample.R.layout
internal class UnitAdapter : BaseAdapter() {
private val units = LengthUnit.units

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View = run {
val inflater = LayoutInflater.from(parent.context)
val viewHolder = if (convertView == null) {
val view = inflater.inflate(layout.io_mehow_ruler_unit_spinner_item, parent, false)
Expand All @@ -20,10 +20,10 @@ internal class UnitAdapter : BaseAdapter() {
convertView.tag as UnitViewHolder
}
viewHolder.bind(getItem(position))
return viewHolder.item
viewHolder.item
}

override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View = run {
val inflater = LayoutInflater.from(parent.context)
val viewHolder = if (convertView == null) {
val view = inflater.inflate(layout.io_mehow_ruler_unit_drop_down_item, parent, false)
Expand All @@ -32,7 +32,7 @@ internal class UnitAdapter : BaseAdapter() {
convertView.tag as UnitViewHolder
}
viewHolder.bind(getItem(position))
return viewHolder.item
viewHolder.item
}

override fun getCount() = units.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.appcompat.widget.AppCompatSpinner
import io.mehow.ruler.LengthUnit
import androidx.appcompat.R as AppCompatR

@Suppress("VariableDefinition")
internal class UnitViewGroup @JvmOverloads constructor(
context: Context,
attrs: AttributeSet,
Expand Down

0 comments on commit 1b5cb99

Please sign in to comment.