https://kotlinlang.org logo
Title
j

jordan_k_miles

06/20/2017, 7:08 PM
is there a way to make a current java class a data class so I have access to the data class functions? I really only need copy(), so I could probably just make an extension function for it.
d

darlan

06/20/2017, 7:42 PM
jordan_k_miles: Another option is to use inheritance:
public class Pojo {
    final private String fieldA;
    final private String fieldB;
    public Pojo(String fieldA, String fieldB) {
        this.fieldA = fieldA;
        this.fieldB = fieldB;
    }
}

data class PojoData(val paramA: String, val paramB: String) : Pojo(paramA, paramB)

fun main(args: Array<String>) {
    val pojo1 = PojoData("A", "B")
    val pojo2 = pojo1.copy()
    println(pojo1 == pojo2) // true
    println(pojo1 === pojo2) // false
}
j

jordan_k_miles

06/20/2017, 7:45 PM
yeah, that was my first thought. problem is, I'm using Java's clipboard APIs and a lot of their factory methods return Clipboard class. So I would end up having to overwrite and extend several methods.
didn't know if there was a quick and easy way to add the data functionality.
d

darlan

06/20/2017, 7:49 PM
Class delegation can't resolve this for you?
This way you don't need to implement anything
class AnotherSet<T>(val hashSet: MutableCollection<T> = HashSet<T>()): MutableCollection<T> by hashSet
j

jordan_k_miles

06/20/2017, 8:24 PM
let me give that a shot.