In my workplace, our web application used to show the data in LIST. It also allows their users to download that list's data in Excel format (we are using Apache POI for Excel). Now everytime we need to hardcode the column heading, fileheading:

    ExportPdfDataDTO dataDTO = new ExportPdfDataDTO();
//File Name
                    dataDTO.setFileName("Nit Workorder");
//Heading OF Excel 
                    dataDTO.setHeading("NIT List");
//Columns Heading
                    dataDTO.getTableHeading().add("Tender No.");
                    dataDTO.getTableHeading().add("Tender Fee");
                    dataDTO.getTableHeading().add("Sd Amount");

Now instead of hard coding it, I maintained an enum for each Excel headings, in which I maintained constants corresponding to values, so my code becomes like this:

   ExportPdfDataDTO dataDTO = new ExportPdfDataDTO();

What I observe in our ENUMS implementation is that there are only two methods getxxxByName() and getxxxByValue() in the entire application, which is commonly used. It means lots of boilerplate code which is not good programming practice. I later maintained a class in which I add up one or more enums for distinct Excel files, pull those common methods in the outer class by make use of generics so every enum can use it.

Now the problem is that the code only works for single values of ENUM constants. If we try to append one or more argument in ENUM constant it will not able to handle

For example:


But if:


then it will not work.

I need a solution which will take n number of arguments in ENUM constants, so give me suggestions.


  1. Method can accept all types of data
  2. Enum constants can take n number of values of any type

     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
    package com.awzpact.cm.util;

     * @author zia.khan

    interface Named {
        public String getName();

    // Super class of all Tables.
     class Table<E extends Enum<E> & Named> {
        private final Class<E> itsClass;
        private final String sheetName;
        private final String fileName;

        public Table(Class<E> itsClass) {
            this.itsClass = itsClass;
            // Walk the enum to get filename and sheet name.
            String sheetName = null;
            String fileName = null;
            for ( E e: itsClass.getEnumConstants() ){
                if ( e.name().equals("FILENAME")) {

                    fileName = e.getName();

                if ( e.name().equals("THEAD")) {
                    sheetName = e.getName();
            this.sheetName = sheetName;
            this.fileName = fileName;

        // Use the interface and the enum details to do your stuff.
        public E getByName (String name) {
            for ( E e: itsClass.getEnumConstants() ){
                if ( e.getName().equals(name)) {
                    return e;
            return null;

    // Extend Table and tell it about your enum using the super constructor.
     class TableContent1 extends Table<TableContent1.Nit> {

        public TableContent1() {

        public enum Nit implements Named{
            THEAD("NIT WORKORDER"),
            FEE("TENDER FEE"),
            SDAMOUNT("SD AMOUNT"),
            TYPE("NIT TYPE"),
            PRE_BID("PRE BIDDING DATE"),
            OPEN_DATE("OPENING DATE"),

            private final String name;

            Nit(String name) {
                this.name = name;

            public String getName() {
                return name;


    public class Test {
        public static void main(String[] args) {
            Table t=new TableContent1();
            System.out.println("File Name"+" "+ TableContent1.Nit.THEAD.getName());
            System.out.println("File Name"+" "+ TableContent1.Nit.FILENAME.getName());
            System.out.println("File Name"+" "+ TableContent1.Nit.FEE.getName());
            System.out.println("File Name"+" "+ TableContent1.Nit.SDAMOUNT.getName());
            System.out.println("File Name"+" "+ TableContent1.Nit.STATUS.getName());
            System.out.println("File Name"+" "+ TableContent1.Nit.TENDERSPECNO.getName());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.