그래서 Scala 에서는 Enumeration 을 상속받은 Object 로 표현하면 됩니다. 다음 코드는 요일을 나타내는 DayOfWeek 값을 표현합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kr.kth.timeperiod | |
import org.joda.time.DateTimeConstants | |
/** | |
* 한 주의 요일을 표현합니다. | |
* User: sunghyouk.bae@gmail.com | |
* Date: 13. 1. 13. | |
*/ | |
object ScalaDayOfWeek extends Enumeration { | |
type ScalaDayOfWeek = Value | |
// NOTE: withName, apply 메소드를 사용하세요 | |
val Monday = Value(DateTimeConstants.MONDAY, "Monday") | |
val Tuesday = Value(DateTimeConstants.TUESDAY, "Tuesday") | |
val Wednesday = Value(DateTimeConstants.WEDNESDAY, "Wednesday") | |
val Thursday = Value(DateTimeConstants.THURSDAY, "Thursday") | |
val Friday = Value(DateTimeConstants.FRIDAY, "Friday") | |
val Saturday = Value(DateTimeConstants.SATURDAY, "Saturday") | |
val Sunday = Value(DateTimeConstants.SUNDAY, "Sunday") | |
} |
그리고 java 의 valueOf(String value) 는 withName(value:String) 을 사용하면 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package kr.kth.timeperiod | |
import kr.kth.commons.slf4j.Logging | |
import org.junit.{Assert, Test} | |
import org.joda.time.DateTimeConstants | |
/** | |
* kr.kth.timeperiod.ScalaDayOfWeekTest | |
* User: sunghyouk.bae@gmail.com | |
* Date: 13. 1. 13. | |
*/ | |
class ScalaDayOfWeekTest extends Logging { | |
@Test | |
def dayOfWeekEquals() { | |
val sunday = ScalaDayOfWeek.Sunday | |
val parsed = ScalaDayOfWeek.withName("Sunday") | |
Assert.assertEquals(sunday, parsed) | |
Assert.assertEquals("Sunday", sunday.toString) | |
Assert.assertNotEquals(ScalaDayOfWeek.Friday,ScalaDayOfWeek.Monday) | |
Assert.assertEquals(ScalaDayOfWeek.Monday, ScalaDayOfWeek(DateTimeConstants.MONDAY)) | |
log.debug(ScalaDayOfWeek.values) | |
} | |
} |
다만 Java 에서 Scala 로 된 라이브러리를 사용하려면, 안되는 것이 많습니다.
제가 고민 중인 부분이기도 합니다.
뭐 경험이 쌓이면 방법이 생기겠죠^^
댓글 없음:
댓글 쓰기