Categories
discuss

Java: When to use attributes, when to use method parameters?

I tried googling and searching for this question but somehow couldn’t find anything relevant about it. I’m wondering if there is a bbest-practise guide on when to use attributes in a class and when not, but rather use parameters to the single methods.

Many cases are clear to me, e.g.

public class Dog
{
 private name;
 public setName(...) {....}
}

But sometimes it’s not clear to me what’s better to use. E.g. the following, either use:

public class calculation
  XYZ bla;
  public calculation(XYZ something)
  {
    this.bla = something;
  }
  public void calc1()
  {
    // some calculations with this.bla
  }
  public void calc1()
  {
    // some more calculations with this.bla
  }
  public XYZ getBla()
  {
    return this.bla;
  }
}

or maybe do:

public class calculation
  public calculation() {}
  public static XYZ calc1(XYZ bla) // maybe static, if not dependant on other attributes/instance-variables etc
  {
    // some calculations with bla
    return bla;
  }
  public static XYZ calc1() // maybe static, if not dependant on other attributes/instance-variables etc
  {
    // some more calculations with bla
    return bla;
  }
}

I mean you can argue for both cases. I see advantages and maybe disadvantages for both different styles, but somehow I prefer the second one as far as long as there are not too many arguments/parameters needed. Sure, if I need many many more attributes etc., then the first one will be better, simpler etc. because I dont need to pass so many parameters to the method…

Just a question of personal style? Or how to decide for one approach? Thanks


EDIT: A better example: I’m curently doing much image processing and the question would be wether to store the image internally in the state of the object or not. I’m currently NOT doing it because I’m using static methods, and psasing the image itself I to each method:

public class ImageProcessing
{
    /**
     * 
     */
    public static Mat cannyEdges(Mat I, int low, int high)
    {      
        // ...
        return I;
    }
    public static Mat cannyEdges(Mat I)
    {
        return ImageProcessing.cannyEdges(I, ContourDetection.CANNY_LOWTHRES, ContourDetection.CANNY_HIGHTHRES);
    }

    /**
     * 
     */
    public static Mat getHoughLines(Mat Edges, ...some_conf_vars...)
    {
        // ...
        return I;
    }
}

and then I’m calling it from the outside like this e.g.:

// here: read image to I...
Mat edges = ImageProcessing.cannyEdges(I, 20, 100);
Mat lines = ImageProcessing.getHoughLines(I);

// draw lines...

question is: Does I belong to the state of the object? Would it make sense to convert to non-static and then use for example:

// here: read image to I...
ImageProcessing IP = new ImageProcessing(I);
IP.cannyEdges(20, 100); // CHANGE OF cannyEdges: Also save `edges` internally as property!?
IP.calcHoughLines(); // also save the lines internally maybe?
Mat lines = IP.getLines(); 

// draw lines...

is this nicer? The question arising is then again: Should I for example store the result of getHoughLines() (i.e. the lines) internally or should I directly return it to the caller!?

Answer

There are a few reasons I’d go with the first option, i.e. an object with state over static functions, particularly for complex calculations but also for simpler ones.

  1. Objects work better for the command pattern.
  2. Objects work better for the strategy pattern.
  3. Static methods can turn unit tests into a nightmare.
  4. Static is an anti-pattern in OOP because it breaks polymorphism, with the side-effect that related techniques will break with it, e.g. open/closed, mocking, proxies, etc.

That’s my 2c at least.

The weird part of your first example is that those calcX methods don’t say anything about idempotency, so it’s unclear what this.bla is when it’s being manipulated. For complex computations with optional settings, an alternative is to construct an immutable object using a builder pattern, and then offer calcX methods that return the result based on fixed object state and parameters. But the applicability of that really depends on the use case, so YMMV.

Update: With your new code, a more OOP approach would be to decorate Mat. Favouring delegation over inheritance, you’d get something like

public class MyMat
{
    private Mat i;

    public MyMat(Mat i) {
       this.i = i;
    }

    public Mat getBackingMat() {
       return this.i;
    }

    public MyMat cannyEdges(int low, int high)
    {      
        // ...
        return new MyMat(I); // lets you chain operations
    }

    public MyMat cannyEdges()
    {
        return new MyMat(ImageProcessing.cannyEdges(I, ContourDetection.CANNY_LOWTHRES, ContourDetection.CANNY_HIGHTHRES));
    }

