- 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];
- }
- };
- }
- 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)
- }
- }
- }
- }

- override fun newArray(size: Int): Array<receipt?> {
- return arrayOfNulls(size)
- }
- 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)
- }
- }
- }
- }
- 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)
- }
- }
- }
紛らわしいので 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)
- }
- }
- }
- 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)
- }
- }
- }
他のスコープ関数(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)
- }
ここでも選ぶとしたら 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()) }
0 件のコメント:
コメントを投稿