Skip to content

Commit f9c6b2b

Browse files
Add infix extension functions for Property condition methods.
1 parent fd9bae0 commit f9c6b2b

File tree

2 files changed

+177
-15
lines changed
  • objectbox-kotlin/src/main/kotlin/io/objectbox/kotlin
  • tests/objectbox-java-test/src/test/java/io/objectbox/query

2 files changed

+177
-15
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright 2020 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
@file:Suppress("unused") // Public API.
18+
19+
package io.objectbox.kotlin
20+
21+
import io.objectbox.Property
22+
import io.objectbox.query.PropertyQueryCondition
23+
import java.util.*
24+
25+
26+
// Boolean
27+
/** Creates an "equal ('=')" condition for this property. */
28+
infix fun <T> Property<T>.equal(value: Boolean): PropertyQueryCondition<T> {
29+
return equal(value)
30+
}
31+
32+
/** Creates a "not equal ('&lt;&gt;')" condition for this property. */
33+
infix fun <T> Property<T>.notEqual(value: Boolean): PropertyQueryCondition<T> {
34+
return notEqual(value)
35+
}
36+
37+
// IntArray
38+
/** Creates an "IN (..., ..., ...)" condition for this property. */
39+
infix fun <T> Property<T>.oneOf(value: IntArray): PropertyQueryCondition<T> {
40+
return oneOf(value)
41+
}
42+
43+
/** Creates a "NOT IN (..., ..., ...)" condition for this property. */
44+
infix fun <T> Property<T>.notOneOf(value: IntArray): PropertyQueryCondition<T> {
45+
return notOneOf(value)
46+
}
47+
48+
// Long
49+
/** Creates an "equal ('=')" condition for this property. */
50+
infix fun <T> Property<T>.equal(value: Long): PropertyQueryCondition<T> {
51+
return equal(value)
52+
}
53+
54+
/** Creates a "not equal ('&lt;&gt;')" condition for this property. */
55+
infix fun <T> Property<T>.notEqual(value: Long): PropertyQueryCondition<T> {
56+
return notEqual(value)
57+
}
58+
59+
/** Creates a "greater than ('&gt;')" condition for this property. */
60+
infix fun <T> Property<T>.greater(value: Long): PropertyQueryCondition<T> {
61+
return greater(value)
62+
}
63+
64+
/** Creates a "less than ('&lt;')" condition for this property. */
65+
infix fun <T> Property<T>.less(value: Long): PropertyQueryCondition<T> {
66+
return less(value)
67+
}
68+
69+
// LongArray
70+
/** Creates an "IN (..., ..., ...)" condition for this property. */
71+
infix fun <T> Property<T>.oneOf(value: LongArray): PropertyQueryCondition<T> {
72+
return oneOf(value)
73+
}
74+
75+
/** Creates a "NOT IN (..., ..., ...)" condition for this property. */
76+
infix fun <T> Property<T>.notOneOf(value: LongArray): PropertyQueryCondition<T> {
77+
return notOneOf(value)
78+
}
79+
80+
// Double
81+
/** Creates a "greater than ('&gt;')" condition for this property. */
82+
infix fun <T> Property<T>.greater(value: Double): PropertyQueryCondition<T> {
83+
return greater(value)
84+
}
85+
86+
/** Creates a "less than ('&lt;')" condition for this property. */
87+
infix fun <T> Property<T>.less(value: Double): PropertyQueryCondition<T> {
88+
return less(value)
89+
}
90+
91+
// Date
92+
/** Creates an "equal ('=')" condition for this property. */
93+
infix fun <T> Property<T>.equal(value: Date): PropertyQueryCondition<T> {
94+
return equal(value)
95+
}
96+
97+
/** Creates a "not equal ('&lt;&gt;')" condition for this property. */
98+
infix fun <T> Property<T>.notEqual(value: Date): PropertyQueryCondition<T> {
99+
return notEqual(value)
100+
}
101+
102+
/** Creates a "greater than ('&gt;')" condition for this property. */
103+
infix fun <T> Property<T>.greater(value: Date): PropertyQueryCondition<T> {
104+
return greater(value)
105+
}
106+
107+
/** Creates a "less than ('&lt;')" condition for this property. */
108+
infix fun <T> Property<T>.less(value: Date): PropertyQueryCondition<T> {
109+
return less(value)
110+
}
111+
112+
// String
113+
/** Creates an "equal ('=')" condition for this property. */
114+
infix fun <T> Property<T>.equal(value: String): PropertyQueryCondition<T> {
115+
return equal(value)
116+
}
117+
118+
/** Creates a "not equal ('&lt;&gt;')" condition for this property. */
119+
infix fun <T> Property<T>.notEqual(value: String): PropertyQueryCondition<T> {
120+
return notEqual(value)
121+
}
122+
123+
/** Creates a "greater than ('&gt;')" condition for this property. */
124+
infix fun <T> Property<T>.greater(value: String): PropertyQueryCondition<T> {
125+
return greater(value)
126+
}
127+
128+
/** Creates a "less than ('&lt;')" condition for this property. */
129+
infix fun <T> Property<T>.less(value: String): PropertyQueryCondition<T> {
130+
return less(value)
131+
}
132+
133+
infix fun <T> Property<T>.contains(value: String): PropertyQueryCondition<T> {
134+
return contains(value)
135+
}
136+
137+
infix fun <T> Property<T>.startsWith(value: String): PropertyQueryCondition<T> {
138+
return startsWith(value)
139+
}
140+
141+
infix fun <T> Property<T>.endsWith(value: String): PropertyQueryCondition<T> {
142+
return endsWith(value)
143+
}
144+
145+
// Array<String>
146+
/** Creates an "IN (..., ..., ...)" condition for this property. */
147+
infix fun <T> Property<T>.oneOf(value: Array<String>): PropertyQueryCondition<T> {
148+
return oneOf(value)
149+
}
150+
151+
// ByteArray
152+
/** Creates an "equal ('=')" condition for this property. */
153+
infix fun <T> Property<T>.equal(value: ByteArray): PropertyQueryCondition<T> {
154+
return equal(value)
155+
}
156+
157+
/** Creates a "greater than ('&gt;')" condition for this property. */
158+
infix fun <T> Property<T>.greater(value: ByteArray): PropertyQueryCondition<T> {
159+
return greater(value)
160+
}
161+
162+
/** Creates a "less than ('&lt;')" condition for this property. */
163+
infix fun <T> Property<T>.less(value: ByteArray): PropertyQueryCondition<T> {
164+
return less(value)
165+
}

