r2DoesInc
r2DoesInc

Reputation: 3771

Determining a String based on the value of another String?

Not all that sure how I would describe this question, so I'll jump right into the example code.

I have a Constants.java

   package com.t3hh4xx0r.poc;


public class Constants {

    //RootzWiki Device Forum Constants
    public static final String RWFORUM = "http://rootzwiki.com/forum/";
    public static final String TORO = "362-cdma-galaxy-nexus-developer-forum";

    public static String DEVICE;
}

In trying to determine the device type, I use this method.

public void getDevice() {
    Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
    String thread = Constants.(Constants.DEVICE);
}

Thats not correct though, but thats how I would think it would have worked.

Im setting the Constants.DEVICE to TORO in my case on the Galaxy Nexus. I want to then set the thread String to Constants.TORO.

I dont think I'm explaining this well, but you shoudl be able to understand what I'm trying to do fromt he example code. I want

Constants.(VALUE OF WHAT CONSTANTS.DEVICE IS) set for the String thread.

Another way to put it,

I want to get Constants.(//value of android.os.Build.DEVICE.toUpperCase())

I apologies for the poorly worded question, i dont know of any better way to explain what Im trying to achieve.

Im trying to determine the thread based on the device type. I could go in and do an

if (Constants.DEVICE.equals("TORO"){
    String thread = Constants.TORO;
}

But I plan on adding a lot more device options in the future and would like to make it as easy as adding a string to the Constants.java rather than having to add another if clause.

Upvotes: 1

Views: 190

Answers (3)

Jon Skeet
Jon Skeet

Reputation: 1499940

I would suggest using an enum instead of just strings - then you can use:

String name = android.os.Build.DEVICE.toUpperCase();
// DeviceType is the new enum
DeviceType type = Enum.valueOf(DeviceType.class, name);

You can put the value of the string in a field for the enum, and expose it via a property:

public enum DeviceType {
    RWFORUM("http://rootzwiki.com/forum/"),
    TORO("362-cdma-galaxy-nexus-developer-forum");

    private final String forumUrl;

    private DeviceType(String forumUrl) {
        this.forumUrl = forumUrl;
    }

    public String getForumUrl() {
        return forumUrl;
    }
}

(I'm guessing at the meaning of the string value - not a great guess, but hopefully it gives the right idea so you can make your actual code more meaningful.)

EDIT: Or to use a map:

Map<String, String> deviceToForumMap = new HashMap<String, String>();
deviceToForumMap.put("RWFORUM", "http://rootzwiki.com/forum/");
deviceToForumMap.put("TORO", "362-cdma-galaxy-nexus-developer-forum");

...

String forum = deviceToForumMap.get(android.os.Build.DEVICE.toUpperCase());

Upvotes: 4

njzk2
njzk2

Reputation: 39386

You can use reflection:

Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
String thread = (String) Constants.class.getField(Constants.DEVICE).get(null);

Upvotes: 1

Egor
Egor

Reputation: 40193

Not sure I've understood the question properly, but I feel that it's a right place to use a Map. The outcome will be something like this:

    HashMap<String, String> map;

    map.put(TORO, DEVICE);

    Constants.DEVICE = android.os.Build.DEVICE.toUpperCase();
    String thread = map.get(Constants.DEVICE);

Sorry for a possible misunderstanding or your question, but I hope you've got the idea.

P.S. You can find more info about Maps in the Java documentation: Map, HashMap.

Upvotes: 0

Related Questions