IntelliJ Community . FileTemplates

 
FileTemplates 

HOME INDEX SEARCH CHANGES GO  

Custom File Templates

Although this feature does not seem like a fully developed feature in IDEA it is already quite useful. Please contribute your own file templates especially if they help to imlpement patterns and known paradigms.

Extended Enumeration Class

This file template is based on the build-in 'Enumaration Class'. I have added an integer to the class because I often need to carry both a printable name(logging;debugging) and an int value(xml,bit-mask filters).

    package ${PACKAGE_NAME};
    /**
     * Extended Enumeration Object
     *
     * @author ${USER}
     * @since ${DATE}
     */
    public class ${Class_name} {
    #if (${Value_1} != "")
        public static final ${Class_name} ${Value_1} = new ${Class_name}("${Value_1}",0);
    #end
    #if (${Value_2} != "")
        public static final ${Class_name} ${Value_2} = new ${Class_name}("${Value_2}",1);
    #end
    #if (${Value_3} != "")
        public static final ${Class_name} ${Value_3} = new ${Class_name}("${Value_3}",2);
    #end
    #if (${Value_4} != "")
        public static final ${Class_name} ${Value_4} = new ${Class_name}("${Value_4}",3);
    #end
    #if (${Value_5} != "")
    public static final ${Class_name} ${Value_5} = new ${Class_name}("${Value_5}",4);
    #end

        private final int m_value; // for debug only
        private final String m_name;
    
        /**
         * private constructor.
         */
        private ${Class_name}(String name, int value) {
            m_value = value;
            m_name= name;
        }

        /**
         * Get int value for type. 
         * Useful for matching the type with existing 
         * integer representation of thetype. (example: xml or 3rd-party library).
         */
        public int value(){
          return m_value;
        } 
    
        /** 
         * Get the name representation of the type. 
         * Useful for debugging and logging.
         */
        public String name() {
            return m_name;
        }
    }

Starting with Java 1.4.x there is a class called EnumSyntax that provides the similar functionality. It's in the javax.print.attribute package which is a strange location for what I consider a universal paradigm. It should really be in java.lang or java.util as far as I am concerned.

-- FlorianHehlen - 30 Jan 2003

But EnumSyntax not support naming. Also, i have some util classes for it, if you can, take a look at http://sourceforge.net/projects/phantom-common/. There are docs, but only in russian yet. So, you can look at class org.phantom.lang.AbstractEnumeration. This class provide some reflections mechanizm for dinamical building Enumeration classes:

package test.lang;

import org.phantom.lang.AbstractEnumeration;

import java.util.List;
import java.util.Iterator;

/**
 * Enumeration Test
 * @author Alexey Efimov
 */
public class EnumerationTest extends AbstractEnumeration {

  public static final EnumerationTest E1 = new EnumerationTest(new Long(0));
  public static final EnumerationTest E2 = new EnumerationTest(new Long(1));
  public static final EnumerationTest E3 = new EnumerationTest(new Long(2));

  private EnumerationTest(Object value) {
    super(value);
  }

  public static List enumerationValues() {
    return enumerationValues(EnumerationTest.class);
  }

  public static void main(String[] args) {
    System.out.println("-- Now directly toString for each of values");
    System.out.println(EnumerationTest.E1.getName() + 
      " toString is " + EnumerationTest.E1);
    System.out.println(EnumerationTest.E2.getName() + 
      " toString is " + EnumerationTest.E2);
    System.out.println(EnumerationTest.E3.getName() + 
      " toString is " + EnumerationTest.E3);

    // Retrive all values
    System.out.println("-- Now toString for each of values, " + 
      "returning by EnumerationTest.enumerationValues()");
    List values = EnumerationTest.enumerationValues();
    Iterator iterator = values.iterator();
    while (iterator.hasNext()) {
      EnumerationTest enumerationTest = (EnumerationTest)iterator.next();
      System.out.println(enumerationTest.getName() + 
        " toString is " + enumerationTest);
    }
    System.out.println("-- Now compare values");
    System.out.println(EnumerationTest.E1.getName() + " equals " + 
      EnumerationTest.E1.getName() + " is " + 
      EnumerationTest.E1.equals(EnumerationTest.E1));
    System.out.println(EnumerationTest.E1.getName() + " equals " + 
      EnumerationTest.E2.getName() + " is " + 
      EnumerationTest.E1.equals(EnumerationTest.E2));
    System.out.println(EnumerationTest.E1.getName() + " equals " + 
      EnumerationTest.E3.getName() + " is " + 
      EnumerationTest.E1.equals(EnumerationTest.E3));
    System.out.println(EnumerationTest.E2.getName() + " equals " + 
      EnumerationTest.E1.getName() + " is " + 
      EnumerationTest.E2.equals(EnumerationTest.E1));
    System.out.println(EnumerationTest.E2.getName() + " equals " + 
      EnumerationTest.E2.getName() + " is " + 
      EnumerationTest.E2.equals(EnumerationTest.E2));
    System.out.println(EnumerationTest.E2.getName() + " equals " + 
      EnumerationTest.E3.getName() + " is " + 
      EnumerationTest.E2.equals(EnumerationTest.E3));
    System.out.println(EnumerationTest.E3.getName() + " equals " + 
      EnumerationTest.E1.getName() + " is " + 
      EnumerationTest.E3.equals(EnumerationTest.E1));
    System.out.println(EnumerationTest.E3.getName() + " equals " + 
      EnumerationTest.E2.getName() + " is " + 
      EnumerationTest.E3.equals(EnumerationTest.E2));
    System.out.println(EnumerationTest.E3.getName() + " equals " + 
      EnumerationTest.E3.getName() + " is " + 
      EnumerationTest.E3.equals(EnumerationTest.E3));
  }
}
The result is:
-- Now directly toString for each of values
E1 toString is E1 = 0
E2 toString is E2 = 1
E3 toString is E3 = 2
-- Now toString for each of values, returning by EnumerationTest.enumerationValues()
E1 toString is E1 = 0
E2 toString is E2 = 1
E3 toString is E3 = 2
-- Now compare values
E1 equals E1 is true
E1 equals E2 is false
E1 equals E3 is false
E2 equals E1 is false
E2 equals E2 is true
E2 equals E3 is false
E3 equals E1 is false
E3 equals E2 is false
E3 equals E3 is true

-- AlexeyEfimov - 01 Jul 2003

e d i t a t t a c h r e f - b y d i f f s m o r e
Have ideas, requests, problems regarding this site? Send feedback.
Copyright © 2000-2003 by the contributing authors. All materials at intellij.org are the property of the contributing authors.