public class Receipt implements Parcelable {
@NonNull
private final Date date;
private final int value;
public Receipt(@NonNull Date date, int value) {
this.date = date;
this.value = value;
}
@NonNull
public Date getDate() {
return date;
}
public int getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Receipt receipt = (Receipt) o;
if (value != receipt.value) return false;
return date.equals(receipt.date);
}
@Override
public int hashCode() {
int result = date.hashCode();
result = 31 * result + value;
return result;
}
//
// Parcelable
//
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(date);
dest.writeInt(value);
}
public static final Creator<Receipt> CREATOR = new Creator<Receipt>() {
@Override
public Receipt createFromParcel(Parcel in) {
return new Receipt((Date) in.readSerializable(), in.readInt());
}
@Override
public Receipt[] newArray(int size) {
return new Receipt[size];
}
};
}
Kotlin 自動変換直後
class Receipt(val date: Date, val value: Int) : Parcelable {
override fun equals(o: Any?): Boolean {
if (this === o) return true
if (o == null || javaClass != o.javaClass) return false
val receipt = o as Receipt?
if (value != receipt!!.value) return false
return date == receipt.date
}
override fun hashCode(): Int {
var result = date.hashCode()
result = 31 * result + value
return result
}
//
// Parcelable
//
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeSerializable(date)
dest.writeInt(value)
}
companion object {
val CREATOR: Parcelable.Creator<Receipt> = object : Parcelable.Creator<Receipt> {
override fun createFromParcel(`in`: Parcel): Receipt {
return Receipt(`in`.readSerializable() as Date, `in`.readInt())
}
override fun newArray(size: Int): Array<Receipt> {
return arrayOfNulls(size)
}
}
}
}
return arrayOfNulls(size) で型があわないと言われるので、newArray() の返す型を Array<Receipt?> にする
override fun newArray(size: Int): Array {
return arrayOfNulls(size)
}
CREATOR が Java から見えるように @JvmField をつける
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Receipt> = object : Parcelable.Creator<Receipt> {
(Parcelable とは関係ないが) data クラスにして equals() と hashCode() を省略する
data class Receipt(val date: Date, val value: Int) : Parcelable {
override fun describeContents(): Int {
return 0
}
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeSerializable(date)
dest.writeInt(value)
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Receipt> = object : Parcelable.Creator<Receipt> {
override fun createFromParcel(`in`: Parcel): Receipt {
return Receipt(`in`.readSerializable() as Date, `in`.readInt())
}
override fun newArray(size: Int): Array<Receipt?> {
return arrayOfNulls(size)
}
}
}
}
describeContents() と newArray() で = を使って関数の本体を省略
data class Receipt(val date: Date, val value: Int) : Parcelable {
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeSerializable(date)
dest.writeInt(value)
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Receipt> = object : Parcelable.Creator<Receipt> {
override fun createFromParcel(`in`: Parcel): Receipt {
return Receipt(`in`.readSerializable() as Date, `in`.readInt())
}
override fun newArray(size: Int): Array<Receipt?> = arrayOfNulls(size)
}
}
}
createFromParcel() の引数名に Java では in が使われているが、 Kotlin では予約語なので ` でエスケープされている。紛らわしいので source に変える
data class Receipt(val date: Date, val value: Int) : Parcelable {
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeSerializable(date)
dest.writeInt(value)
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Receipt> = object : Parcelable.Creator<Receipt> {
override fun createFromParcel(source: Parcel): Receipt {
return Receipt(source.readSerializable() as Date, source.readInt())
}
override fun newArray(size: Int): Array<Receipt?> = arrayOfNulls(size)
}
}
}
writeToParcel() の dest と createFromParcel() の source でスコープ関数を使う(お好みで)
data class Receipt(val date: Date, val value: Int) : Parcelable {
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.run {
writeSerializable(date)
writeInt(value)
}
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Receipt> = object : Parcelable.Creator<Receipt> {
override fun createFromParcel(source: Parcel): Receipt
= source.run { Receipt(readSerializable() as Date, readInt()) }
override fun newArray(size: Int): Array<Receipt?> = arrayOfNulls(size)
}
}
}
run() を使っています。他のスコープ関数(let, with, run, apply, also)を使うと次のようになります。
writeToParcel() ではどれを使ってもやりたいことはできます。
選ぶとしたら it が省略できる(そのレシーバを this とする関数を呼び出す) with か run か apply がよさそうです。
// let
dest.let {
it.writeSerializable(date)
it.writeInt(value)
}
// with
with(dest) {
writeSerializable(date)
writeInt(value)
}
// run
dest.run {
writeSerializable(date)
writeInt(value)
}
// apply
dest.apply {
writeSerializable(date)
writeInt(value)
}
// also
dest.also {
it.writeSerializable(date)
it.writeInt(value)
}
createFromParcel() では、レシーバーの Parcel ではなく Receipt を返したいので apply, also は使えません。ここでも選ぶとしたら it が省略できる with か run がよさそうです。
// let
override fun createFromParcel(source: Parcel): Receipt
= source.let { Receipt(it.readSerializable() as Date, it.readInt()) }
// with
override fun createFromParcel(source: Parcel): Receipt
= with(source) { Receipt(readSerializable() as Date, readInt()) }
// run
override fun createFromParcel(source: Parcel): Receipt
= source.run { Receipt(readSerializable() as Date, readInt()) }
65行が21行になった。
0 件のコメント:
コメントを投稿