fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7.  
  8. class CellHelper<T> {
  9. final T[] mValues;
  10. final Class<T> cls;
  11. public CellHelper(T[] values, Class<T> c) {
  12. mValues = values;
  13. cls = c;
  14. }
  15. public T getCell(int index) {
  16. if (index < mValues.length ) {
  17. return mValues[index];
  18. }
  19. throw new IllegalArgumentException("Invalid " + cls.getSimpleName() + " value: " + index);
  20. }
  21. }
  22.  
  23. enum SomeEnumClass {
  24. ONE(1), TWO(2), THREE(3);
  25. SomeEnumClass(int n){}
  26. private static final CellHelper<SomeEnumClass> helper = new CellHelper(SomeEnumClass.values(), SomeEnumClass.class);
  27. public static SomeEnumClass getCell(int i) {return helper.getCell(i);}
  28. }
  29.  
  30. enum OtherEnumClass {
  31. Monday(1), Tuesday(2), Wednesday(3), Thrusday(4), Friday(5), Saturday(6), Sunday(7);
  32. OtherEnumClass(int n){}
  33. private static final CellHelper<OtherEnumClass> helper = new CellHelper(OtherEnumClass.values(), OtherEnumClass.class);
  34. public static OtherEnumClass getCell(int i) {return helper.getCell(i);}
  35.  
  36.  
  37. }
  38.  
  39. /* Name of the class has to be "Main" only if the class is public. */
  40. class Ideone
  41. {
  42. public static void main (String[] args) throws java.lang.Exception
  43. {
  44. System.out.println(SomeEnumClass.getCell(1));
  45. System.out.println(OtherEnumClass.getCell(1));
  46. }
  47. }
Success #stdin #stdout 0.04s 320576KB
stdin
Standard input is empty
stdout
TWO
Tuesday