Skip to content

Commit 73e03c1

Browse files
authored
Merge pull request #647 from AppDevNext/EntriesNonNullable
Entries are now non nullable
2 parents e3deb7c + ceafbc0 commit 73e03c1

13 files changed

Lines changed: 80 additions & 132 deletions

app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener {
148148
}
149149

150150
private fun createSet(): LineDataSet {
151-
val set = LineDataSet(null, "DataSet 1")
151+
val set = LineDataSet(label = "DataSet 1")
152152
set.lineWidth = 2.5f
153153
set.circleRadius = 4.5f
154154
set.color = Color.rgb(240, 99, 99)

app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener {
113113
}
114114

115115
private fun createSet(): LineDataSet {
116-
val set = LineDataSet(null, "Dynamic Data")
116+
val set = LineDataSet(label = "Dynamic Data")
117117
set.axisDependency = AxisDependency.LEFT
118118
set.color = ColorTemplate.holoBlue
119119
set.setCircleColor(Color.WHITE)

app/src/main/kotlin/info/appdev/chartexample/compose/HorizontalBarComposeActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ class HorizontalBarComposeActivity : DemoBaseCompose() {
354354
if (chartData != null && chartData.dataSetCount > 0) {
355355
set1 = chartData.getDataSetByIndex(0) as BarDataSet
356356
@Suppress("DEPRECATION")
357-
set1.values = values
357+
set1.entries = values
358358
chartData.notifyDataChanged()
359359
localChart.notifyDataSetChanged()
360360
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import info.appdev.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataS
77
/**
88
* Baseclass of all DataSets for Bar-, Line-, Scatter- and CandleStickChart.
99
*/
10-
abstract class BarLineScatterCandleBubbleDataSet<T : Entry>(yVals: MutableList<T>?, label: String) :
10+
abstract class BarLineScatterCandleBubbleDataSet<T : Entry>(yVals: MutableList<T>, label: String) :
1111
DataSet<T>(yVals, label), IBarLineScatterCandleBubbleDataSet<T> {
1212
/**
1313
* Sets the color that is used for drawing the highlight indicators.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package info.appdev.charting.data
33
import info.appdev.charting.interfaces.datasets.IBubbleDataSet
44
import info.appdev.charting.utils.convertDpToPixel
55

6-
open class BubbleDataSet(yVals: MutableList<BubbleEntry>?, label: String) : BarLineScatterCandleBubbleDataSet<BubbleEntry>(yVals, label), IBubbleDataSet {
6+
open class BubbleDataSet(yVals: MutableList<BubbleEntry>, label: String) : BarLineScatterCandleBubbleDataSet<BubbleEntry>(yVals, label), IBubbleDataSet {
77
protected var mMaxSize: Float = 0f
88
protected var mNormalizeSize: Boolean = true
99

@@ -21,8 +21,8 @@ open class BubbleDataSet(yVals: MutableList<BubbleEntry>?, label: String) : BarL
2121

2222
override fun copy(): DataSet<BubbleEntry> {
2323
val entries: MutableList<BubbleEntry> = ArrayList()
24-
for (i in mEntries!!.indices) {
25-
entries.add(mEntries!![i].copy())
24+
for (i in mEntries.indices) {
25+
entries.add(mEntries[i].copy())
2626
}
2727
val copied = BubbleDataSet(entries, label)
2828
copy(copied)

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import info.appdev.charting.utils.convertDpToPixel
99
/**
1010
* DataSet for the CandleStickChart.
1111
*/
12-
open class CandleDataSet(yVals: MutableList<CandleEntry>?, label: String = "") : LineScatterCandleRadarDataSet<CandleEntry>(yVals, label), ICandleDataSet {
12+
open class CandleDataSet(yVals: MutableList<CandleEntry>, label: String = "") : LineScatterCandleRadarDataSet<CandleEntry>(yVals, label), ICandleDataSet {
1313
/**
1414
* the width of the shadow of the candle
1515
*/
@@ -72,10 +72,8 @@ open class CandleDataSet(yVals: MutableList<CandleEntry>?, label: String = "") :
7272

7373
override fun copy(): DataSet<CandleEntry> {
7474
val entries: MutableList<CandleEntry> = mutableListOf()
75-
mEntries?.let {
76-
for (i in it.indices) {
77-
entries.add(it[i].copy())
78-
}
75+
for (i in mEntries.indices) {
76+
entries.add(mEntries[i].copy())
7977
}
8078

8179
val copied = CandleDataSet(entries, label)

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

Lines changed: 37 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import kotlin.math.abs
1111
* LineChart, or the values of a specific group of bars in the BarChart).
1212
*/
1313
abstract class DataSet<T : Entry>(
14-
protected var mEntries: MutableList<T>?,
14+
protected var mEntries: MutableList<T>,
1515
label: String = ""
1616
) : BaseDataSet<T>(label), Serializable {
1717
/**
@@ -38,17 +38,12 @@ abstract class DataSet<T : Entry>(
3838
override var xMin: Float = Float.MAX_VALUE
3939
protected set
4040

41-
4241
/**
4342
* Creates a new DataSet object with the given values (entries) it represents. Also, a
4443
* label that describes the DataSet can be specified. The label can also be
4544
* used to retrieve the DataSet from a ChartData object.
4645
*/
4746
init {
48-
if (mEntries == null) {
49-
mEntries = ArrayList<T>()
50-
}
51-
5247
calcMinMax()
5348
}
5449

@@ -58,7 +53,7 @@ abstract class DataSet<T : Entry>(
5853
this.xMax = -Float.MAX_VALUE
5954
this.xMin = Float.MAX_VALUE
6055

61-
if (mEntries == null || mEntries!!.isEmpty()) {
56+
if (mEntries.isEmpty()) {
6257
return
6358
}
6459

@@ -71,7 +66,7 @@ abstract class DataSet<T : Entry>(
7166
this.yMax = -Float.MAX_VALUE
7267
this.yMin = Float.MAX_VALUE
7368

74-
if (mEntries == null || mEntries!!.isEmpty()) {
69+
if (mEntries.isEmpty()) {
7570
return
7671
}
7772

@@ -85,7 +80,7 @@ abstract class DataSet<T : Entry>(
8580
for (i in indexFrom..indexTo) {
8681
// only recalculate y
8782

88-
calcMinMaxY(mEntries!![i])
83+
calcMinMaxY(mEntries[i])
8984
}
9085
}
9186

@@ -118,32 +113,13 @@ abstract class DataSet<T : Entry>(
118113
}
119114

120115
override val entryCount: Int
121-
get() = mEntries!!.size
122-
123-
@get:Deprecated("")
124-
@set:Deprecated("")
125-
var values: MutableList<T>?
126-
/**
127-
* This method is deprecated.
128-
* Use getEntries() instead.
129-
*/
130-
get() = mEntries
131-
/**
132-
* This method is deprecated.
133-
* Use setEntries(...) instead.
134-
*/
135-
set(values) {
136-
this.entries = values
137-
}
116+
get() = mEntries.size
138117

139-
var entries: MutableList<T>?
140-
/**
141-
* Returns the array of entries that this DataSet represents.
142-
*/
118+
/**
119+
* Returns the array of entries that this DataSet represents.
120+
*/
121+
var entries: MutableList<T>
143122
get() = mEntries
144-
/**
145-
* Sets the array of entries that this DataSet represents, and calls notifyDataSetChanged()
146-
*/
147123
set(entries) {
148124
mEntries = entries
149125
notifyDataSetChanged()
@@ -161,42 +137,35 @@ abstract class DataSet<T : Entry>(
161137
override fun toString(): String {
162138
val buffer = StringBuilder()
163139
buffer.append(toSimpleString())
164-
for (i in mEntries!!.indices) {
165-
buffer.append(mEntries!![i].toString()).append(" ")
140+
for (i in mEntries.indices) {
141+
buffer.append(mEntries[i].toString()).append(" ")
166142
}
167143
return buffer.toString()
168144
}
169145

170146
/**
171147
* Returns a simple string representation of the DataSet with the type and the number of Entries.
172148
*/
173-
fun toSimpleString() = "DataSet, label: $label, entries: ${mEntries!!.size}"
149+
fun toSimpleString() = "DataSet, label: $label, entries: ${mEntries.size}"
174150

175151
override fun addEntryOrdered(entry: T) {
176-
if (mEntries == null) {
177-
mEntries = ArrayList()
178-
}
179-
180152
calcMinMax(entry)
181153

182-
if (!mEntries!!.isEmpty() && mEntries!![mEntries!!.size - 1].x > entry.x) {
154+
if (!mEntries.isEmpty() && mEntries[mEntries.size - 1].x > entry.x) {
183155
val closestIndex = getEntryIndex(entry.x, entry.y, Rounding.UP)
184-
mEntries!!.add(closestIndex, entry)
156+
mEntries.add(closestIndex, entry)
185157
} else {
186-
mEntries!!.add(entry)
158+
mEntries.add(entry)
187159
}
188160
}
189161

190162
override fun clear() {
191-
mEntries!!.clear()
163+
mEntries.clear()
192164
notifyDataSetChanged()
193165
}
194166

195167
override fun addEntry(entry: T): Boolean {
196-
var values = this.entries
197-
if (values == null) {
198-
values = ArrayList()
199-
}
168+
val values = this.entries
200169

201170
calcMinMax(entry)
202171

@@ -205,11 +174,9 @@ abstract class DataSet<T : Entry>(
205174
}
206175

207176
override fun removeEntry(entry: T): Boolean {
208-
if (mEntries == null)
209-
return false
210177

211178
// remove the entry
212-
val removed = mEntries!!.remove(entry)
179+
val removed = mEntries.remove(entry)
213180

214181
if (removed) {
215182
calcMinMax()
@@ -220,14 +187,14 @@ abstract class DataSet<T : Entry>(
220187

221188
override fun getEntryIndex(entry: T): Int {
222189
// return getEntryIndex(entry)
223-
return mEntries!!.indexOf(entry)
190+
return mEntries.indexOf(entry)
224191
}
225192

226193

227194
override fun getEntryForXValue(xValue: Float, closestToY: Float, rounding: Rounding?): T? {
228195
val index = getEntryIndex(xValue, closestToY, rounding)
229196
if (index > -1) {
230-
return mEntries!![index]
197+
return mEntries[index]
231198
}
232199
return null
233200
}
@@ -240,28 +207,28 @@ abstract class DataSet<T : Entry>(
240207
if (index < 0) {
241208
Timber.e("index $index is < 0 for getEntryForIndex")
242209
return null
243-
} else if (index >= mEntries!!.size) {
244-
Timber.e("index " + index + "/" + mEntries!!.size + " is out of range for getEntryForIndex")
210+
} else if (index >= mEntries.size) {
211+
Timber.e("index $index / ${mEntries.size} is out of range for getEntryForIndex")
245212
return null
246213
}
247-
return mEntries!![index]
214+
return mEntries[index]
248215
}
249216

250217
override fun getEntryIndex(xValue: Float, closestToY: Float, rounding: Rounding?): Int {
251-
if (mEntries == null || mEntries!!.isEmpty()) {
218+
if (mEntries.isEmpty()) {
252219
return -1
253220
}
254221

255222
var low = 0
256-
var high = mEntries!!.size - 1
223+
var high = mEntries.size - 1
257224
var closest = high
258225

259226
while (low < high) {
260227
val m = low + (high - low) / 2
261228

262-
val currentEntry: Entry = mEntries!![m]
229+
val currentEntry: Entry = mEntries[m]
263230

264-
val nextEntry: Entry = mEntries!![m + 1]
231+
val nextEntry: Entry = mEntries[m + 1]
265232

266233
val d1 = currentEntry.x - xValue
267234
val d2 = nextEntry.x - xValue
@@ -291,11 +258,11 @@ abstract class DataSet<T : Entry>(
291258
closest = high
292259
}
293260

294-
val closestEntry: Entry = mEntries!![closest]
261+
val closestEntry: Entry = mEntries[closest]
295262
val closestXValue = closestEntry.x
296263
if (rounding == Rounding.UP) {
297264
// If rounding up, and found x-value is lower than specified x, and we can go upper...
298-
if (closestXValue < xValue && closest < mEntries!!.size - 1) {
265+
if (closestXValue < xValue && closest < mEntries.size - 1) {
299266
++closest
300267
}
301268
} else if (rounding == Rounding.DOWN) {
@@ -307,7 +274,7 @@ abstract class DataSet<T : Entry>(
307274

308275
// Search by closest to y-value
309276
if (!closestToY.isNaN()) {
310-
while (closest > 0 && mEntries!![closest - 1].x == closestXValue) {
277+
while (closest > 0 && mEntries[closest - 1].x == closestXValue) {
311278
closest -= 1
312279
}
313280

@@ -316,11 +283,11 @@ abstract class DataSet<T : Entry>(
316283

317284
while (true) {
318285
closest += 1
319-
if (closest >= mEntries!!.size) {
286+
if (closest >= mEntries.size) {
320287
break
321288
}
322289

323-
val value: T = mEntries!![closest]
290+
val value: T = mEntries[closest]
324291

325292
if (value.x != closestXValue) {
326293
break
@@ -341,23 +308,23 @@ abstract class DataSet<T : Entry>(
341308
val entries: MutableList<T> = mutableListOf()
342309

343310
var low = 0
344-
var high = mEntries!!.size - 1
311+
var high = mEntries.size - 1
345312

346313
while (low <= high) {
347314
var m = (high + low) / 2
348-
var entry = mEntries!![m]
315+
var entry = mEntries[m]
349316

350317
// if we have a match
351318
if (xValue == entry.x) {
352-
while (m > 0 && mEntries!![m - 1].x == xValue) {
319+
while (m > 0 && mEntries[m - 1].x == xValue) {
353320
m--
354321
}
355322

356-
high = mEntries!!.size
323+
high = mEntries.size
357324

358325
// loop over all "equal" entries
359326
while (m < high) {
360-
entry = mEntries!![m]
327+
entry = mEntries[m]
361328
if (entry.x == xValue) {
362329
entries.add(entry)
363330
} else {

0 commit comments

Comments
 (0)