Minimize xml size & avoid storing date information as a string
[idea/community.git] / plugins / stats-collector / src / com / intellij / stats / personalization / impl / DayImpl.kt
1 package com.intellij.stats.personalization.impl
2
3 import com.intellij.stats.personalization.Day
4 import java.text.ParsePosition
5 import java.text.SimpleDateFormat
6 import java.util.*
7
8 /**
9  * @author Vitaliy.Bibaev
10  */
11 class DayImpl(date: Date) : Day {
12     override val dayOfMonth: Int
13     override val month: Int
14     override val year: Int
15
16     companion object {
17         private val DATE_FORMAT = SimpleDateFormat("dd-MM-yyyy")
18
19         fun fromString(str: String): Day? {
20             val position = ParsePosition(0)
21             val date = DATE_FORMAT.parse(str, position)
22             if (position.index == 0) return null
23             return DayImpl(date)
24         }
25     }
26
27     init {
28         val calendar = Calendar.getInstance()
29         calendar.time = date
30         dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
31         month = calendar.get(Calendar.MONTH)
32         year = calendar.get(Calendar.YEAR)
33     }
34
35     override fun compareTo(other: Day): Int {
36         if (year == year) {
37             if (month == month) {
38                 return dayOfMonth.compareTo(other.dayOfMonth)
39             }
40             return month.compareTo(other.month)
41         }
42         return year.compareTo(other.year)
43     }
44
45     override fun hashCode(): Int {
46         return Objects.hash(year, month, dayOfMonth)
47     }
48
49     override fun equals(other: Any?): Boolean {
50         if (other != null && other is Day) return compareTo(other) == 0
51         return false
52     }
53
54     override fun toString(): String {
55         return "$dayOfMonth-$month-$year"
56     }
57 }