0

I have some jxpaths I want to put them in an enumeration I will be sharing with a JSF page using a map that will make them available to EL as the keys for jxpath to do a createPathAndSetValue on.

Then I can easily fill out a model without creating tons of getters and setters. like this:

<h:inputText value="#{backingBeanMap[backingBeanMap.billingAddress_postalCode]}" />

I have something like this

public enum MWField {
    isGiftJoin,
    billingAddress_postalCode,
    ...
    associates$3_memberInfo_membershipType_type;

    public final String xpath;

    MWField(){
        this.xpath = name().replace('_', '/').replace("$0", "[0]")
        .replace("$1", "[1]").replace("$2", "[2]")
        .replace("$3", "[3]").replace("$4", "[4]")
        .replace("$5", "[5]").replace("$6", "[6]")
        .replace("$7", "[7]").replace("$6", "[6]")
        .replace("$9", "[9]").replace("$10", "[10]");
    }
}

I use the name isGiftJoin or billingAddress_postalCode instead of uppercase names and do substitution of the _ with / in the constructor (so the xpath attribute is the actual xpath). So long as none of the properties have _ in the name (they don't), I am done. The enumerations won't be "UPPERCASE", but enumerations aren't really CONSTANTS in the sense that RED is a constant. Here they are instances of a class that point at data, and aren't intended to be "constant" in that way.

Are these names too wierd and non-standard? Is it silly to be chained to the C style constant UPPERCASE standard?

ggb667
  • 1,881
  • 2
  • 20
  • 44

1 Answers1

1

Are you asking if you may name an enum whatever you want?

Of course; why not? Use a standard that works for you. Personally, I reserve all caps only for static finals, and tends towards class naming conventions for enum values.

I'm not a huge fan of underscores in names, but in your case, since you're deliberately creating a hierarchical structure, I have less of an issue with it.

I would wonder if an enum is really necessary for naming paths: do you need to use them in switch statements (in JDK 6 or less)? Is there a reason to represent them as anything other than strings? Are the names without the paths valuable?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • JSF and java need to be in synch, and getting the xpaths right can be tricky so I will use junit tests to check to be sure elements are referenced right. This way I can recycle the xpaths for other backing beans. I COULD make them strings in some other class, but id jave to have String SOME_JXPATH = "some/jxPath", which seems silly and error prone, when I can just type the name/path one time. – ggb667 Jan 24 '13 at 20:46
  • Isn't an enum instance actually a public static final instance of enum class? – Sami Korhonen Jan 24 '13 at 21:32
  • Yes the names were in fact valuable, and this solution did work out rather well. – ggb667 Nov 06 '13 at 16:52