名前系
package com.jtn.springbootsample.domain.model.product
import java.lang.IllegalArgumentException
class ProductName(
val value: String
) {
companion object {
private const val MIN_SIZE = 1
private const val MAX_SIZE = 50
}
init {
if (isUnderMin(value))
throw IllegalArgumentException("ProductName value must be $MIN_SIZE or more. Assigned value is $value")
if (isAboveMax(value))
throw IllegalArgumentException("ProductName value must be $MAX_SIZE or less. Assigned value is $value")
}
fun value(): String = this.value
fun sameValue(other: ProductName): Boolean = this.value == other.value
private fun isUnderMin(value: String): Boolean = value.length < MIN_SIZE
private fun isAboveMax(value: String): Boolean = value.length < MAX_SIZE
}
金額系
package com.jtn.springbootsample.domain.type.amount
import java.lang.IllegalArgumentException
import java.math.BigDecimal
class Amount(
val value: BigDecimal
) {
companion object {
private const val MIN_AMOUNT = 0
private const val MAX_AMOUNT = 999999
fun valueOf(value: Int): Amount = Amount(BigDecimal(value))
}
init {
if (isUnderMin(value))
throw IllegalArgumentException("Amount value must be $MIN_AMOUNT or more. Assigned value is $value")
if (isAboveMax(value))
throw IllegalArgumentException("Amount value must be $MAX_AMOUNT or more. Assigned value is $value")
}
fun canAdd(other: Amount): Boolean {
val result: BigDecimal = addValue(other)
if (isAboveMax(result)) return false
return true
}
fun add(other: Amount): Amount {
val result: BigDecimal = addValue(other)
return Amount(result)
}
fun value(): BigDecimal = this.value
fun sameValue(other: Amount): Boolean {
val result: Int = this.value.compareTo(other.value)
if (result == 0) return true
return false
}
private fun addValue(other: Amount): BigDecimal = this.value.add(other.value)
private fun isUnderMin(value: BigDecimal): Boolean {
val result: Int = value.compareTo(BigDecimal(MIN_AMOUNT))
if (result == -1) return true
return false
}
private fun isAboveMax(value: BigDecimal): Boolean {
val result: Int = value.compareTo(BigDecimal(MIN_AMOUNT))
if (result == 1) return true
return false
}
}