Flippy
Flippy

Reputation: 221

Avoid multiple adding of content to Generics List

I create custom Nodes with one circle on the left and one on the right. Everytime I connect two Nodes, I want to set Boolean left or Boolean right to true. When I connect two Nodes to one Node, I want to set both Boolean right and left to true. Currently I add mutliple entries without updating my Generics content.

My Code:

public static List<NodeList<Boolean,Boolean,String>> connectList = new ArrayList<NodeList<Boolean,Boolean,String>>();
private String linkedNodeID;
private Boolean right = false;
private Boolean left = false;

and the actual Code and here is where I make the actual mistake by adding always inside leftTrue()/rightTrue():

public void leftTrue(){
    left = true;
    connectList.add(new NodeList<Boolean, Boolean, String>(left, right, linkedNodeID));
}
public void rightTrue(){
    right = true;
    connectList.add(new NodeList<Boolean, Boolean, String>(left, right, linkedNodeID));
}

What I got is:

right connected: false ,left connected: true ,ID: 81def74e-4cb6-428c-b7ca-a57279534814
right connected: true ,left connected: false ,ID: 46514df2-da7a-41e0-bdd7-43e6f15b4220
right connected: true ,left connected: true ,ID: 46514df2-da7a-41e0-bdd7-43e6f15b4220
right connected: true ,left connected: false ,ID: 2e85f320-43cf-4d83-8670-4e917f31cd25

and what I want is:

right connected: false ,left connected: true ,ID: 81def74e-4cb6-428c-b7ca-a57279534814
right connected: true ,left connected: true ,ID: 46514df2-da7a-41e0-bdd7-43e6f15b4220
right connected: true ,left connected: false ,ID: 2e85f320-43cf-4d83-8670-4e917f31cd25

Any ideas how to set the left/right connect without adding another entry to my list?

EDIT:

public void registerSource(String linkId, DragNode linkNode) {
    linkedNodeID = linkNode.getId();
    double nodeWidth1 = linkNode.getWidth()/2;
    if(DragNode.linkNodeOneX <= nodeWidth1){
        leftTrue();
    }else{
        rightTrue();
    }
}
public void registerTarget(String linkId, DragNode linkNode) {
    linkedNodeID = linkNode.getId();
    double nodeWidth2 = linkNode.getWidth()/2;
    if( DragNode.linkNodeTwoX < nodeWidth2){
        leftTrue();
        testprint();
    }else{
        rightTrue();
    }
}

EDIT2:

My problem is that when ever I call leftTrue(); I create a new connectList.add(new NodeList<Boolean, Boolean, String>(left, right, linkedNodeID)); entry, but if a Id is already in my List, it should update left or right. Any ideas? Like connectList.get(i).getL(); and then if this equals another Id just set right/false = true;

EDIT3:

public void leftTrue(){ 
    left = true;
}

public void rightTrue(){
    right = true;
}
public void registerSource(String linkId, DragNode linkNode) {
    linkedNodeID = linkNode.getId();
    double nodeWidth1 = linkNode.getWidth()/2;
    if(DragNode.linkNodeOneX <= nodeWidth1){
        leftTrue();
    }else{
        rightTrue();
    }
    if(left || right){
      connectList.add(new NodeList<Boolean, Boolean, String>(left, right, linkedNodeID));
      left = false;
      right = false;
    }
}
public void registerTarget(String linkId, DragNode linkNode) {
    linkedNodeID = linkNode.getId();
    double nodeWidth2 = linkNode.getWidth()/2;
    if( DragNode.linkNodeTwoX < nodeWidth2){
        leftTrue();
    }else{
        rightTrue();
    }
    if(left || right){
          connectList.add(new NodeList<Boolean, Boolean, String>(left, right, linkedNodeID));
          left = false;
          right = false;
    }
}

Upvotes: 0

Views: 117

Answers (2)

Flippy
Flippy

Reputation: 221

I made it to work by updating the leftTrue() and rightTrue() methods:

public void leftTrue(){ 
    left = true;
    //if the function is rightTrue() then set right = true;
    if(left && right){
        String testID = self.getId();
        for(int i = 0; i < connectList.size(); i++){
            String testID2 = connectList.get(i).getL();
            if(testID == testID2){
                left = true;
                right = true;
                connectList.get(i).setT(left);
                connectList.get(i).setI(right);
            }
        }
    }else{
        connectList.add(new NodeList<Boolean, Boolean, String>(left, right, linkedNodeID));
    }
}

Then my output only has three unique ID's with correct left/right:

right connected: false ,left connected: true ,ID: 6036bf0c-c514-454e-abbb-c9298800a585, listsize 3
right connected: true ,left connected: true ,ID: b78abcb8-a940-44e7-9429-11006b54dc79, listsize 3
right connected: true ,left connected: false ,ID: b3188490-2183-44ee-8fc6-5af0309766a8, listsize 3

Upvotes: 0

Glen Pierce
Glen Pierce

Reputation: 4831

Store those left and right values and use them only once to create the new node after determining if their is left, right, or both.

if( DragNode.linkNodeTwoX < nodeWidth2){
    leftTrue();
}else{ 
    rightTrue(); 
}
if(left || right){
  connectList.add(new NodeList<Boolean, Boolean, String>(left, right, linkedNodeID));
  left = false;
  right = false;
}


public void leftTrue(){ 
  left = true; 
} 
public void rightTrue(){ 
  right = true; 
} 

Upvotes: 1

Related Questions