Using wild cards with static classes in java -


the idea i'm going have bunch of actions/functions happen in our program. they're predefined , separated categories. there might category admin actions defines bunch of static codes actions in admin actions category.

since categories , actions fixed, they're in static classes.

these static category classes implement interface, icategory:

public static interface icategory{     int getcateogory();     string getcategoryname();     string getfunctionname(int function); } 

each of these static classes added static map:

private static map<integer, class<? extends icategory>> catmap = new hashmap<integer, class<? extends icategory>>(); 

basically there's integer code associated each category. i'm trying made human readable string can print out when receive category , action codes.

icategory whatever = catmap.get(catnumber); system.out.println(whatever.getcategoryname()); system.out.println(whatever.getfunctionname(actioncode)); 

so catmap.get(catnumber) return proper static class, don't know how can use returned class access these static methods. can regular instances of class, fine, doing static classes has got me puzzled.

clarification of problem:

some clarification of problem i'm trying solve in case guys have suggestions of better / more intuitive ways this:

basically i'm interpreting commands piece of custom hardware @ company. it's little data collection gizmo has bunch of predefined messages/functions have interpret.

these functions split various categories: display, keypad, acquisition, etc.

so have mapping this:

  display category: 128       showgraph: 01       showtext: 02   keypad category: 129       f1: 01       f2: 02       menukey: 03 

i'm making little stream display prints stream of commands out in human readable format. i'd print out big list of like

got category display, function showgraph got category keypad, function menukey 

normally i'd use map this, want use functions in each category constants because i'll have reference them in if-statements , times send same categories little gizmo.

for instance:

sendmessage(categories.displaycategory.getcategoryint(), categories.displaycategory.show_graph); 

more code requested:

public class functions {      public static interface icategory{         int getcateogory();         string getcategoryname();         string getfunctionname(int function);     }      private static map<integer, class<? extends icategory>> catmap = new hashmap<integer, class<? extends icategory>>();      public static string getcategorystring(int category) {         class<? extends icategory> clazz = catmap.get(category);         system.out.println(catmap.tostring());          if(clazz != null){             try{                 method m = clazz.getmethod("getcategoryname", integer.class);                 return (string) m.invoke(0, category);             }catch (exception e){                 return null;             }          }else{             system.out.println("clazz null");             return null;         }     }      public static class systemkey implements icategory{         public static int category = 134;         private static map<integer, string> fmap = new hashmap<integer, string>();         @override         public int getcateogory() {             return category;         }          @override         public string getcategoryname() {             return "systemkey";         }          @override         public string getfunctionname(int function) {             return fmap.get(function);         }     }      public static class systemcat implements icategory{         public static int category = 128;         private static map<integer, string> fmap = new hashmap<integer, string>();          public static final int power_up = 0x01;         public static final int end_of_transmit = 0x02;         public static final int clear_to_send = 0x03;         public static final int net_test = 0x05; /*fom station ctrlr*/          public static final int net_ok = 0x06; /*response controller*/         public static final int main_menu = 0x07;          static{             catmap.put(category, systemcat.class);              fmap.put(power_up, "power_up");              fmap.put(end_of_transmit, "end_of_transmit");              fmap.put(clear_to_send, "clear_to_send");             fmap.put(net_test, "net_test");              fmap.put(net_ok, "net_ok");              fmap.put(main_menu, "main_menu");         }           @override         public int getcateogory() {             return category;         }          @override         public string getcategoryname() {             return "system";         }          @override         public string getfunctionname(int function) {             return fmap.get(function);         }     }         public static class softkey implements icategory{         public static int category = 129;          private static map<integer, string> fmap = new hashmap<integer, string>();          public static final int f1 = 0x20;         public static final int f2 = 0x21;         public static final int f3 = 0x22;         public static final int f4 = 0x23;         public static final int f5 = 0x24;          static{             catmap.put(category, softkey.class);              fmap.put(f1, "f1");             fmap.put(f2, "f2");             fmap.put(f3, "f3");             fmap.put(f4, "f4");             fmap.put(f5, "f5");          @override         public int getcateogory() {             return category;         }          @override         public string getcategoryname() {             return "softkey";         }          @override         public string getfunctionname(int function) {             return fmap.get(function);         }      }       public static void main (string[] args) throws exception{            system.out.println(functions.getcategorystring(128));     }  } 

update

as suspected, solution quite simple. there different ways this, here one, seem remember calling registry, in days when patterns known idioms. there, need following changes:

  1. change catmap type map<string,class<? extends icategory> map<integer, icategory>.

  2. in static initializers create object , put in map, e.g.

    public static class softkey implements icategory{ ....

    static{     catmap.put(category, new softkey()); 
  3. in getcategorystring use icategory object in registry:

    icategory categ = catmap.get(category); return categ.getcategoystring()


i might have misunderstood question, part of confusing:

so catmap.get(catnumber) return proper static class,

by static class assume mean interfaces nested inside class/interface. there no such thing top-level static class in java. get returns object of static class, not class.

but don't know how can use returned class access these static methods.

the methods have declared not static, instance methods

i can regular instances of class, fine, doing static classes has got me puzzled.

i puzzled too. can call instance methods on objects of static class. can post complete code sample?


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -