-2

I don't know how to translate this kotlin line into java.

data class ContactData(val name: String? = "", val online: Boolean = false)

This is what I tried, but I'm not sure.

public final class ContactData {

private String name = "";
private boolean online = false;


public final String getName() {
    return name;
}

public final boolean getOnline() {
    return online;
}

ContactData(String name, boolean online) {
    this.name = name;
    this.online = online;
}

public final String name() {
    return this.name;
}

public final boolean online() {
    return this.online;
}

}

please help me to fix it

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
Ady Irawan
  • 31
  • 1
  • 4
    Besides duplicating your getters, what exactly is the problem? – Mureinik Nov 22 '18 at 07:25
  • 1
    Welcome to Stack Overflow! Please take the [tour] and visit our [help] to learn what kinds of questions are appropriate for this site. If you can [edit] your question to fit the requirements of this site, please do so. – Joe C Nov 22 '18 at 07:41
  • 1
    Please provide exact and detailed information on your actual problem. Explain what does not work, include error messages and explain with which part you have difficulties. Also make sure to do proper research before posting on StackOverflow, thanks. – Zabuzard Nov 22 '18 at 08:06
  • 1
    at least in Intellij you can show the Kotlin bytecode and from there you can choose "Decompile" to get the decompiled Java source. That code will then probably not work as is, but you should find everything you are interested in... – Roland Nov 22 '18 at 08:13
  • Thanks for your response, sorry if I don't follow the post rules, I will fix it :) – Ady Irawan Nov 22 '18 at 08:14
  • @Roland yes I have tried decompiling it and the results are very messy, so I tried asking for help here – Ady Irawan Nov 22 '18 at 08:17
  • well... why do you even want to translate it to Java? Kotlin produces bytecode, which is usable/callable from within Java classes... no need to translate them... if you just wanted to take a "short route" to get data classes in Java, **do not** use the decompiled variant of the Kotlin byte code... data classes are a language feature of Kotlin... – Roland Nov 22 '18 at 08:21

2 Answers2

1

I would say it is going to be something like this:

import org.jetbrains.annotations.NotNull;

public class ContactDataa {

    @NotNull
    private final String name;
    private final boolean online;

    public ContactDataa() {
        this("", false);
    }

    public ContactDataa(final String name) {
        this(name, false);
    }

    public ContactDataa(final boolean online) {
        this("", online);
    }

    public ContactDataa(final String name, final boolean online) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        this.name = name;
        this.online = online;
    }

    public String getName() {
        return name;
    }

    public boolean isOnline() {
        return online;
    }

    public String component1() {
        return name;
    }

    public boolean component2() {
        return online;
    }

    @NotNull
    public final ContactData copy(@NotNull String name, boolean online) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        return new ContactData(name, online);
    }

    @NotNull
    public final ContactData copy(@NotNull String name) {
        if (name == null) {
            throw new IllegalArgumentException();
        }
        return new ContactData(name, online);
    }

    @NotNull
    public final ContactData copy(boolean online) {
        return new ContactData(name, online);
    }

    @NotNull
    public final ContactData copy() {
        return new ContactData(name, online);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ContactDataa that = (ContactDataa) o;

        if (online != that.online) return false;
        return name != null ? name.equals(that.name) : that.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (online ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "ContactDataa{" +
                "name='" + name + '\'' +
                ", online=" + online +
                '}';
    }
}

Problem here is a complete intertop with other Kotlin's classes. Also, if you use Intellij IDEA or Android Studio, you can just get Kotlin bytecode and decompile it to Java.

nyarian
  • 4,085
  • 1
  • 19
  • 51
  • Will it really be this much? I've tried using decompiler, and it looks like that isn't what I'm looking for, by the way, thank you for the response – Ady Irawan Nov 22 '18 at 08:22
  • 1
    yes, it will be that much... data classes deliver `hashCode`, `toString`, `equals`, `copy`, etc. pp... that's why they are so popular... what did you expect? Please also read up about [data classes in the Kotlin reference](https://kotlinlang.org/docs/reference/data-classes.html)... – Roland Nov 22 '18 at 08:26
  • Kotlin generates very specific methods and adds specific parameters for data classes' methods, and thus compiles all the invocations of these methods from the outside in a transforming way. If you want complete intertop with Kotlin's classes - decompiler will help. I've just created a Java's copy that is the same on how this class is **used**, not by how it is **compiled** into bytecode. – nyarian Nov 22 '18 at 08:26
  • ahhh ok thank you very much, now i understand – Ady Irawan Nov 22 '18 at 08:29
  • @Roland I just want to translate [this](https://github.com/devjn/WebRTCAndroidFirebase/tree/master/app/src/main/java/com/github/devjn/webrtcandroidfirebase) source into java but I can't find the right way to translate FirebaseData.kt files – Ady Irawan Nov 22 '18 at 08:33
0

If you want the same behaviour as the default values in Kotlin you need three extra constructors:

ContactData() {
}

ContactData(String name) {
    this.name = name;
}

ContactData(boolean online) {
    this.online = online;
}

Also you have to override hashCode() and equals(), toString and copy() and the component functions see https://kotlinlang.org/docs/reference/data-classes.html.

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54