Skip to content
This repository was archived by the owner on Oct 26, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.gradle
.idea
build
local.properties
50 changes: 30 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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'
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
81 changes: 81 additions & 0 deletions publish/mavencentral.gradle
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions src/main/kotlin/nectec/thai/unit/Area.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package nectec.thai.unit

import java.lang.StringBuilder
import kotlin.math.floor

data class Area(val squareMetre: Double) : Comparable<Area> {

Expand Down Expand Up @@ -97,7 +98,7 @@ data class Area(val squareMetre: Double) : Comparable<Area> {
}
}

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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
60 changes: 34 additions & 26 deletions src/test/kotlin/nectec/thai/unit/AreaTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,83 @@
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())
assertEquals("1-0-99.5 ไร่", Area(0, 0, 499.5).prettyPrint())
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)
Expand All @@ -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))
}
}