Search code examples
javaif-statementjgrapht

Define if statement conditions by pairs, and on only one parameter of an object


I have created a digraph using the jgrapht library, and my vertices are Point objects:

public static class Point {

  public int x;
  public int y;
  public String type;

  public  Point(int x, int y, String type) 
  {

    this.x = x;
    this.y = y;
    this.type = type;
  }
  @Override
    public String toString() {
    return ("[x="+x+" y="+y+" type="+type+ "]");
  }

  @Override
    public int hashCode() {
    int hash = 7;
    hash = 71 * hash + this.x;
    hash = 71 * hash + this.y;

    return hash;
  }



  @Override
    public boolean equals(Object other) 
  {
    if (this == other)
      return true;

    if (!(other instanceof Point))
      return false;

    Point otherPoint = (Point) other;
    return otherPoint.x == x && otherPoint.y == y;
  }
}

I can go through all my vertices and get the successor of each point like this:*

 for (Point myPoint : directedGraph.vertexSet ()) {
      for (Point successor : Graphs.successorListOf (directedGraph, myPoint)){
 // Add if statement
 }
}

I need to define an if statement's conditions by pairs, but only on one or two of the Point parameters. For example, is these are my points:

public static Point startPoint = new Point(2, 6, "A");
public static Point firstPoint = new Point(2, 7, "A");
public static Point secondPoint = new Point(2, 8, "B");
public static Point thirdPoint = new Point(2, 9, "B");
public static Point fourthPoint = new Point(2, 10, "B");
public static Point fifthPoint = new Point(3, 7, "C");
public static Point sixthPoint = new Point(4, 7, "C");
public static Point seventhPoint = new Point(5, 7, "C");

I'd like to have a condition to be sure that myPoint and successor does not have particular "type":

for (Point myPoint : directedGraph.vertexSet ()) {
          for (Point successor : Graphs.successorListOf (directedGraph, myPoint)){
     if ((myPoint,successor) != (myPoint.type.equals("A"), successos.type.equals("B") && (myPoint,successor)!= (myPoint.type.equals("A"), successos.type.equals("C")){
// Do what I want
      } 
     }
    }

So what I'd need is to put a condition on a couple of points, but only on one parameter of my Point objects [the couple (myPoint and successor) can't be respectively (type A and type B) and (type A and type C).

Is this possible to do it this way ? I couldn't find any resources on this (and had trouble formulating my question so it didn't help)


Solution

  • look into javatuples you could make your own as described in this post https://stackoverflow.com/a/10631692/3944563

    But it may not look pretty. you would be doing something similar to this,

    //define your tuple class
    class myTuple
    {
     final String T1;
     final String T2;
     public myTuple(String t1, String t2)
    {
      T1 = t1;
      T2 = t2;
    }
    
    if(myTupple(myPoint.type, successos.type) == myTupple("A", "B"))
    { /* do something */ }`
    

    anyway, i dont think you can make it pretty, but you get the idea.

    What i would do is add a static method to that class called "validateType" or whatever, and pass the two points and to your check in there