title: Iterate enum values using java generics
body: <p>I'm trying to find a way to iterate through an enum's values while using generics.  Not sure how to do this or if it is possible.  </p>
  
  <p>The following code illustrates what I want to do. Note that the code <strong>T.values()</strong> is not valid in the following code.</p>
  
  <pre><code>public class Filter&lt;T&gt; {
      private List&lt;T&gt; availableOptions = new ArrayList&lt;T&gt;();
      private T selectedOption;
  
      public Filter(T selectedOption) {
          this.selectedOption = selectedOption;
          for (T option : T.values()) {  // INVALID CODE
              availableOptions.add(option);
          }
      }
  }
  </code></pre>
  
  <p>Here is how I would instantiate a Filter object:</p>
  
  <pre><code>Filter&lt;TimePeriod&gt; filter = new Filter&lt;TimePeriod&gt;(TimePeriod.ALL);
  </code></pre>
  
  <p>The enum is defined as follows:</p>
  
  <pre><code>public enum TimePeriod {
      ALL("All"), 
      FUTURE("Future"), 
      NEXT7DAYS("Next 7 Days"), 
      NEXT14DAYS("Next 14 Days"), 
      NEXT30DAYS("Next 30 Days"), 
      PAST("Past"),
      LAST7DAYS("Last 7 Days"), 
      LAST14DAYS("Last 14 Days"),
      LAST30DAYS("Last 30 Days"); 
  
      private final String name;
  
      private TimePeriod(String name) {
          this.name = name;
      }
  
      @Override
      public String toString() {
          return name;
      }
  }
  </code></pre>
  
  <p>I realize it might not make sense to copy a enum's values to a list, but I'm using a library that needs a list of values as input and won't work with enums.</p>
  
  <hr>
  
  <p>EDIT 2/5/2010:</p>
  
  <p>Most of the answers proposed are very similar and suggest doing something like this:</p>
  
  <pre><code>class Filter&lt;T extends Enum&lt;T&gt;&gt; {
      private List&lt;T&gt; availableOptions = new ArrayList&lt;T&gt;();
      private T selectedOption;
  
      public Filter(T selectedOption) {
          Class&lt;T&gt; clazz = (Class&lt;T&gt;) selectedOption.getClass();
          for (T option : clazz.getEnumConstants()) {
              availableOptions.add(option);
          }
      }
  }
  </code></pre>
  
  <p>This would work great if I can be sure that selectedOption has a non-null value.  Unfortunately, in my use case, this value is often null, as there is a <strong>public Filter()</strong> no-arg constructor as well. This means I can't do a selectedOption.getClass() without getting an NPE.  This filter class manages a list of available options which of the options is selected. When nothing is selected, selectedOption is null.</p>
  
  <p>The only thing I can think to solve this is to actually pass in a Class in the constructor.  So something like this:</p>
  
  <pre><code>class Filter&lt;T extends Enum&lt;T&gt;&gt; {
      private List&lt;T&gt; availableOptions = new ArrayList&lt;T&gt;();
      private T selectedOption;
  
      public Filter(Class&lt;T&gt; clazz) {
          this(clazz,null);
      }
  
      public Filter(Class&lt;T&gt; clazz, T selectedOption) {
          this.selectedOption = selectedOption;
          for (T option : clazz.getEnumConstants()) {
              availableOptions.add(option);
          }
      }
  }
  </code></pre>
  
  <p>Any ideas how to do this without needing an extra Class parameter in the constructors?</p>