Bug 14831

Summary: EnumeratedAttributes are not developer friendly
Product: Ant Reporter: Dominique Devienne <ddevienne>
Component: CoreAssignee: Ant Notifications List <notifications>
Status: RESOLVED FIXED    
Severity: enhancement    
Priority: P3    
Version: 1.5.1   
Target Milestone: ---   
Hardware: All   
OS: other   

Description Dominique Devienne 2002-11-25 18:15:59 UTC
The code below illustrates the 3 lines of code necessary to set an enum attrib 
programmatically. I find it heavy...

PresentSelector present = new PresentSelector();
present.setTargetdir(file);
// 1) Instantiate enum attrib
PresentSelector.FilePresence both = new PresentSelector.FilePresence();
// 2) Set value of enum attrib
both.setValue("both");
// 3) Set enum attrib
present.setPresent(both);
none.appendSelector(present);

I propose 3 different things:

1) Adding a protected EnumeratedAttribute(String value) Ctor
2) Adding EnumClass(String value) { super(value); } Ctor to the various enums.
2') Prebuilt declare the various enum values, like in

public class PresentSelector extends BaseSelector {
    public static class FilePresence extends EnumeratedAttribute {
        public static final FilePresence BOTH = new FilePresence("both");
        public static final FilePresence SRCONLY = new FilePresence("srconly");
        private FilePresence(String value) { super(value); }
        public String[] getValues() {
            return new String[] {SRCONLY.getValue(), BOTH.getValue()};
        }
    }
}

My example then becomes:

PresentSelector present = new PresentSelector();
present.setTargetdir(file);
present.setPresent(PresentSelector.FilePresence.BOTH);
none.appendSelector(present);

As an aside, I personnally think that although the Bean pattern is generic and 
fine, I'd rather be able to write programmatically:

none.appendSelector(new PresentSelector(file,
                                        PresentSelector.FilePresence.BOTH));

Thanks, --DD
Comment 1 Jan Mat 2006-10-13 02:04:34 UTC
I think with the committed change your example would result in

   PresentSelector present = new PresentSelector();
   present.setTargetdir(file);
   present.setPresent(
EnumeratedAttribute.getInstance(PresentSelector.FilePresence.class, "both") );
   none.appendSelector(present);


Jan