I'm writing a simple distributed java rmi application and I have a bunch of methods that each need to iterate through a map of client interfaces in order to call various other methods on those interfaces, like so:
public void methodX (arg1, arg2) {
Iterator<String> itr = clients.keySet().iterator;
while (itr.hasNext()) {
String name = itr.next();
if (!"mod".equals(name)) {
try {
clients.get(name).methodXX(arg1, arg2);
} catch(RemoteException ex) {
// do something
}
}
}
}
public void methodY (arg1, arg2, arg3) {
Iterator<String> itr = clients.keySet().iterator;
while (itr.hasNext()) {
String name = itr.next();
if (!"mod".equals(name)) {
try {
clients.get(name).methodYY(arg1, arg2, arg3);
} catch(RemoteException ex) {
// do something
}
}
}
}
Now I modified these so that instead they each call a single method doAll by passing a new argument called MESSAGE_TYPE, like so:
public void methodX (arg1, arg2) {
doAll(MESSAGE_TYPE.METHODX, arg1, arg2, null);
}
public void methodY (arg1, arg2, arg3) {
doAll(MESSAGE_TYPE_METHODY, arg1, arg2, arg3);
}
And the doAll method:
public void doAll(msg_type, arg1, arg2, arg3) {
Iterator<String> itr = clients.keySet().iterator;
while (itr.hasNext()) {
String name = itr.next();
if (!"mod".equals(name)) {
try {
switch(msg_type) {
case METHODX:
clients.get(name).methodXX(arg1, arg2);
break;
case METHODY:
clients.get(name).methodYY(arg1, arg2, arg3);
break;
}
} catch(RemoteException ex) {
// do something
}
}
}
}
Now there are many more methods like this, and so my doAll method needs to take a bunch of args and each methodXX that calls it pass a bunch of nulls to it.
Can I rewrite this so it's more concise? If so, can you provide an example?