|
1 | 1 | package info.appdev.charting.data |
2 | 2 |
|
3 | | -class EntryDouble(val xDouble: Double, y: Float) : Entry(xDouble.toFloat(), y) |
| 3 | +import android.graphics.drawable.Drawable |
| 4 | +import android.os.Build |
| 5 | +import android.os.Parcel |
| 6 | +import android.os.ParcelFormatException |
| 7 | +import android.os.Parcelable |
| 8 | +import info.appdev.charting.utils.Utils |
| 9 | +import java.io.Serializable |
| 10 | +import kotlin.math.abs |
| 11 | + |
| 12 | +open class EntryDouble : BaseEntry<Double>, Parcelable, Serializable { |
| 13 | + |
| 14 | + private var _x: Double = 0.0 |
| 15 | + open var x: Double |
| 16 | + get() = _x |
| 17 | + set(value) { |
| 18 | + _x = value |
| 19 | + } |
| 20 | + |
| 21 | + constructor() |
| 22 | + |
| 23 | + /** |
| 24 | + * A Entry represents one single entry in the chart. |
| 25 | + * |
| 26 | + * @param x the x value |
| 27 | + * @param y the y value (the actual value of the entry) |
| 28 | + */ |
| 29 | + constructor(x: Double, y: Double) : super(y) { |
| 30 | + this._x = x |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * A Entry represents one single entry in the chart. |
| 35 | + * |
| 36 | + * @param x the x value |
| 37 | + * @param y the y value (the actual value of the entry) |
| 38 | + * @param data Spot for additional data this Entry represents. |
| 39 | + */ |
| 40 | + constructor(x: Double, y: Double, data: Any?) : super(y, data) { |
| 41 | + this._x = x |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * A Entry represents one single entry in the chart. |
| 46 | + * |
| 47 | + * @param x the x value |
| 48 | + * @param y the y value (the actual value of the entry) |
| 49 | + * @param icon icon image |
| 50 | + */ |
| 51 | + constructor(x: Double, y: Double, icon: Drawable?) : super(y, icon) { |
| 52 | + this._x = x |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * A Entry represents one single entry in the chart. |
| 57 | + * |
| 58 | + * @param x the x value |
| 59 | + * @param y the y value (the actual value of the entry) |
| 60 | + * @param icon icon image |
| 61 | + * @param data Spot for additional data this Entry represents. |
| 62 | + */ |
| 63 | + constructor(x: Double, y: Double, icon: Drawable?, data: Any?) : super(y, icon, data) { |
| 64 | + this._x = x |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * returns an exact copy of the entry |
| 69 | + */ |
| 70 | + open fun copy(): EntryDouble { |
| 71 | + val e = EntryDouble(x, y, data) |
| 72 | + return e |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Compares value, xIndex and data of the entries. Returns true if entries |
| 77 | + * are equal in those points, false if not. Does not check by hash-code like |
| 78 | + * it's done by the "equals" method. |
| 79 | + */ |
| 80 | + fun equalTo(e: EntryDouble?): Boolean { |
| 81 | + if (e == null) |
| 82 | + return false |
| 83 | + |
| 84 | + if (e.data !== this.data) |
| 85 | + return false |
| 86 | + |
| 87 | + if (abs((e.x - this.x)) > Utils.DOUBLE_EPSILON) |
| 88 | + return false |
| 89 | + |
| 90 | + if (abs((e.y - this.y)) > Utils.DOUBLE_EPSILON) |
| 91 | + return false |
| 92 | + |
| 93 | + return true |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * returns a string representation of the entry containing x-index and value |
| 98 | + */ |
| 99 | + override fun toString(): String { |
| 100 | + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 101 | + "${this.javaClass.typeName.substringAfterLast(".")} x=$x y=$y" |
| 102 | + } else { |
| 103 | + "EntryDouble x=$x y=$y" |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + override fun describeContents(): Int { |
| 108 | + return 0 |
| 109 | + } |
| 110 | + |
| 111 | + override fun writeToParcel(dest: Parcel, flags: Int) { |
| 112 | + dest.writeDouble(this.x) |
| 113 | + dest.writeDouble(this.y) |
| 114 | + if (data != null) { |
| 115 | + if (data is Parcelable) { |
| 116 | + dest.writeInt(1) |
| 117 | + dest.writeParcelable(data as Parcelable?, flags) |
| 118 | + } else { |
| 119 | + throw ParcelFormatException("Cannot parcel an EntryDouble with non-parcelable data") |
| 120 | + } |
| 121 | + } else { |
| 122 | + dest.writeInt(0) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + protected constructor(`in`: Parcel) { |
| 127 | + this._x = `in`.readDouble() |
| 128 | + this.yBase = `in`.readDouble() |
| 129 | + if (`in`.readInt() == 1) { |
| 130 | + this.data = if (Build.VERSION.SDK_INT >= 33) { |
| 131 | + `in`.readParcelable(Any::class.java.classLoader, Any::class.java) |
| 132 | + } else { |
| 133 | + @Suppress("DEPRECATION") |
| 134 | + `in`.readParcelable(Any::class.java.classLoader) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + companion object { |
| 140 | + @JvmField |
| 141 | + val CREATOR: Parcelable.Creator<EntryDouble> = object : Parcelable.Creator<EntryDouble> { |
| 142 | + override fun createFromParcel(source: Parcel): EntryDouble { |
| 143 | + return EntryDouble(source) |
| 144 | + } |
| 145 | + |
| 146 | + override fun newArray(size: Int): Array<EntryDouble?> { |
| 147 | + return arrayOfNulls(size) |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments