I have a AWTrack class in my app as follows:
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import kaaes.spotify.webapi.android.models.Track;
public class AWTrack implements Serializable {
private int id;
private String idSpotify;
private String name;
private String album;
private String author;
private String spotifyPreviewUrl;
private int musicType;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdSpotify() {
return idSpotify;
}
public void setIdSpotify(String idSpotify) {
this.idSpotify = idSpotify;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private int getMusicType() {
return musicType;
}
public void setMusicType(int musicType) {
this.musicType = musicType;
}
public String getSpotifyPreviewUrl() {
return spotifyPreviewUrl;
}
public void setSpotifyPreviewUrl(String spotifyPreviewUrl) {
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
public AWTrack(){
}
public AWTrack(int id)
{
this.id = id;
}
public static AWTrack map(Track track, int trackId)
{
/*AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};*/
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
}
In this class I've created a static map method intended to cast kaaes.spotify.webapi.android.models.Track object into AWTrack (only the needed properties) and I'm trying to use inline object initialization in the following way:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};
}
But I'm facing a weird problem, and it's that the resulting AWTrack object has an embedded (and unexpected) Track object in it.
If, on the contrary, I do object initialization in the traditional way -like the next-:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
it works correctly, but the fact is I'm expecting to be able to do object initialization like the one shown in the first method, definitely prefer the first syntax.
EDIT: To make it clear, my main problem is after creating the object I serialise it with Gson like this:
ArrayList<AWTrack> list = .....
Gson gson = new Gson();
String json = gson.toJson(list);
And if I apply serialisation to the object instantiated in the classic way I get a string like the next:
[{"author":"Philip Wesley","id":70,"musicType":0,"name":"Loves Crush","spotifyPreviewUrl":"http://xxx"}]
But with the inline instantiation what I get after serialisation with Gson is:
"[null]"
Any help will be much appreciated.
Ok, I'll answer my own question.
The answer is simple, as far as I've read the inline initialisation with {{}} creates an anonymous inner class (What is Double Brace initialization in Java?), so it's not the way you would expect it to work and may produce memory leaks if you do lots of initialisations. In many cases it could work, and I like that syntax, but Gson won't accept that object as a parameter for serialisation.
I've ended up going through the traditional constructor way. In the end it's clear and only one line of code:
public AWTrack(int id, String name, String author, String spotifyPreviewUrl) {
this.id = id;
this.name = name;
this.author = author;
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
And then:
AWTrack awTrack = new AWTrack(trackId, track.name, track.artists.get(0).name, track.preview_url);