-1

I am getting different provider through service call.

On that base my tab section will differ; and I want to minimize this code:

if(selectedProvider.equalsIgnoreCase("youtube")){
    switch (tabName.toLowerCase()) {
        case "songs":
            sectionTab = "video";
            break;
        case "artists":
            sectionTab="";
            break;
        case "albums":
            sectionTab="channel";
            break;
        case "playlists":
            sectionTab="playlist";
            break;
    }}
else if(selectedProvider.equalsIgnoreCase("soundcloud")){
    switch (tabName.toLowerCase()) {
        case "songs":
            sectionTab = "track";
            break;
        case "artists":
            sectionTab="artist";
            break;
        case "albums":
            sectionTab="";
            break;
        case "playlists":
            sectionTab="playlist";
            break;
    }}
else {
    switch (tabName.toLowerCase()) {
        case "songs":
            sectionTab = "track";
            break;
        case "artists":
            sectionTab = "artist";
            break;
        case "albums":
            sectionTab = "album";
            break;
        case "playlists":
            sectionTab = "playlist";
            break;
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108

4 Answers4

1

One possible solution here: use a map that contains a map.

Like:

Map<String, String> soundCloudMappings = new HashMap<>();
soundCloudMappings.put("songs", "track");

...

Map<String, Map<String, String> providerMappings = ...
providerMappings.put("soundcloud", soundCloudMappings);

And then you can check if provider.toLowerCase() exists in your outer map; and then ask the inner map for the correct sectionTab entry.

But of course, this is a pretty "low level" solution. Depending on your context, you might rather look into turning these raw strings into Enums constants; and add nice mapping methods to that Enum. In other words: consider balancing flexibility (doing everything with strings) with increased compile-time safety.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

You can use a couple of Maps like this to do the translation

static final Map<String, String> SECTION_TAB2 = new LinkedHashMap<>();
static final Map<String, String> SECTION_TAB1 = new LinkedHashMap<>();

static {
    // are there special two word outcomes
    SECTION_TAB2.put("youtube songs", "artists");
    SECTION_TAB2.put("youtube artists", "");
    SECTION_TAB2.put("youtube albums", "channel");
    SECTION_TAB2.put("soundcloud albums", "");
    // if not, what are the default tab name outcomes.
    SECTION_TAB1.put("songs", "track");
    SECTION_TAB1.put("artists", "artist");
    SECTION_TAB1.put("albums", "album");
    SECTION_TAB1.put("playlists", "playlist");
}

public static String sectionTab(String selectedProvider, String tabName) {
    return SECTION_TAB2.getOrDefault((selectedProvider + " " + tabName).toLowerCase(),
            SECTION_TAB1.get(tabName.toLowerCase()));
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

I prefer to build structures to define the logic rather than coding it in situations like this.

// What tabs we have.
enum Tabs {
    songs,
    artists,
    albums,
    playlists;
    // Build a lookup.
    static Map<String, Tabs> lookup = Arrays.stream(Tabs.values()).collect(Collectors.toMap(e -> e.name(), e -> e));

    static Tabs lookup(String s) {
        return lookup.get(s);
    }
}

// The providers.
enum Providers {
    youtube("video","","channel","playlist"),
    soundcloud("track","artist","","playlist"),
    others("track","artist","album","playlist");
    // Build a String lookup.
    static Map<String, Providers> lookup = Arrays.stream(Providers.values()).collect(Collectors.toMap(e -> e.name(), e -> e));

    Map<Tabs,String> tabs = new HashMap<>();
    Providers(String track, String artist, String album, String playlists) {
        tabs.put(Tabs.songs, track);
        tabs.put(Tabs.artists, artist);
        tabs.put(Tabs.albums, album);
        tabs.put(Tabs.playlists, playlists);
    }

    static Providers lookup(String s) {
        Providers p = lookup.get(s);
        // Default to others.
        return p == null ? others : p;
    }

    public static String getSectionTabName(String provider, String tabName) {
        // Lookup the provider.
        Providers p = lookup(provider);
        Tabs t = Tabs.lookup(tabName);
        return p.tabs.get(t);
    }
}

public void test() {
    String provider = "youtube";
    String tabName = "albums";
    String section = Providers.getSectionTabName(provider, tabName);
    System.out.println(provider+"!"+tabName+" = "+section);
}

Benefits to doing it this way:

  • Adding new providers just requires adding new enums.
  • Adding new tabs is a little less trivial (add a new parameter to the Providers constructor) but still does not change the code significantly.
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
-1

i am unable to comment so i am commenting as answer for

      public static void main(String[] args) throws IOException
{
    Table<String,String,String> table= HashBasedTable.create();
    table.put("youtube","songs","video");
    table.put("youtube","artists","");
    table.put("youtube","albums","channel");
    table.put("youtube","playlists","playlist");
    table.put("soundcloud","songs","track");
    table.put("soundcloud","artists","artist");
    table.put("soundcloud","albums","");
    table.put("soundcloud","playlists","playlist");
    table.put("default","songs","track");
    table.put("default","artists","artist");
    table.put("default","albums","albums");
    table.put("default","playlists","playlist");
  String sectionTab =  table.get("soundcloud","artists"); // u will get artist
}

we can use guava table to avoid map of map and easy to maintain and redable

https://www.tutorialspoint.com/guava/guava_table.htm

Ashok Kumar N
  • 573
  • 6
  • 23