tests/objectbox-java-test/src/test/java/io/objectbox/query/QueryTestK.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package io.objectbox.query
22

33
import io.objectbox.TestEntity_
4-
import io.objectbox.kotlin.and
5-
import io.objectbox.kotlin.inValues
6-
import io.objectbox.kotlin.or
7-
import io.objectbox.kotlin.query
4+
import io.objectbox.kotlin.*
85
import org.junit.Assert.assertEquals
96
import org.junit.Test
107

@@ -36,8 +33,8 @@ class QueryTestK : AbstractQueryTest() {
3633

3734
// suggested query API
3835
val newQuery = box.query(
39-
(TestEntity_.simpleInt.less(12) or TestEntity_.simpleLong.oneOf(longArrayOf(1012)))
40-
and TestEntity_.simpleString.equal("Fry")
36+
(TestEntity_.simpleInt less 12 or (TestEntity_.simpleLong oneOf longArrayOf(1012)))
37+
and (TestEntity_.simpleString equal "Fry")
4138
).order(TestEntity_.simpleInt).build()
4239
val resultsNew = newQuery.find()
4340
assertEquals(2, resultsNew.size)
@@ -46,15 +43,15 @@ class QueryTestK : AbstractQueryTest() {
4643

4744
val newQueryOr = box.query(
4845
// (EQ OR EQ) AND LESS
49-
(TestEntity_.simpleString.equal("Fry") or TestEntity_.simpleString.equal("Sarah"))
50-
and TestEntity_.simpleInt.less(12)
46+
(TestEntity_.simpleString equal "Fry" or (TestEntity_.simpleString equal "Sarah"))
47+
and (TestEntity_.simpleInt less 12)
5148
).build().find()
5249
assertEquals(1, newQueryOr.size) // only the Fry age 10
5350

5451
val newQueryAnd = box.query(
5552
// EQ OR (EQ AND LESS)
56-
TestEntity_.simpleString.equal("Fry") or
57-
(TestEntity_.simpleString.equal("Sarah") and TestEntity_.simpleInt.less(12))
53+
TestEntity_.simpleString equal "Fry" or
54+
(TestEntity_.simpleString equal "Sarah" and (TestEntity_.simpleInt less 12))
5855
).build().find()
5956
assertEquals(3, newQueryAnd.size) // all Fry's
6057
}
@@ -63,8 +60,8 @@ class QueryTestK : AbstractQueryTest() {
6360
fun intLessAndGreater() {
6461
putTestEntitiesScalars()
6562
val query = box.query(
66-
TestEntity_.simpleInt.greater(2003)
67-
and TestEntity_.simpleShort.less(2107)
63+
TestEntity_.simpleInt greater 2003
64+
and (TestEntity_.simpleShort less 2107)
6865
).build()
6966
assertEquals(3, query.count())
7067
}
@@ -84,7 +81,7 @@ class QueryTestK : AbstractQueryTest() {
8481

8582
val valuesInt = intArrayOf(1, 1, 2, 3, 2003, 2007, 2002, -1)
8683
val query = box.query(
87-
TestEntity_.simpleInt.oneOf(valuesInt).alias("int")
84+
(TestEntity_.simpleInt oneOf(valuesInt)).alias("int")
8885
).build()
8986
assertEquals(3, query.count())
9087

@@ -102,7 +99,7 @@ class QueryTestK : AbstractQueryTest() {
10299
fun or() {
103100
putTestEntitiesScalars()
104101
val query = box.query(
105-
TestEntity_.simpleInt.equal(2007) or TestEntity_.simpleLong.equal(3002)
102+
TestEntity_.simpleInt equal 2007 or (TestEntity_.simpleLong equal 3002)
106103
).build()
107104
val entities = query.find()
108105
assertEquals(2, entities.size.toLong())
@@ -115,7 +112,7 @@ class QueryTestK : AbstractQueryTest() {
115112
putTestEntitiesScalars()
116113
// Result if OR precedence (wrong): {}, AND precedence (expected): {2008}
117114
val query = box.query(
118-
TestEntity_.simpleInt.equal(2006) and TestEntity_.simpleInt.equal(2007) or TestEntity_.simpleInt.equal(2008)
115+
TestEntity_.simpleInt equal 2006 and (TestEntity_.simpleInt equal 2007) or (TestEntity_.simpleInt equal 2008)
119116
).build()
120117
val entities = query.find()
121118
assertEquals(1, entities.size.toLong())

0 commit comments

Comments
 (0)