diff --git a/.gitignore b/.gitignore
index e0d53d8..7e762bc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
.gradle
.idea
build
+local.properties
diff --git a/build.gradle b/build.gradle
index 70bf368..2322d69 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,34 +1,44 @@
plugins {
- id "org.jetbrains.kotlin.jvm" version '1.1.2-2'
- id "com.jfrog.bintray" version "1.7.3"
+ id "org.jetbrains.kotlin.jvm" version "1.7.20"
+ id "com.palantir.git-version" version "0.15.0"
id "maven-publish"
}
-group 'th.or.nectec'
-version = {->
- def stdout = new ByteArrayOutputStream()
- exec {
- commandLine 'git', 'describe', '--tags'
- standardOutput = stdout
- }
- return stdout.toString().trim()
-}.call()
+group "th.or.nectec"
+version = versionDetails().lastTag
-sourceCompatibility = 1.7
-targetCompatibility = 1.7
+ext {
+ libraryName = "Thai Units"
+ libraryDescription = "Kotlin library for handle Thai units of measurement"
+
+ groupId = "com.github.nectec-opensource"
+ artifactId = "thai-unit"
+
+ siteUrl = "https://github.com/${project.githubRepo}"
+ gitUrl = "https://github.com/${project.githubRepo}.git"
+
+ developerId = 'piruin'
+ developName = 'Piruin Panichphol'
+ developerEmail = 'piruin.panichphol@nectec.or.th'
+
+ licenseName = "The Apache License, Version 2.0"
+ licenseUrl = "http://www.apache.org/licenses/LICENSE-2.0.txt"
+}
+
+sourceCompatibility = 1.8
+targetCompatibility = 1.8
repositories {
- jcenter()
+ mavenCentral()
}
dependencies {
- def kotlinVersion = '1.1.2'
+ def kotlinVersion = "1.7.20"
- compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
- testCompile 'junit:junit:4.12'
- testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
- testCompile "com.google.code.gson:gson:2.8.1"
+ testImplementation "junit:junit:4.13.2"
+ testImplementation "com.google.code.gson:gson:2.10"
}
-apply from: 'bintray.gradle'
+apply from: './publish/mavencentral.gradle'
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 2a6ca58..ae04661 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,5 @@
-#Tue Jun 06 14:02:38 ICT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip
diff --git a/publish/mavencentral.gradle b/publish/mavencentral.gradle
new file mode 100644
index 0000000..1718a8c
--- /dev/null
+++ b/publish/mavencentral.gradle
@@ -0,0 +1,81 @@
+apply plugin: 'maven-publish'
+apply plugin: 'signing'
+
+group = project.groupId
+version = project.version
+
+ext["ossrhUsername"] = ''
+ext["ossrhPassword"] = ''
+
+File secretPropsFile = project.rootProject.file('local.properties')
+if (secretPropsFile.exists()) {
+ Properties p = new Properties()
+ p.load(new FileInputStream(secretPropsFile))
+ p.each { name, value ->
+ ext[name] = value
+ }
+} else {
+ ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
+ ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
+}
+
+publishing {
+ publications {
+ release(MavenPublication) {
+ groupId project.groupId
+ artifactId project.artifactId
+ version project.version
+
+ artifact("$buildDir/libs/${project.getName()}-${version}.jar")
+
+ pom {
+ name = project.libraryName
+ description = project.libraryDescription
+ url = project.siteUrl
+ licenses {
+ license {
+ name = project.licenseName
+ url = project.licenseUrl
+ }
+ }
+ developers {
+ developer {
+ id = project.developerId
+ name = project.developName
+ email = project.developerEmail
+ }
+ }
+ scm {
+ connection = project.gitUrl
+ developerConnection = project.gitUrl
+ url = project.siteUrl
+ }
+ withXml {
+ def dependenciesNode = asNode().appendNode('dependencies')
+ project.configurations.implementation.allDependencies.each {
+ if (it.name != 'unspecified') {
+ def dependencyNode = dependenciesNode.appendNode('dependency')
+ dependencyNode.appendNode('groupId', it.group)
+ dependencyNode.appendNode('artifactId', it.name)
+ dependencyNode.appendNode('version', it.version)
+ }
+ }
+ }
+ }
+ }
+ }
+ repositories {
+ maven {
+ name = "sonatype"
+ url = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
+ credentials {
+ username ossrhUsername
+ password ossrhPassword
+ }
+ }
+ }
+}
+
+signing {
+ sign publishing.publications
+}
diff --git a/src/main/kotlin/nectec/thai/unit/Area.kt b/src/main/kotlin/nectec/thai/unit/Area.kt
index c72c08c..8962e50 100644
--- a/src/main/kotlin/nectec/thai/unit/Area.kt
+++ b/src/main/kotlin/nectec/thai/unit/Area.kt
@@ -18,6 +18,7 @@
package nectec.thai.unit
import java.lang.StringBuilder
+import kotlin.math.floor
data class Area(val squareMetre: Double) : Comparable {
@@ -97,7 +98,7 @@ data class Area(val squareMetre: Double) : Comparable {
}
}
- fun Double.round(): Any {
- return if (this == Math.floor(this)) this.toInt() else this.toString()
+ private fun Double.round(): Any {
+ return if (this == floor(this)) this.toInt() else this.toString()
}
}
diff --git a/src/test/kotlin/nectec/thai/unit/AreaJavaTest.java b/src/test/kotlin/nectec/thai/unit/AreaJavaTest.kt
similarity index 74%
rename from src/test/kotlin/nectec/thai/unit/AreaJavaTest.java
rename to src/test/kotlin/nectec/thai/unit/AreaJavaTest.kt
index 4f1478a..7c8bd89 100644
--- a/src/test/kotlin/nectec/thai/unit/AreaJavaTest.java
+++ b/src/test/kotlin/nectec/thai/unit/AreaJavaTest.kt
@@ -14,17 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+package nectec.thai.unit
-package nectec.thai.unit;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class AreaJavaTest {
+import org.junit.Assert.assertEquals
+import org.junit.Test
+class AreaJavaTest {
@Test
- public void name() throws Exception {
- Area area = new Area(1600).copy(3200);
- Assert.assertEquals(2, area.getRai());
+ fun name() {
+ val area = Area(1600).copy(3200.0)
+ assertEquals(2, area.rai.toLong())
}
}
diff --git a/src/test/kotlin/nectec/thai/unit/AreaTest.kt b/src/test/kotlin/nectec/thai/unit/AreaTest.kt
index 755ecc1..11ac996 100644
--- a/src/test/kotlin/nectec/thai/unit/AreaTest.kt
+++ b/src/test/kotlin/nectec/thai/unit/AreaTest.kt
@@ -18,39 +18,45 @@
package nectec.thai.unit
import com.google.gson.Gson
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
import org.junit.Test
-import kotlin.test.assertEquals
-import kotlin.test.assertTrue
class AreaTest {
- val area = Area(1600.0)
+ private val area = Area(1600.0)
- @Test fun getRai() {
+ @Test
+ fun getRai() {
assertEquals(1, area.rai)
}
- @Test fun getNgan() {
+ @Test
+ fun getNgan() {
assertEquals(0, area.ngan)
}
- @Test fun getSquareWa() {
- assertEquals(0.0, area.squareWa)
+ @Test
+ fun getSquareWa() {
+ assertEquals(0.0, area.squareWa, .0)
}
- @Test fun getSquareMetre() {
- assertEquals(1600.0, area.squareMetre)
+ @Test
+ fun getSquareMetre() {
+ assertEquals(1600.0, area.squareMetre, .0)
}
- @Test fun formalPrint() {
- assertEquals("1 ไร่", area.formalPrint());
- assertEquals("2 งาน", Area(0, 2, 0).formalPrint());
- assertEquals("1 ตารางวา", Area(0, 0, 1).formalPrint());
- assertEquals("1 ไร่ 99 ตารางวา", Area(0, 0, 499).formalPrint());
- assertEquals("2 ไร่ 3 งาน 99 ตารางวา", Area(0, 7, 499).formalPrint());
+ @Test
+ fun formalPrint() {
+ assertEquals("1 ไร่", area.formalPrint())
+ assertEquals("2 งาน", Area(0, 2, 0).formalPrint())
+ assertEquals("1 ตารางวา", Area(0, 0, 1).formalPrint())
+ assertEquals("1 ไร่ 99 ตารางวา", Area(0, 0, 499).formalPrint())
+ assertEquals("2 ไร่ 3 งาน 99 ตารางวา", Area(0, 7, 499).formalPrint())
}
- @Test fun prettyPrint() {
+ @Test
+ fun prettyPrint() {
assertEquals("1-0-0 ไร่", area.prettyPrint())
assertEquals("0-2-0 ไร่", Area(0, 2, 0).prettyPrint())
assertEquals("0-0-1.9 ไร่", Area(0, 0, 1.9).prettyPrint())
@@ -58,35 +64,37 @@ class AreaTest {
assertEquals("2-3-99.75 ไร่", Area(0, 7, 499.75).prettyPrint())
}
- @Test fun rounding() {
+ @Test
+ fun rounding() {
val ex1 = Area(2400.0)
assertEquals(1, ex1.rai)
assertEquals(2, ex1.ngan)
- assertEquals(0.0, ex1.squareWa)
+ assertEquals(0.0, ex1.squareWa, .0)
val ex2 = Area(1599.0)
assertEquals(0, ex2.rai)
assertEquals(3, ex2.ngan)
- assertEquals(99.75, ex2.squareWa)
+ assertEquals(99.75, ex2.squareWa, .0)
val ex3 = Area(1605.0)
assertEquals(1, ex3.rai)
assertEquals(0, ex3.ngan)
- assertEquals(1.25, ex3.squareWa)
+ assertEquals(1.25, ex3.squareWa, .0)
val ex4 = Area(1601.0)
assertEquals(1, ex4.rai)
assertEquals(0, ex4.ngan)
- assertEquals(0.25, ex4.squareWa)
+ assertEquals(0.25, ex4.squareWa, .0)
}
- @Test fun gsonToJson() {
- assertEquals("{\"rai\":1,\"ngan\":0,\"squareWa\":0.0,\"squareMetre\":1600.0}",
+ @Test
+ fun gsonToJson() {
+ assertEquals("{\"squareMetre\":1600.0,\"rai\":1,\"ngan\":0,\"squareWa\":0.0}",
Gson().toJson(Area(1600.0)))
-
}
- @Test fun gsonFromJson() {
+ @Test
+ fun gsonFromJson() {
val area = Gson().fromJson("{\"rai\":1,\"ngan\":0,\"squareWa\":0,\"squareMetre\":1600}",
Area::class.java)
assertEquals(1, area.rai)
@@ -95,7 +103,7 @@ class AreaTest {
@Test
fun compare() {
assertTrue(Area(1, 0, 0) > Area(0, 3, 99))
- assertTrue(Area(1, 0, 0) == Area(1600.0))
+ assertEquals(Area(1, 0, 0), Area(1600.0))
assertTrue(Area(2, 0, 0) < Area(4, 0, 99))
}
}