File tree Expand file tree Collapse file tree 2 files changed +62
-1
lines changed
src/main/java/com/lightningkite/kotlin/collection Expand file tree Collapse file tree 2 files changed +62
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,6 @@ import java.util.*
10
10
inline fun <E > List<E>.random (): E {
11
11
return this [Math .random().times(size).toInt()]
12
12
}
13
-
14
13
fun <E > SetupList (setup : (E ) -> Unit ): MutableList <E > {
15
14
return object : ArrayList <E >() {
16
15
override fun addAll (index : Int , elements : Collection <E >): Boolean {
Original file line number Diff line number Diff line change
1
+ package com.lightningkite.kotlin.collection
2
+
3
+ import java.util.*
4
+
5
+ /* *
6
+ * Created by joseph on 12/2/16.
7
+ */
8
+ abstract class ObserveEmptyArrayList <E >() : ArrayList<E>() {
9
+
10
+ abstract fun onNotEmpty (): Unit
11
+ abstract fun onEmpty (): Unit
12
+
13
+ var active = false
14
+ private fun checkUp () {
15
+ if (! super .isEmpty() && ! active) {
16
+ active = true
17
+ onNotEmpty()
18
+ }
19
+ }
20
+
21
+ private fun checkDown () {
22
+ if (super .isEmpty() && active) {
23
+ active = false
24
+ onEmpty()
25
+ }
26
+ }
27
+
28
+ override fun add (element : E ): Boolean {
29
+ val result = super .add(element)
30
+ checkUp()
31
+ return result
32
+ }
33
+
34
+ override fun addAll (elements : Collection <E >): Boolean {
35
+ val result = super .addAll(elements)
36
+ checkUp()
37
+ return result
38
+ }
39
+
40
+ override fun clear () {
41
+ super .clear()
42
+ checkDown()
43
+ }
44
+
45
+ override fun remove (element : E ): Boolean {
46
+ val result = super .remove(element)
47
+ checkDown()
48
+ return result
49
+ }
50
+
51
+ override fun removeAll (elements : Collection <E >): Boolean {
52
+ val result = super .removeAll(elements)
53
+ checkDown()
54
+ return result
55
+ }
56
+
57
+ override fun retainAll (elements : Collection <E >): Boolean {
58
+ val result = super .retainAll(elements)
59
+ checkDown()
60
+ return result
61
+ }
62
+ }
You can’t perform that action at this time.
0 commit comments