views:

74

answers:

3

I want to improve my use of JDK 1.5 and stop using private static final String instead of enum. This is what seems to be recommended.

But now my constant class looks like this :

public class CEnum{
    /**
     * @author JJA
     * date : 20/10/2010
     */
    public enum ListTypeAffichage {
        DEP("DEPOT_TVA"), PAD("PAS_DEPOT_TVA"), NORM("CAT_NORMALE"), CAP("CAT_PARTICULIERE"), CAA("CAT_AUTRE_CAS");

        private final String sName;

        /**
         * @param name String
         */    
        ListTypeAffichage(String name) {
            this.sName = name;
        }

        /**
         * @return String
         */
        public String getType() {
            return sName;
        }        
    }

    /**
     * @author JJA
     * date : 20/10/2010
     */
    public enum ListTypeCategorie {
        DEDUIRE("SOMME_A_DEDUIRE"), AJOUTER("SOMME_A_AJOUTER");

        private final String sName;

        /**
         * @param name String
         */
        ListTypeCategorie(String name) {
            this.sName = name;
        }

        /**
         * @return String
         */
        public String getType() {
            return sName;
        }    
    }

    /**
     * @author JJA
     * date : 26/10/2010
     */
    public enum ListInterval {
        POS("POSITIF"), NS("NON_SIGNE");

        private final String sName;

        /**
         * @param name String
         */
        ListInterval(String name) {
            this.sName = name;
        }

        /**
         * @return String
         */
        public String getInterval() {
            return sName;
        }    
    }
}

instead of

public class ConstantesADMD {
    public static final List<String> typeAffich = new ArrayList<String>();
...
    ConstantesADMD(){
        typeAffich.add("DEPOT_TVA");
        typeAffich.add("PAS_DEPOT_TVA");
        typeAffich.add("CAT_NORMALE"); 

...       
    }
}

My code seems to be really bad, but at least works. For each enum I have to add the redundant code :

private final String sName;

/**
 * @param name String
 */    
ListTypeAffichage(String name) {
    this.sName = name;
}

/**
 * @return String
 */
public String getType() {
    return sName;
}

What improvment do you advise me? Note : forget the last sentences of my first question, I need the index. Tell me if I have to post another question, editing my fisrt seems easier.

+3  A: 

I would name my enum-constants as you have named your strings. You can then access the name using the Enum.toString() method. For example:

public enum ListTypeAffichage {
    DEPOT_TVA, PAS_DEPOT_TVA, CAT_NORMALE, CAT_PARTICULIERE, CAT_AUTRE_CAS;

    /**
     * @return String
     */
    public String getType() {
        return toString();
    }        
}

You could of course also skip the getType() all together, and access the "type" using toString() instead:

ListTypeAffichage myEnum = ListTypeAffichage.CAT_PARTICULIRE;
System.out.println("Type: " + myEnum.toString());             // like this...
System.out.println("Type: " + myEnum);                        // ...or like this

According to the API, this is better than using the Enum.name() directly:

public final String name()
        [...] Most programmers should use the toString() method in preference to this one [...]

aioobe
This is better than the answer of my first question thanks. I used the name method instead of toString.
jayjaypg22
+2  A: 

Each enum has a name() method which return the exact string represantation of the constant. So you may do this:

 public enum ListTypeAffichage {
        DEPOT_TVA, PAS_DEPOT_TVA, CAT_NORMALE, CAT_PARTICULIERE, CAT_AUTRE_CAS                  
    }

and then

ListTypeAffichage.DEPOT_TVA.name();
Vladimir Ivanov
A: 

By using the abbreviations (DEP, PAD, NORM) etc. you have created aliases to (DEPOT_TVA, PAS_DEPOT_TVA, CAT_NORMALE) etc.

If you want to keep the abbreviations, then you'll have to maintain the enum as you have it.

If you are willing to do away with the abbreviations, then you can change you enum as below, I have included a main method in the enum to demonstrate its use.

public enum ListTypeAffichageNames {
    DEPOT_TVA,
    PAS_DEPOT_TVA,
    CAT_NORMALE,
    CAT_PARTICULIERE,
    CAT_AUTRE_CAS;


    public static void main(String[] args) {
        System.out.println(DEPOT_TVA.toString());
        ListTypeAffichageNames affichage = ListTypeAffichageNames.valueOf("DEPOT_TVA");
        System.out.println(affichage.toString());
    }
}  

In addition to this, your current structure of constants does not give you compile time type checking, and does not prevent something like the following happening during runtime:

    ConstantesADMD.typeAffich.clear();
    // or
    ConstantesADMD.typeAffich.remove("DEPOT_TVA");
    ConstantesADMD.typeAffich.add("dEpOt-tVa");
crowne