refactoring java code (1)

hi all,

I have a little trouble to refactoring to function from a same class

there the 2 functions

public Collection getRangeMarkers(int index, Layer layer){

Collection result =null;

Integer key =new Integer(index);

if (layer == Layer.FOREGROUND){

result = (Collection) this.foregroundRangeMarkers.get(key);

}

elseif (layer == Layer.BACKGROUND){

result = (Collection) this.backgroundRangeMarkers.get(key);

}

if (result !=null){

result = Collections.unmodifiableCollection(result);

}

return result;

}

public Collection getDomainMarkers(int index, Layer layer){

Collection result =null;

Integer key =new Integer(index);

if (layer == Layer.FOREGROUND){

result = (Collection) this.foregroundDomainMarkers.get(key);

}

elseif (layer == Layer.BACKGROUND){

result = (Collection) this.backgroundDomainMarkers.get(key);

}

if (result !=null){

result = Collections.unmodifiableCollection(result);

}

return result;

}

there is how there are declared

this.foregroundDomainMarkers =new HashMap();

this.backgroundDomainMarkers =new HashMap();

this.foregroundRangeMarkers =new HashMap();

this.backgroundRangeMarkers =new HashMap();

as we can see it's just a name who change, the behavior is exacly the same it's the data structure who change.

In c++ we can use template, in java?

it's not just two function, I have that all the time (and on big function).

thanks

[2820 byte] By [elekisa] at [2007-11-26 22:48:35]
# 1

You could pass in the foreground and background markers as

parameters to a single method:public Collection getMarkers(int index, Layer layer, ? f, ? b) {

Collection result = null;

Integer key = new Integer(index);

if (layer == Layer.FOREGROUND) {

result = (Collection) f.get(key);

}

else if (layer == Layer.BACKGROUND) {

result = (Collection) b.get(key);

}

if (result != null) {

result = Collections.unmodifiableCollection(result);

}

return result;

}

Not knowing the type of the markers, I simply substituted '?' for their

types.

kind regards,

Jos

JosAHa at 2007-7-10 12:08:28 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks, sory for the type (bad ctrl-c-v) it's mapa++
elekisa at 2007-7-10 12:08:28 > top of Java-index,Java Essentials,Java Programming...