3

I would like to find the longest String within a Java Enum. What is the best way to do this? I havent been working to much with Enums so any suggestion is welcome. These are my enums

 public enum DOMAIN_LANGUAGES {
    ENG, SWE;

    public static List<DOMAIN_LANGUAGES> getDomainLanguages(){
        List<DOMAIN_LANGUAGES> languages = new ArrayList<DOMAIN_LANGUAGES>();
        languages.add(ENG);
        languages.add(SWE);
        return languages;
    }
}

public enum DOMAIN_STATE {
    LIVE,
    PENDING_RENEWAL,
    PENDING_TRANSFER_OUT,
}

EDIT:

I didnt define this question very well and hence i edit it. What I whant from my two enums is a function that takes any of the two kind of enums i have defined as and finds the longest literal. So in the DOMAIN_STATE that would be "PENDING_TRANSFER_OUT". I hope this makes things a bit easier to understand.

EDIT 2

So now i'v gotten som help that has been great, but im not sure why this code will not work. It complains when i try to use the enumList.values()? This is the reason i couldnt find a solution in the first place, what am i missing? =)

    public String CalculateDropdownListWidth(Enum enumList){
        int chars = 0;
        for(Enum e : enumList.values()){
            //do stuff
        }
}

FINAL:

So now i knwo why this didnt work. I hade to loop over a EnumSet instead of a Enum. Since the Enum is only one in the "set" if im not totally misstaken. So this is my solution. Thanks all for helping out!

public String CalculateDropdownListWidth(EnumSet enumList){
    int chars = 0;
    for(Object e : enumList){
        if(e.toString().length() > chars){
            chars = e.toString().length();
        }
    }

    //Pixelmodifier
    Double oneChar = 1.35;

    Double result = oneChar * chars;

    return String.valueOf(result) + "px";
}

Thx for any help!

/Marthin

Marthin
  • 6,413
  • 15
  • 58
  • 95

3 Answers3

5
 String longestYet= "";
 for (Enum<?> value :OfferType.values()){
     if (longestYet.length()< value.name().length()) {
        longestYet= value.name();   
     }                
 }
 longest = longestYet;

If you need this value very often for a specific enum, then you can add it to the enum itselve, and set the value in a static initialialzer.

public static enum MyType {

    NORMAL,
    OTHER;

   public static final String longest; 
   static{
        String longestYet= "";
        for (Enum<?> value :MyType.values()){
           if (longestYet.length()< value.name().length()) {
            longestYet= value.name();   
           }                
        }
        longest = longestYet;
    }        
}

or even better, put the static initializer method, in a Util function that can be reused in several Enums.

Ralph
  • 118,862
  • 56
  • 287
  • 383
1

You can do YourEnum.values() then iterate through all and get the length of the string returned by name().

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

Iterate over DOMAIN_LANGUAGES.values and find the longest name.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205