Last active
August 30, 2017 17:08
-
-
Save Miha-x64/691e6454f4070ab33f5bc0574d23f145 to your computer and use it in GitHub Desktop.
Immutable Date subclass
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 net.aquadc.common | |
import java.util.* | |
import kotlin.DeprecationLevel.ERROR | |
import kotlin.UnsupportedOperationException as UOE | |
/** | |
* Created by mike on 17.02.17 | |
*/ | |
class ImDate private constructor(date: Long) : Date(date) { | |
override fun clone(): ImDate = this | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getYear(): Int = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun setYear(year: Int): Unit = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getMonth(): Int = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun setMonth(month: Int): Unit = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getDate(): Int = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun setDate(date: Int): Unit = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getDay(): Int = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getHours(): Int = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun setHours(hours: Int): Unit = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getMinutes(): Int = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun setMinutes(minutes: Int): Unit = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getSeconds(): Int = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun setSeconds(seconds: Int): Unit = throw UOE(DEPRECATED) | |
/* override fun getTime(): Long = super.getTime() // okay_face.jpg */ | |
@Deprecated(message = IMMUTABLE, level = ERROR) override fun setTime(time: Long): Unit = throw UOE(IMMUTABLE) | |
/* override fun before(`when`: Date): Boolean = super.before(`when`) */ | |
/* override fun after(`when`: Date): Boolean = super.after(`when`) */ | |
/* override fun equals(other: Any?): Boolean = super.equals(other) */ | |
/* override fun compareTo(other: Date): Int = super.compareTo(other) */ | |
/* override fun hashCode(): Int = super.hashCode() */ | |
/* override fun toString(): String = super.toString() */ | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun toLocaleString(): String = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun toGMTString(): String = throw UOE(DEPRECATED) | |
@Deprecated(message = DEPRECATED, level = ERROR) override fun getTimezoneOffset(): Int = throw UOE(DEPRECATED) | |
companion object { | |
private const val DEPRECATED = "deprecated since February 12, 1997, i. e. 20 years ago!!" | |
private const val IMMUTABLE = "cannot mutate ImDate, it's immutable!" | |
fun now(): ImDate = | |
ImDate(System.currentTimeMillis()) | |
fun fromDate(date: Date): ImDate = | |
date as? ImDate ?: ImDate(date.time) | |
fun fromMillis(millis: Long): ImDate = | |
ImDate(millis) | |
fun fromSeconds(seconds: Long): ImDate = | |
ImDate(1000 * seconds) | |
} | |
} |
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 net.aquadc.common | |
// imports here... | |
object ImDateTypeAdapterFactory : TypeAdapterFactory { | |
@Suppress("UNCHECKED_CAST") // :( | |
override fun <T : Any?> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? = | |
if (type.rawType == ImDate::class.java) ImDateTypeAdapter as TypeAdapter<T> else null | |
} | |
object ImDateTypeAdapter : TypeAdapter<ImDate>() { | |
private val dateAdapter = DateTypeAdapter() | |
override fun write(out: JsonWriter, value: ImDate?) = dateAdapter.write(out, value) | |
override fun read(`in`: JsonReader?): ImDate? = dateAdapter.read(`in`)?.let { ImDate.fromDate(it) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment