Skip to content

Commit f898677

Browse files
committed
refactor: remove duplicated code paths
1 parent c299b2f commit f898677

22 files changed

Lines changed: 215 additions & 488 deletions

posthog-android/src/main/java/com/posthog/android/internal/PostHogAndroidUtils.kt

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,28 @@ public fun Bitmap.isValid(): Boolean {
214214
height > 0
215215
}
216216

217+
private fun webpLossyFormat(): Bitmap.CompressFormat =
218+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
219+
Bitmap.CompressFormat.WEBP_LOSSY
220+
} else {
221+
Bitmap.CompressFormat.WEBP
222+
}
223+
224+
private fun webpLosslessFormat(): Bitmap.CompressFormat =
225+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
226+
Bitmap.CompressFormat.WEBP_LOSSLESS
227+
} else {
228+
Bitmap.CompressFormat.WEBP
229+
}
230+
217231
@PostHogInternal
218232
@Suppress("DEPRECATION")
219233
public fun Bitmap.webpBase64(quality: Int = 30): String? {
220234
if (!isValid()) {
221235
return null
222236
}
223-
val format =
224-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
225-
Bitmap.CompressFormat.WEBP_LOSSY
226-
} else {
227-
Bitmap.CompressFormat.WEBP
228-
}
229237

230-
return base64(format, quality)
238+
return base64(webpLossyFormat(), quality)
231239
}
232240

233241
@PostHogInternal
@@ -240,19 +248,8 @@ public fun Bitmap.base64(
240248
return null
241249
}
242250

243-
val lossyFormat =
244-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
245-
Bitmap.CompressFormat.WEBP_LOSSY
246-
} else {
247-
Bitmap.CompressFormat.WEBP
248-
}
249-
250-
val losslessFormat =
251-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
252-
Bitmap.CompressFormat.WEBP_LOSSLESS
253-
} else {
254-
Bitmap.CompressFormat.WEBP
255-
}
251+
val lossyFormat = webpLossyFormat()
252+
val losslessFormat = webpLosslessFormat()
256253

257254
val htmlFormat =
258255
when (format) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.posthog.server
2+
3+
internal fun <K, V> MutableMap<K, V>?.putBuilderValue(
4+
key: K,
5+
value: V,
6+
): MutableMap<K, V> = (this ?: mutableMapOf()).apply { put(key, value) }
7+
8+
internal fun <K, V> MutableMap<K, V>?.putBuilderValues(values: Map<K, V>): MutableMap<K, V> =
9+
(this ?: mutableMapOf()).apply { putAll(values) }
10+
11+
internal fun <T> MutableList<T>?.addBuilderValues(values: List<T>): MutableList<T> = (this ?: mutableListOf()).apply { addAll(values) }
12+
13+
internal fun MutableMap<String, MutableMap<String, Any?>>?.putBuilderGroupProperty(
14+
group: String,
15+
key: String,
16+
value: Any?,
17+
): MutableMap<String, MutableMap<String, Any?>> =
18+
(this ?: mutableMapOf()).apply {
19+
getOrPut(group) { mutableMapOf() }[key] = value
20+
}
21+
22+
internal fun MutableMap<String, MutableMap<String, Any?>>?.putBuilderGroupProperties(
23+
groupProperties: Map<String, Map<String, Any?>>,
24+
): MutableMap<String, MutableMap<String, Any?>> =
25+
(this ?: mutableMapOf()).apply {
26+
groupProperties.forEach { (group, properties) ->
27+
getOrPut(group) { mutableMapOf() }.putAll(properties)
28+
}
29+
}

posthog-server/src/main/java/com/posthog/server/PostHogCaptureOptions.kt

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ public class PostHogCaptureOptions private constructor(
6464
key: String,
6565
value: Any,
6666
): Builder {
67-
properties =
68-
(properties ?: mutableMapOf()).apply {
69-
put(key, value)
70-
}
67+
properties = properties.putBuilderValue(key, value)
7168
return this
7269
}
7370

