开发者

using comparators as embedded classes, the mother class (initialized in clone ) fields are not visible in the compare method

开发者 https://www.devze.com 2023-01-15 03:55 出处:网络
I have developed a server-to-server protocol called CCMN and different drop policies for the messages cached by each server. The PEERSIM simulator creates a template node with the CCMN protocol and th

I have developed a server-to-server protocol called CCMN and different drop policies for the messages cached by each server. The PEERSIM simulator creates a template node with the CCMN protocol and then clones this template nod开发者_Go百科e.

The CCMN class includes different data structures maintaining the states required to implement the drop policies. For instance, Pmap resolves a content identifier to a content object, and freq a content identifier to the number of times a content object has been used.

public HashMap <String, Content> Pmap;
public HashMap<String, Integer> freq;

The initialization of these data structures is done in the clone method of CCMN. In order to support the drop policies, I use a priorityQueue initialized with a comparator corresponding to the policy to use. In the clone method:

if(dp.equals(DroppingPolicy.LFU)){

lfu less_frequent=new lfu(); av.raw_drop=new PriorityQueue(1, less_frequent); } The comparator are declared as follows:

public class lfu implements Comparator<String> {

public int compare(String s0, String s1) { if(freq.get(s0) > freq.get(s1)) return 1; else if(freq.get(s0) < freq.get(s1)) return -1; else return 0; } }

The problem is that I get a NullPointerException exception when i call :

raw_drop.add(ct.getID());

By debugging, I found that the add works for the first element, but the exception occurs when a second element is added. I traced the exception to the compare method, by checking the variables I find that the freq variable (and the other variables initialized in the clone method) equals null, while the variables defined in the constructor are well defined.

Moreover, I check that freq is well initialized before the add call.

I guess that there is a problem between the definition of the comparators as embedded classes and the clone method.

Any help is welcome.

Regards, Mohamed


Ok, but this will not change anything to the problem, because the problem is that freq is not initialized in the compare method...


I have fixed the problem. In fact, the nested comparators referenced the template instance instead of the cloned instance. I add a counter so that the first time, the method the instruction raw_drop.add(ct.getID()) belongs to, is called the priorityQueue used by the instance is initialized in order to have the right initialization.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号