Current Position:Home > How to pass parameter

How to pass parameter

Update:10-11Source: network consolidation
Advertisement
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...

The Best Answer

Advertisement
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