@@ -78,10 +75,7 @@ public class PostHogCaptureOptions private constructor(
7875
* @return This builder.
7976
*/
8077
public fun properties(properties: Map<String, Any>): Builder {
81-
this.properties =
82-
(this.properties ?: mutableMapOf()).apply {
83-
putAll(properties)
84-
}
78+
this.properties = this.properties.putBuilderValues(properties)
8579
return this
8680
}
8781

@@ -97,10 +91,7 @@ public class PostHogCaptureOptions private constructor(
9791
key: String,
9892
value: Any,
9993
): Builder {
100-
this.userProperties =
101-
(this.userProperties ?: mutableMapOf()).apply {
102-
put(key, value)
103-
}
94+
this.userProperties = this.userProperties.putBuilderValue(key, value)
10495
return this
10596
}
10697

@@ -112,10 +103,7 @@ public class PostHogCaptureOptions private constructor(
112103
* @see <a href="https://posthog.com/docs/product-analytics/user-properties">Documentation: User Properties</a>
113104
*/
114105
public fun userProperties(userProperties: Map<String, Any>): Builder {
115-
this.userProperties =
116-
(this.userProperties ?: mutableMapOf()).apply {
117-
putAll(userProperties)
118-
}
106+
this.userProperties = this.userProperties.putBuilderValues(userProperties)
119107
return this
120108
}
121109

@@ -131,10 +119,7 @@ public class PostHogCaptureOptions private constructor(
131119
key: String,
132120
value: Any,
133121
): Builder {
134-
this.userPropertiesSetOnce =
135-
(this.userPropertiesSetOnce ?: mutableMapOf()).apply {
136-
put(key, value)
137-
}
122+
this.userPropertiesSetOnce = this.userPropertiesSetOnce.putBuilderValue(key, value)
138123
return this
139124
}
140125

@@ -146,10 +131,7 @@ public class PostHogCaptureOptions private constructor(
146131
* @see <a href="https://posthog.com/docs/product-analytics/user-properties">Documentation: User Properties</a>
147132
*/
148133
public fun userPropertiesSetOnce(userPropertiesSetOnce: Map<String, Any>): Builder {
149-
this.userPropertiesSetOnce =
150-
(this.userPropertiesSetOnce ?: mutableMapOf()).apply {
151-
putAll(userPropertiesSetOnce)
152-
}
134+
this.userPropertiesSetOnce = this.userPropertiesSetOnce.putBuilderValues(userPropertiesSetOnce)
153135
return this
154136
}
155137

@@ -165,10 +147,7 @@ public class PostHogCaptureOptions private constructor(
165147
type: String,
166148
key: String,
167149
): Builder {
168-
this.groups =
169-
(this.groups ?: mutableMapOf()).apply {
170-
put(type, key)
171-
}
150+
this.groups = this.groups.putBuilderValue(type, key)
172151
return this
173152
}
174153

@@ -180,10 +159,7 @@ public class PostHogCaptureOptions private constructor(
180159
* @see <a href="https://posthog.com/docs/product-analytics/group-analytics">Documentation: Group Analytics</a>
181160
*/
182161
public fun groups(groups: Map<String, String>): Builder {
183-
this.groups =
184-
(this.groups ?: mutableMapOf()).apply {
185-
putAll(groups)
186-
}
162+
this.groups = this.groups.putBuilderValues(groups)
187163
return this
188164
}
189165

posthog-server/src/main/java/com/posthog/server/PostHogEvaluateFlagsOptions.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class PostHogEvaluateFlagsOptions private constructor(
5353
type: String,
5454
key: String,
5555
): Builder {
56-
this.groups = (groups ?: mutableMapOf()).apply { put(type, key) }
56+
this.groups = groups.putBuilderValue(type, key)
5757
return this
5858
}
5959

@@ -64,7 +64,7 @@ public class PostHogEvaluateFlagsOptions private constructor(
6464
* @return This builder.
6565
*/
6666
public fun groups(groups: Map<String, String>): Builder {
67-
this.groups = (this.groups ?: mutableMapOf()).apply { putAll(groups) }
67+
this.groups = this.groups.putBuilderValues(groups)
6868
return this
6969
}
7070

@@ -79,7 +79,7 @@ public class PostHogEvaluateFlagsOptions private constructor(
7979
key: String,
8080
value: Any?,
8181
): Builder {
82-
this.personProperties = (personProperties ?: mutableMapOf()).apply { put(key, value) }
82+
this.personProperties = personProperties.putBuilderValue(key, value)
8383
return this
8484
}
8585

@@ -90,7 +90,7 @@ public class PostHogEvaluateFlagsOptions private constructor(
9090
* @return This builder.
9191
*/
9292
public fun personProperties(personProperties: Map<String, Any?>): Builder {
93-
this.personProperties = (this.personProperties ?: mutableMapOf()).apply { putAll(personProperties) }
93+
this.personProperties = this.personProperties.putBuilderValues(personProperties)
9494
return this
9595
}
9696

@@ -107,9 +107,8 @@ public class PostHogEvaluateFlagsOptions private constructor(
107107
key: String,
108108
value: Any?,
109109
): Builder {
110-
val existing = groupProperties?.get(type)?.toMutableMap() ?: mutableMapOf()
111-
existing[key] = value
112-
this.groupProperties = (groupProperties ?: mutableMapOf()).apply { put(type, existing) }
110+
val existing = groupProperties?.get(type)?.toMutableMap().putBuilderValue(key, value)
111+
this.groupProperties = groupProperties.putBuilderValue(type, existing)
113112
return this
114113
}
115114

@@ -120,7 +119,7 @@ public class PostHogEvaluateFlagsOptions private constructor(
120119
* @return This builder.
121120
*/
122121
public fun groupProperties(groupProperties: Map<String, Map<String, Any?>>): Builder {
123-
this.groupProperties = (this.groupProperties ?: mutableMapOf()).apply { putAll(groupProperties) }
122+
this.groupProperties = this.groupProperties.putBuilderValues(groupProperties)
124123
return this
125124
}
126125

@@ -134,7 +133,7 @@ public class PostHogEvaluateFlagsOptions private constructor(
134133
* @return This builder.
135134
*/
136135
public fun flagKeys(flagKeys: List<String>): Builder {
137-
this.flagKeys = (this.flagKeys ?: mutableListOf()).apply { addAll(flagKeys) }
136+
this.flagKeys = this.flagKeys.addBuilderValues(flagKeys)
138137
return this
139138
}
140139

posthog-server/src/main/java/com/posthog/server/PostHogFeatureFlagOptions.kt

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ public class PostHogFeatureFlagOptions private constructor(
5555
key: String,
5656
propValue: String,
5757
): Builder {
58-
groups =
59-
(groups ?: mutableMapOf()).apply {
60-
put(key, propValue)
61-
}
58+
groups = groups.putBuilderValue(key, propValue)
6259
return this
6360
}
6461

@@ -69,10 +66,7 @@ public class PostHogFeatureFlagOptions private constructor(
6966
* @return This builder.
7067
*/
7168
public fun groups(groups: Map<String, String>): Builder {
72-
this.groups =
73-
(this.groups ?: mutableMapOf()).apply {
74-
putAll(groups)
75-
}
69+
this.groups = this.groups.putBuilderValues(groups)
7670
return this
7771
}
7872

@@ -88,10 +82,7 @@ public class PostHogFeatureFlagOptions private constructor(
8882
key: String,
8983
propValue: Any?,
9084
): Builder {
91-
personProperties =
92-
(personProperties ?: mutableMapOf()).apply {
93-
put(key, propValue)
94-
}
85+
personProperties = personProperties.putBuilderValue(key, propValue)
9586
return this
9687
}
9788

@@ -103,10 +94,7 @@ public class PostHogFeatureFlagOptions private constructor(
10394
* @see <a href="https://posthog.com/docs/product-analytics/user-properties">Documentation: User Properties</a>
10495
*/
10596
public fun personProperties(userProperties: Map<String, Any?>): Builder {
106-
this.personProperties =
107-
(this.personProperties ?: mutableMapOf()).apply {
108-
putAll(userProperties)
109-
}
97+
this.personProperties = this.personProperties.putBuilderValues(userProperties)
11098
return this
11199
}
112100

@@ -123,10 +111,7 @@ public class PostHogFeatureFlagOptions private constructor(
123111
key: String,
124112
propValue: Any?,
125113
): Builder {
126-
groupProperties =
127-
(groupProperties ?: mutableMapOf()).apply {
128-
getOrPut(group) { mutableMapOf() }[key] = propValue
129-
}
114+
groupProperties = groupProperties.putBuilderGroupProperty(group, key, propValue)
130115
return this
131116
}
132117

@@ -137,12 +122,7 @@ public class PostHogFeatureFlagOptions private constructor(
137122
* @return This builder.
138123
*/
139124
public fun groupProperties(groupProperties: Map<String, Map<String, Any?>>): Builder {
140-
this.groupProperties =
141-
(this.groupProperties ?: mutableMapOf()).apply {
142-
groupProperties.forEach { (group, properties) ->
143-
getOrPut(group) { mutableMapOf() }.putAll(properties)
144-
}
145-
}
125+
this.groupProperties = this.groupProperties.putBuilderGroupProperties(groupProperties)
146126
return this
147127
}
148128

posthog-server/src/main/java/com/posthog/server/PostHogFeatureFlagResultOptions.kt

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ public class PostHogFeatureFlagResultOptions private constructor(
5757
key: String,
5858
propValue: String,
5959
): Builder {
60-
groups =
61-
(groups ?: mutableMapOf()).apply {
62-
put(key, propValue)
63-
}
60+
groups = groups.putBuilderValue(key, propValue)
6461
return this
6562
}
6663

@@ -71,10 +68,7 @@ public class PostHogFeatureFlagResultOptions private constructor(
7168
* @return This builder.
7269
*/
7370
public fun groups(groups: Map<String, String>): Builder {
74-
this.groups =
75-
(this.groups ?: mutableMapOf()).apply {
76-
putAll(groups)
77-
}
71+
this.groups = this.groups.putBuilderValues(groups)
7872
return this
7973
}
8074

@@ -90,10 +84,7 @@ public class PostHogFeatureFlagResultOptions private constructor(
9084
key: String,
9185
propValue: Any?,
9286
): Builder {
93-
personProperties =
94-
(personProperties ?: mutableMapOf()).apply {
95-
put(key, propValue)
96-
}
87+
personProperties = personProperties.putBuilderValue(key, propValue)
9788
return this
9889
}
9990

@@ -105,10 +96,7 @@ public class PostHogFeatureFlagResultOptions private constructor(
10596
* @see <a href="https://posthog.com/docs/product-analytics/user-properties">Documentation: User Properties</a>
10697
*/
10798
public fun personProperties(userProperties: Map<String, Any?>): Builder {
108-
this.personProperties =
109-
(this.personProperties ?: mutableMapOf()).apply {
110-
putAll(userProperties)
111-
}
99+
this.personProperties = this.personProperties.putBuilderValues(userProperties)
112100
return this
113101
}
114102

@@ -125,10 +113,7 @@ public class PostHogFeatureFlagResultOptions private constructor(
125113
key: String,
126114
propValue: Any?,
127115
): Builder {
128-
groupProperties =
129-
(groupProperties ?: mutableMapOf()).apply {
130-
getOrPut(group) { mutableMapOf() }[key] = propValue
131-
}
116+
groupProperties = groupProperties.putBuilderGroupProperty(group, key, propValue)
132117
return this
133118
}
134119

@@ -139,12 +124,7 @@ public class PostHogFeatureFlagResultOptions private constructor(
139124
* @return This builder.
140125
*/
141126
public fun groupProperties(groupProperties: Map<String, Map<String, Any?>>): Builder {
142-
this.groupProperties =
143-
(this.groupProperties ?: mutableMapOf()).apply {
144-
groupProperties.forEach { (group, properties) ->
145-
getOrPut(group) { mutableMapOf() }.putAll(properties)
146-
}
147-
}
127+
this.groupProperties = this.groupProperties.putBuilderGroupProperties(groupProperties)
148128
return this
149129
}
150130

0 commit comments

Comments
 (0)