    public MyMat getHoughLines(...some_conf_vars...)
    {
        // ...
    }
}

MyMat myMat = new MyMat(I);
lines = myMat.cannyEdges(20, 100).calcHoughLines();

This is just a guess, cause I have no idea what those things mean. 🙂

Categories
discuss

What’s a good way to enforce a single rate limit on multiple machines?

I have a web service with a load balancer that maps requests to multiple machines. Each of these requests end up sending a http call to an external API, and for that reason I would like to rate limit the number of requests I send to the external API.

My current design:

  • Service has a queue in memory that stores all received requests
  • I rate limit how often we can grab a request from the queue and process it.

This doesn’t work when I’m using multiple machines, because each machine has its own queue and rate limiter. For example: when I set my rate limiter to 10,000 requests/day, and I use 10 machines, I will end up processing 100,000 requests/day at full load because each machine processes 10,000 requests/day. I would like to rate limit so that only 10,000 requests get processed/day, while still load balancing those 10,000 requests.

I’m using Java and MYSQL.

Answer

The two things you stated were:

1)"I would like to rate limit so that only 10,000 requests get processed/day"
2)"while still load balancing those 10,000 requests."

First off, it seems like you are using a divide and conquer approach where each request from your end user gets mapped to one of the n machines. So, for ensuring that only the 10,000 requests get processed within the given time span, there are two options:

1) Implement a combiner which will route the results from all n machines to
another endpoint which the external API is then able to access. This endpoint is able to keep a count of the amount of jobs being processed, and if it’s over your threshold, then reject the job.

2) Another approach is to store the amount of jobs you’ve processed for the day as a variable inside of your database. Then, it’s common practice to check if your threshold value has been reached by the value in your database upon the initial request of the job (before you even pass it off to one of your machines). If the threshold value has been reached, then reject the job at the beginning. This, coupled with an appropriate message, has an advantage as having a better experience for the end user.

In order to ensure that all these 10,000 requests are still being load balanced so that no 1 CPU is processing more jobs than any other cpu, you should use a simple round robin approach to distribute your jobs over the m CPU’s. With round robin, as apposed to a bin/categorization approach, you’ll ensure that the job request is being distributed as uniformly as possible over your n CPU’s. A downside to round robin, is that depending on the type of job you’re processing you might be replicating a lot data as you start to scale up. If this is a concern for you, you should think about implementing a form of locality-sensitive hash (LSH) function. While a good hash function distributes the data as uniformly as possible, LSH exposes you to having a CPU process more jobs than other CPU’s if a skew in the attribute you choose to hash against has a high probability of occurring. As always, there’s tradeoffs associated with both, so you’ll know best for your use cases.

Categories
discuss

When should we create our own Java exception classes? [closed]

From a good design/practice point of view, when should we create and use custom Java exception classes instead of the ones already predefined in Java?

In some applications I see almost custom exception classes created, they make an effort to always use native Java exceptions. On the other hand, there are some applications that define custom exceptions for (almost) everything.

Answer

From Best Practices for Exception Handling:

Try not to create new custom exceptions if they do not have useful information for client code.

What is wrong with the following code?

public class DuplicateUsernameException extends Exception {}

It is not giving any useful information to the client code, other than an indicative exception name. Do not forget that Java Exception classes are like other classes, wherein you can add methods that you think the client code will invoke to get more information.

We could add useful methods to DuplicateUsernameException, such as:

public class DuplicateUsernameException
    extends Exception {
    public DuplicateUsernameException 
        (String username){....}
    public String requestedUsername(){...}
    public String[] availableNames(){...}
}

The new version provides two useful methods: requestedUsername(), which returns the requested name, and availableNames(), which returns an array of available usernames similar to the one requested. The client could use these methods to inform that the requested username is not available and that other usernames are available. But if you are not going to add extra information, then just throw a standard exception:

throw new IllegalArgumentException("Username already taken");
Categories
discuss

Amazon S3 Java SDK multiple files upload

