WISHY
WISHY

Reputation: 11999

Overriding get method for variable in class kotlin?

I have a model class in Java which I converted to data class in kotlin

public class VideoAssets implements Serializable {

@SerializedName("type")
@Expose
String type;

@SerializedName("mpeg")
@Expose
List<Mpeg> mpeg = null;

@SerializedName("hls")
@Expose
String hls;

@SerializedName("widevine")
@Expose
WideVine wideVine;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public List<Mpeg> getMpeg() {
    return mpeg;
}

public void setMpeg(List<Mpeg> mpeg) {
    this.mpeg = mpeg;
}

public String getHls() {
    hls = Macros.INSTANCE.replaceURl(hls);
    return hls;
}

public void setHls(String hls) {
    this.hls = hls;
}

public WideVine getWideVine() {
    return wideVine;
}

public void setWideVine(WideVine wideVine) {
    this.wideVine = wideVine;
}
}

As you see I want to change the value of variable hls when I retrieve it.

I created the data class as below

data class VideoAssets(@SerializedName("mpeg") @Expose
                   var mpeg: List<Mpeg> = emptyList(),
                   @SerializedName("hls")
                   @Expose
                   var hls: String,
                   @SerializedName("widevine")
                   @Expose
                   val wideVine: WideVine? = null) : Serializable

I am struggling here as how should I update the get method for data class. After searching and taking reference from Override getter for Kotlin data class I even created a non data class which doesn't seem to work

class VideoAssets(@SerializedName("mpeg") @Expose
              var mpeg: List<Mpeg> = emptyList(),
              @SerializedName("hls")
              @Expose
              val hlsUrl: String? = null,
              @SerializedName("widevine")
              @Expose
              val wideVine: WideVine? = null) : Serializable {
val hls: String? = hlsUrl
    get() = field?.let { Macros.replaceURl(it) }

}

Whenerver I try to retrieve videoAssets.getHls() it returns null while it should return the new value. The object videoAssets.gethlsUrl() has the value but not `videoAssets.getHls()' is always null.

Can someone point me what I am missing?

Upvotes: 0

Views: 1427

Answers (1)

cactustictacs
cactustictacs

Reputation: 19524

Here's your code:

val hls: String? = hlsUrl
    get() = field?.let { Macros.replaceURl(it) }

So what this is doing, is creating a property called hls and giving it a backing field (a variable) called field. It initially sets that to whatever value for hlsUrl was passed into the constructor (might be null).

The getter code takes that value for field, and if it isn't null it calls that replaceURl function and returns the result, otherwise it returns null.

So if you set hlsUrl to null, field will always be null and the hls getter will always return null. Even if you update hlsUrl later (whicb I'm assuming you're doing, the code runs fine for me if I pass in a value to the constructor) the value of field is fixed at initialisation.

Also your Java code runs differently - when that gets the new value of hls, it stores that and uses it in the function call of the next get. You're never changing the value of field so your Kotlin code uses the initial value every time.

Technically you don't need the backing field since you're always effectively calling hlsUrl?.let { Macros.replaceURl(it) }. In that case you could make hlsUrl var and update that, or you can add a setter to your hls property and set the backing field when you get the new value

Here's the Kotlin page on properties, in case you haven't seen it!

Upvotes: 1

Related Questions