@@ -28,8 +28,8 @@ And you will have to include the required dependencies:
28
28
29
29
``` groovy
30
30
dependencies {
31
- implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.5.2 '
32
- ksp 'com.github.tobrun.kotlin-data-compat:processor:0.5.2 '
31
+ implementation 'com.github.tobrun.kotlin-data-compat:annotation:0.6.0 '
32
+ ksp 'com.github.tobrun.kotlin-data-compat:processor:0.6.0 '
33
33
}
34
34
```
35
35
@@ -44,10 +44,14 @@ Given an exisiting data class:
44
44
- support imports for default parameters
45
45
- retain existing class annotations (but not parameters for them)
46
46
- retain existing interfaces
47
+ - make non-nullable parameters without defaults mandatory Builder constructor parameters
47
48
48
49
For example:
49
50
50
51
``` kotlin
52
+ import com.tobrun.datacompat.annotation.DataCompat
53
+ import com.tobrun.datacompat.annotation.Default
54
+
51
55
interface SampleInterface
52
56
annotation class SampleAnnotation
53
57
@@ -67,19 +71,31 @@ private data class PersonData(
67
71
*/
68
72
val nickname : String? ,
69
73
@Default(" 42" )
70
- val age : Int
74
+ val age : Int ,
75
+ @Default(" 50.2f" )
76
+ val euroAmount : Float ,
77
+ @Default(" 300.0" )
78
+ val dollarAmount : Double? ,
79
+ /* *
80
+ * Extra info. Mandatory property.
81
+ */
82
+ val extraInfo : String ,
71
83
) : SampleInterface
72
84
```
73
85
74
86
After compilation, the following class will be generated:
75
87
76
88
``` kotlin
89
+ @file:Suppress(" RedundantVisibilityModifier" )
90
+
77
91
package com.tobrun.`data`.compat.example
78
92
79
93
import java.util.Date
80
94
import java.util.Objects
81
95
import kotlin.Any
82
96
import kotlin.Boolean
97
+ import kotlin.Double
98
+ import kotlin.Float
83
99
import kotlin.Int
84
100
import kotlin.String
85
101
import kotlin.Unit
@@ -94,24 +110,35 @@ import kotlin.jvm.JvmSynthetic
94
110
@SampleAnnotation
95
111
public class Person private constructor(
96
112
/* *
97
- * The full name .
113
+ * Name .
98
114
*/
99
115
public val name : String ,
100
116
/* *
101
- * The nickname.
102
117
* Additional comment.
103
118
*/
104
119
public val nickname : String? ,
105
120
/* *
106
- * The age.
121
+ * Age.
122
+ */
123
+ public val age : Int ,
124
+ /* *
125
+ * Euro amount.
107
126
*/
108
- public val age : Int
127
+ public val euroAmount : Float ,
128
+ /* *
129
+ * Dollar amount.
130
+ */
131
+ public val dollarAmount : Double? ,
132
+ /* *
133
+ * Extra info. Mandatory property.
134
+ */
135
+ public val extraInfo : String
109
136
) : SampleInterface {
110
137
/* *
111
138
* Overloaded toString function.
112
139
*/
113
- public override fun toString () = """ Person(name=$name , nickname=$nickname ,
114
- age= $age )""" .trimIndent()
140
+ public override fun toString () = """ Person(name=$name , nickname=$nickname , age= $age ,
141
+ euroAmount= $euroAmount , dollarAmount= $dollarAmount , extraInfo= $extraInfo )""" .trimIndent()
115
142
116
143
/* *
117
144
* Overloaded equals function.
@@ -120,64 +147,82 @@ public class Person private constructor(
120
147
if (this == = other) return true
121
148
if (javaClass != other?.javaClass) return false
122
149
other as Person
123
- return name == other.name && nickname == other.nickname && age == other.age
150
+ return name == other.name && nickname == other.nickname && age == other.age &&
151
+ euroAmount.compareTo(other.euroAmount) == 0 &&
152
+ (dollarAmount ? : 0.0 ).compareTo(other.dollarAmount ? : 0.0 ) == 0 &&
153
+ extraInfo == other.extraInfo
124
154
}
125
155
126
156
/* *
127
157
* Overloaded hashCode function based on all class properties.
128
158
*/
129
- public override fun hashCode (): Int = Objects .hash(name, nickname, age)
159
+ public override fun hashCode (): Int = Objects .hash(name, nickname, age, euroAmount, dollarAmount,
160
+ extraInfo)
130
161
131
162
/* *
132
163
* Convert to Builder allowing to change class properties.
133
164
*/
134
- public fun toBuilder (): Builder = Builder () .setName(name) .setNickname(nickname) .setAge(age)
165
+ public fun toBuilder (): Builder = Builder (extraInfo) .setName(name) .setNickname(nickname)
166
+ .setAge(age) .setEuroAmount(euroAmount) .setDollarAmount(dollarAmount)
167
+ .setExtraInfo(extraInfo)
135
168
136
169
/* *
137
170
* Composes and builds a [Person] object.
138
171
*
139
172
* This is a concrete implementation of the builder design pattern.
140
- *
141
- * @property name The full name.
142
- * @property nickname The nickname.
143
- * @property age The age.
144
173
*/
145
- public class Builder {
174
+ public class Builder (
146
175
/* *
147
- * The full name .
176
+ * Extra info. Mandatory property .
148
177
*/
149
178
@set:JvmSynthetic
150
- public var name: String? = " John" + Date (1580897313933L ).toString()
179
+ public var extraInfo : String
180
+ ) {
181
+ /* *
182
+ * Name.
183
+ */
184
+ @set:JvmSynthetic
185
+ public var name: String = " John" + Date (1580897313933L ).toString()
151
186
152
187
/* *
153
- * The nickname.
154
188
* Additional comment.
155
189
*/
156
190
@set:JvmSynthetic
157
191
public var nickname: String? = null
158
192
159
193
/* *
160
- * The age.
194
+ * Age.
195
+ */
196
+ @set:JvmSynthetic
197
+ public var age: Int = 42
198
+
199
+ /* *
200
+ * Euro amount.
161
201
*/
162
202
@set:JvmSynthetic
163
- public var age : Int? = 42
203
+ public var euroAmount : Float = 50.2f
164
204
165
205
/* *
166
- * Set the full name.
206
+ * Dollar amount.
207
+ */
208
+ @set:JvmSynthetic
209
+ public var dollarAmount: Double? = 300.0
210
+
211
+ /* *
212
+ * Setter for name: name.
167
213
*
168
- * @param name the full name.
214
+ * @param name
169
215
* @return Builder
170
216
*/
171
- public fun setName (name : String? ): Builder {
217
+ public fun setName (name : String ): Builder {
172
218
this .name = name
173
219
return this
174
220
}
175
221
176
222
/* *
177
- * Set the nickname.
178
- * Additional comment.
223
+ * Setter for nickname: additional comment.
179
224
*
180
- * @param nickname the nickname.
225
+ * @param nickname
181
226
* @return Builder
182
227
*/
183
228
public fun setNickname (nickname : String? ): Builder {
@@ -186,32 +231,55 @@ public class Person private constructor(
186
231
}
187
232
188
233
/* *
189
- * Set the age.
234
+ * Setter for age: age.
190
235
*
191
- * @param age the age.
236
+ * @param age
192
237
* @return Builder
193
238
*/
194
- public fun setAge (age : Int? ): Builder {
239
+ public fun setAge (age : Int ): Builder {
195
240
this .age = age
196
241
return this
197
242
}
198
243
199
244
/* *
200
- * Returns a [Person] reference to the object being constructed by the builder .
245
+ * Setter for euroAmount: euro amount .
201
246
*
202
- * Throws an [IllegalArgumentException] when a non-null property wasn't initialised.
247
+ * @param euroAmount
248
+ * @return Builder
249
+ */
250
+ public fun setEuroAmount (euroAmount : Float ): Builder {
251
+ this .euroAmount = euroAmount
252
+ return this
253
+ }
254
+
255
+ /* *
256
+ * Setter for dollarAmount: dollar amount.
203
257
*
204
- * @return Person
258
+ * @param dollarAmount
259
+ * @return Builder
205
260
*/
206
- public fun build (): Person {
207
- if (name== null ) {
208
- throw IllegalArgumentException (""" Null name found when building Person.""" .trimIndent())
209
- }
210
- if (age== null ) {
211
- throw IllegalArgumentException (""" Null age found when building Person.""" .trimIndent())
212
- }
213
- return Person (name!! , nickname, age!! )
261
+ public fun setDollarAmount (dollarAmount : Double? ): Builder {
262
+ this .dollarAmount = dollarAmount
263
+ return this
214
264
}
265
+
266
+ /* *
267
+ * Setter for extraInfo: extra info. Mandatory property.
268
+ *
269
+ * @param extraInfo
270
+ * @return Builder
271
+ */
272
+ public fun setExtraInfo (extraInfo : String ): Builder {
273
+ this .extraInfo = extraInfo
274
+ return this
275
+ }
276
+
277
+ /* *
278
+ * Returns a [Person] reference to the object being constructed by the builder.
279
+ *
280
+ * @return Person
281
+ */
282
+ public fun build (): Person = Person (name, nickname, age, euroAmount, dollarAmount, extraInfo)
215
283
}
216
284
}
217
285
@@ -222,6 +290,6 @@ public class Person private constructor(
222
290
* @return Person
223
291
*/
224
292
@JvmSynthetic
225
- public fun Person (initializer : Person .Builder .() -> Unit ): Person =
226
- Person .Builder ().apply (initializer).build()
293
+ public fun Person (extraInfo : String , initializer : Person .Builder .() -> Unit ): Person =
294
+ Person .Builder (extraInfo ).apply (initializer).build()
227
295
```
0 commit comments