What I need is to download many files from Internet and upload them to S3 as fast as it possible. For now I open every file as stream and upload it one after another. Average speed is 1 picture per second =(

Any ideas?

Answer

Split in parts and upload in parallel would be a good start, the TransferManager class can help with that.
Also, the article “Pushing the Limits of S3 Upload Performance” has some nice ideas on the subject.

Categories
discuss

Reorder four points of a rectangle to the correct order

The aspect-ratio=height/width is always >1 (for most cases even >2), so it should be clear/precise how I’d like to rotate.


I have a RotatedRect object in OpenCV / Java.
I can get an Array of it with 4 Objects of Type Point and Point defines x/y-values.

Now I’d like to sort those 4 Points so that the top-left Point is the first element of the Array and then clockwise, so that the top-bottom point is the fourth element.

I’m assuming that the Rectangles aren’t rotated much (just some small angles), e.g.

I’ve indicated in the example which point I’d refer to as top left (TL).

How to do it?

You don’t need to tell me specifically for OpenCV etc., just assume you’d have two arrays

int[] x = new int[4];
int[] y = new int[4];

Whereas the n-th Point has the coordinates (x[n-1], y[n-1]). I can then do it for OpenCV specifically myself.

Answer

Answer

There is an extremely easy solution if you know that:

  1. -45 < roundedRect.angle < 45
  2. roundedRect.size.height > roundedRect.size.width

If that is true, then the points, in clockwise order, will ALWAYS be in this order:

pts[0], pts[3], pts[2], pts[1]

As an aside, if it doesn’t harm your program too much, the points are delivered in counterclockwise order, starting with the top left… then you wouldn’t have to do any reordering / sorting.

Other cases:

  • height > width && 135 < roundedRect.angle < 225
    • The clockwise order starting from the top left is 2,3,0,1
    • The counterclockwise order from top left is 2,1,0,3.
  • width > height && -135 < roundedRect.angle < -45
    • The clockwise order starting from the top left is 3,2,1,0
    • The counterclockwise order from top left is 3,0,1,2
  • width > height && 45 < roundedRect.angle < 135
    • The clockwise order starting from the top left is 1,0,3,2
    • The counterclockwise order from top left is 1,2,3,0

The remaining cases would all imply that the rectangle is bigger from left to right than it is from top to bottom, which cannot happen in your scenario. Also, if the angle is outside these ranges, you can add or subtract 360 successively to get an angle in one of these ranges.


Explanation

(tl;dr)

We know this from how OpenCV calculates the values of those points. You can figure this out with a little experimentation. Here’s a little program I wrote that demonstrates it:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.opencv.core.Point;
import org.opencv.core.RotatedRect;
import org.opencv.core.Size;

public class TestFrame extends JFrame {
    public static void main(String... args) {
        final TestFrame frame = new TestFrame();
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
            }
        });
    }

    private RectComponent rect;

    public TestFrame() {
        JPanel containerPane = new JPanel(new BorderLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        rect = new RectComponent();
        containerPane.add(rect);
        setContentPane(containerPane);
        setSize(400,400);
        new Timer(100, rect).start();
    }

    public class RectComponent extends JComponent implements ActionListener {
        private RotatedRect rect = new RotatedRect(new Point(0,0), new Size(1, 3), 0);

        private final Point[] pts = new Point[4];

        @Override
        protected void paintComponent(Graphics g) {
            rect.points(pts);
            printPoints();
            Dimension size = getSize();
            drawRectLine(g, pts[0], pts[1], size);
            drawRectLine(g, pts[1], pts[2], size);
            drawRectLine(g, pts[2], pts[3], size);
            drawRectLine(g, pts[0], pts[3], size);
        }

        private void printPoints() {
            System.out.format("A: %d, TL: %s, TR: %s, BR: %s, BL%s%n",
                    (int) (rect.angle + (rect.angle < 0 ? -1e-6 : 1e-6)), // Stupid doubles, stupid rounding error
                    pointToString(pts[0]),
                    pointToString(pts[3]),
                    pointToString(pts[2]),
                    pointToString(pts[1]));
        }

        private String pointToString(Point p) {
            return String.format("{%.2f,%.2f}",p.x, p.y);
        }

        private void drawRectLine(Graphics g, Point left, Point right, Dimension size) {
            g.drawLine(scale(left.x, size.width), scale(left.y, size.height),
                    scale(right.x, size.width), scale(right.y, size.height));
        }


        private int scale(double value, int coord) {
            return (int) (value * coord) / 4 + coord / 2;
        }


        @Override
        public void actionPerformed(ActionEvent e) {
            rect.angle += 1;
            if(rect.angle > 44) rect.angle = -44;
            repaint();
        }
    }
}
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..