Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplifying Java method with variable amount of arguments

Working in java, I was wanting to simplify a draw function (polygon creator) that I am working with. Typically, when you create a polygon, you do this:

Polygon mypoly = new Polygon();
mypoly.addPoint(x1, y1);
mypoly.addPoint(x2, y2);
mypoly.addPoint(x3, y3);
Draw.fillPolygon(g, mypoly, Color.blue);

I would like to use an image mapper to automatically give me the coordinates, so I could just copy paste them into my own function.

myCommand(x1, y1, x2, y2, x3, y3);

Each of these would go into the polygon command on the top. The problem that I am facing though is that when mypoly is created, how would it know how many points to add and where to put them?

I am trying to get myCommand to automatically add points as I add arguments, and each point to correspond with the x,y of the original polygon creation method.

like image 960
nomnomnomplant Avatar asked Dec 21 '12 14:12

nomnomnomplant


4 Answers

Sounds like you need to make use of the builder pattern. In pseudocode:

PolygonBuilder pb = new PolygonBuilder();
pb.addPoint(1,1);
pb.addPoint(1,2);
// etc...

Polygon p = pb.newPolygon();

so the idea is that you provide the builder with a set of points, and it'll generate you the appropriate polygon. Builders are often designed with a fluent interface. Note that the builder can act like a factory and return you appropriate subclasses of Polygon (square, triangle, pentagle etc. if you so wish).

Note that you could instead provide a method that takes a variable number of arguments, using the Java varargs mechanism. e.g.

public void addPoints(Integer... args) {
   // and iterate here
}

You may wish to create a Point object to define an x/y coordinate together. Otherwise the above will have to check for an even number of arguments, and those arguments won't be tied together.

like image 98
Brian Agnew Avatar answered Oct 14 '22 09:10

Brian Agnew


You can use varargs and create the polygon dynamically by using the constructor that gets arrays of xs and ys

(Code not tested)

public Polygon createPolygon(int... points) {
    if (0 != points.length % 2) {
        throw new IllegalArgumentException("Must have even number of points");
    }

    int numOfPoints = points.length / 2;
    int xs = new int[numOfPoints];
    int ys = new int[numOfPoints];
    for (int i=0; i < numOfPoints;i++) {
       xs[i] = points[i*2];
       yx[i] = points[i*2 + 1];
    }

    return new Polygon(xs, ys, numOfPOints);
}

Then you can invoke the method with any number of points

Polygon p = createPolygon(x1,y1,x2,y2,x3,y3);

like image 41
Aviram Segal Avatar answered Oct 14 '22 10:10

Aviram Segal


To extend Brian Agnew's answer, it might also be worth adding a Point class which the addPoints method could take in. It could make it slightly easier to add/remove points from your polygon.

public final class Point<X,Y>{
    private final X x;
    private final Y y;

    public Point(X x, Y y){
        this.x=x;
        this.y=y;
    }

    public X getX(){return x;}

    public Y getY(){return y;}
}

Then you could have a:

public void addPoints(Point<Integer,Integer>... points){
    for(Point<Integer,Integer> point:points)
        //your logic
}
like image 2
John Kane Avatar answered Oct 14 '22 10:10

John Kane


I think you could use a method that received a varargs (...)

You need a wrapper for each point:

class Point {
    int x;
    int y;
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

The method could be:

myCommand(Point ... points)

For call

myCommand(new Point(0,0), new Point(1,1), new Point(0,1));

And for draw:

Polygon mypoly = new Polygon();
for(Point p : points)
    mypoly.addPoint(p.x,p.y);
Draw.fillPolygon(g,mypoly,Color.blue);
like image 1
Paul Vargas Avatar answered Oct 14 '22 08:10

Paul Vargas