Home arrow static arrow Java Programming [Archive] - how to pass parameter
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how to pass parameter
This topic has 5 replies on 1 page.

Posts:124
Registered: 3/10/01
how to pass parameter   
Jul 19, 2004 5:45 AM



 
Hello,

I've to similar methods that I want to combine them. The are small differences in the methods. Other parts are exactly same.

Here are the samples:

long findWaitMaxEla(CCursor pUnKnownCursor, CWaitKey pWaitKey) {   ...    while (iter.hasNext())     {      mapEntry       = (Map.Entry) iter.next();      iUnKnownCursor = (CCursor)   mapEntry.getValue();       if ((iWait = (CWait) iUnKnownCursor.waitMap.get(pWaitKey))==null) continue;     }   ...  }  long findWaitMaxEla(CCursor pUnKnownCursor, CHistogramKey pHistogramKey) {   ...    while (iter.hasNext())     {      mapEntry       = (Map.Entry) iter.next();      iUnKnownCursor = (CCursor)   mapEntry.getValue();       if ((iHistogramValue = (CHistogramValue) iUnKnownCursor.histogram.waitMap.get(pHistogramKey))==null) continue;     }   ...  }


The differences:

- Second parameters: pWaitKey, pHistogramKey
- Map names: iUnKnownCursor.waitMap, iUnKnownCursor.histogram.waitMap (Please note that iUnKnownCursor is got in the method. I can not pass them as a parameter).
- Returned class names from the maps: CWait, CHistogramValue

How can I combine these 2 methods into one method ?

Thanks in advance...

 

Posts:319
Registered: 11/8/00
Re: how to pass parameter   
Jul 19, 2004 6:00 AM (reply 1 of 5)



 
This is one way, i'm sure there will be more though

long findWaitMaxEla(CCursor pUnKnownCursor, Object pSecondParam) {   ...    while (iter.hasNext())     {      mapEntry       = (Map.Entry) iter.next();      iUnKnownCursor = (CCursor)   mapEntry.getValue();      if ((CWait.class).isInstance(pSecondParam))     {           if ((iWait = (CWait) iUnKnownCursor.waitMap.get(pWaitKey))==null) continue;      }     else if ((CHistogramKey.class).isInstance(pSecondParam))     {          if ((iHistogramValue = (CHistogramValue)                                          UnKnownCursor.histogram.waitMap.get(pHistogramKey))==null) continue;     }      }   ...  }
 

Posts:124
Registered: 3/10/01
Re: how to pass parameter   
Jul 19, 2004 11:32 PM (reply 2 of 5)



 
Thanks,

It's better since it's a single method. But, in your case, I've to use two different classes (CWait,CHistogramKey) and I've to check them. is there another way ?
 

Posts:6,750
Registered: 1/25/04
Re: how to pass parameter   
Jul 20, 2004 8:46 AM (reply 3 of 5)



 
What's in the ... portions of the methods? It's impossible to tell how to rewrite them correctly with only a part of the methods.
 

Posts:1,695
Registered: 1/13/04
Re: how to pass parameter   
Jul 20, 2004 8:55 AM (reply 4 of 5)



 
Put the get() method of your 2 classes in an interface, then pass the interface as parameter of the method... that should do it.
 

Posts:441
Registered: 2/25/04
Re: how to pass parameter   
Jul 20, 2004 10:11 AM (reply 5 of 5)



 
Assuming that iWait and iHistogramValue are fields rather than variables, and are not used elsewhere in the methods, then something like this would work:
private interface Lambda {  Object apply (XXX self, CCursor iUnKnownCursor, Object key) ;} private static Lambda iHistogramValueLambda = new Lambda(){  public Object apply (XXX self, CCursor iUnKnownCursor, Object key) {   return self.iHistogramValue  = (CHistogramValue)iUnKnownCursor.histogram.waitMap.get(key);  }} private static Lambda iWaitLambda = new Lambda(){  public Object apply (XXX self, CCursor iUnKnownCursor, Object key) {    return self.iWait = (CWait) iUnKnownCursor.waitMap.get(key)  }} long findWaitMaxEla(CCursor pUnKnownCursor, Object pKey, Lambda lambda) {   ...     while (iter.hasNext())     {       mapEntry       = (Map.Entry) iter.next();       iUnKnownCursor = (CCursor)   mapEntry.getValue();        if (lambda.apply(this, iUnKnownCursor, pkey)==null) continue;     }   ...  } long findWaitMaxEla(CCursor pUnKnownCursor, CWaitKey pWaitKey)  return findWaitMaxEla(pUnKnownCursor, pWaitKey, iWaitLambda);} long findWaitMaxEla(CCursor pUnKnownCursor, CHistogramKey pHistogramKey) {  return findWaitMaxEla(pUnKnownCursor, pHistogramKey, iHistogramValueLambda);} 
Pete
 
This topic has 5 replies on 1 page.