Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 3rd, 2012  |  syntax: None  |  size: 1.47 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Converting this function to using generics
  2. public static <T extends Enum<T> & SomeInterface> EnumSet<T> convertToES(long val) {
  3.   EnumSet<T> es = EnumSet.noneOf(T.class);
  4.  
  5.   for(T t : values()) {
  6.       if(t.getSomeProperty() > 0)
  7.         es.add(t);
  8.   }
  9.  
  10.  
  11.   return es;
  12. }
  13.        
  14. public static EnumSet<SomeType> convertToES(long val) {
  15.         EnumSet<SomeType> es = EnumSet.noneOf(SomeType.class);
  16.  
  17.         for (SomeType p : values()) {
  18.             if (p.getSomeThing())) > 0) {
  19.                 es.add(p);
  20.             }
  21.         }
  22.  
  23.         return es;
  24.     }
  25.        
  26. public static <T extends Enum<T> & SomeInterface> EnumSet<T> convertToES(
  27.     long val,
  28.     Class<T> tClass)
  29. {
  30.     EnumSet<T> es = EnumSet.noneOf(tClass);
  31.        
  32. for(T t : tClass.getEnumConstants()) {
  33.         ....
  34.        
  35. public static <T extends Enum<T> & SomeInterface> EnumSet<T> convertToES(Class<T> clazz, long val) {
  36.  
  37.     EnumSet<T> es = EnumSet.noneOf(clazz);
  38.  
  39.     for(T t : clazz.getEnumConstants()) {
  40.         if(t.getSomeProperty() > 0)
  41.           es.add(t);
  42.     }
  43.     return es;
  44.   }
  45.        
  46. public static <T extends Enum<T> & SomeInterface> //
  47.         EnumSet<T> convertToES(Class<T> tClass, long val) {
  48.     EnumSet<T> set = EnumSet.noneOf(tClass);
  49.     for (T t : EnumSet.allOf(tClass)) {
  50.         if (t.getSomeProperty() == val) {
  51.             set.add(t);
  52.         }
  53.     }
  54.     return set;
  55. }
  56.        
  57. EnumSet<Foo> x = convertToES(Foo.class, 3);
  58.        
  59. for(Foo suitableFoo: convertToES(Foo.class, 5)) {
  60.     print(suitableFoo + " has a property of 5!");
  61. }