Skip to content

Commit f7a7a84

Browse files
committed
BaseEntry generic
to be able to use it as Float or Double
1 parent f5c0c17 commit f7a7a84

File tree

4 files changed

+159
-9
lines changed

4 files changed

+159
-9
lines changed

chartLib/src/main/kotlin/info/appdev/charting/data/BaseEntry.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package info.appdev.charting.data
22

33
import android.graphics.drawable.Drawable
44

5-
abstract class BaseEntry {
5+
abstract class BaseEntry<T> where T : Number, T : Comparable<T> {
66

7-
protected var yBase: Float = 0f
8-
open var y: Float
7+
protected lateinit var yBase: T
8+
open var y: T
99
get() = yBase
1010
set(value) {
1111
yBase = value
@@ -17,19 +17,19 @@ abstract class BaseEntry {
1717

1818
constructor()
1919

20-
constructor(y: Float) {
20+
constructor(y: T) {
2121
this.yBase = y
2222
}
2323

24-
constructor(y: Float, data: Any?) : this(y) {
24+
constructor(y: T, data: Any?) : this(y) {
2525
this.data = data
2626
}
2727

28-
constructor(y: Float, icon: Drawable?) : this(y) {
28+
constructor(y: T, icon: Drawable?) : this(y) {
2929
this.icon = icon
3030
}
3131

32-
constructor(y: Float, icon: Drawable?, data: Any?) : this(y) {
32+
constructor(y: T, icon: Drawable?, data: Any?) : this(y) {
3333
this.icon = icon
3434
this.data = data
3535
}

chartLib/src/main/kotlin/info/appdev/charting/data/Entry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import kotlin.math.abs
1313
* Class representing one entry in the chart. Might contain multiple values.
1414
* Might only contain a single value depending on the used constructor.
1515
*/
16-
open class Entry : BaseEntry, Parcelable, Serializable {
16+
open class Entry : BaseEntry<Float>, Parcelable, Serializable {
1717

1818
private var _x: Float = 0f
1919
open var x: Float
Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,151 @@
11
package info.appdev.charting.data
22

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+
}

chartLib/src/main/kotlin/info/appdev/charting/utils/Utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.view.VelocityTracker
77
import android.view.ViewConfiguration
88
import info.appdev.charting.formatter.DefaultValueFormatter
99
import info.appdev.charting.formatter.IValueFormatter
10+
import java.lang.Float.intBitsToFloat
1011
import kotlin.math.abs
1112
import kotlin.math.cos
1213
import kotlin.math.sin
@@ -23,6 +24,7 @@ object Utils {
2324
const val FDEG2RAD: Float = (Math.PI.toFloat() / 180f)
2425

2526
val FLOAT_EPSILON: Float = java.lang.Float.intBitsToFloat(1)
27+
val DOUBLE_EPSILON: Double = java.lang.Double.longBitsToDouble(1L)
2628

2729
/**
2830
* initialize method, called inside the Chart.init() method.

0 commit comments

Comments
 (0)