What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?
Idempotent means that you can apply the operation a number of times, but the resulting state of one call will be indistinguishable from the resulting state of multiple calls. In short, it is safe to call the method multiple times. Effectively the second and third (and so on) calls will have no visible effect on the state of the program.
So if you close this object once, and it closes, you don't have enough information to know if it is idempotent. However, if you close it twice, and the first time it closes, but the second time it throws an exception, it is clearly not idempotent. On the other hand, if you close it once, and close it twice, and the second closure results in the item remaining closed in the same manner (perhaps it is a noop), then it is idempotent.
One technique of making an idempotent Closeable
could be:
public class Example implements Closeable {
private boolean closed;
public Example() {
closed = false;
}
public void close() {
if (!isClosed()) {
closed = true;
}
}
public boolean isClosed() {
return closed;
}
}
Where it is now obvious that if close()
is called once or multiple times, all returns of the state through isClosed()
will forever return true. Therefore, the method close()
would be considered idempotent.
Explanation of Concept Without Code
To adopt Einstein's aphorism, if you do the same thing, and get different results, then the method is not idempotent.
Example of idempotency
"Please sir, can I have a pay rise?"
"No"
Same result every time. Asking for a pay rise is an idempotent operation.
Examples with HTTP Requests:
- Making a
get
request: If properly implemented then no matter how many times you make this request, you will get the same response. - An operation that is not Idempotent, for example, would be making a
post
request to create a resource - every time you do this you will be changing state of the application you are posting this to: a new resource will be created every single time!
JAVA GLOSSARY Idempotent
If methods are written in such a way that repeated calls to the same method do not cause duplicate updates, the method is said to be "idempotent."
In mathematics an idempotent element, or an idempotent for short, is anything that, when multiplied by itself, gives itself as result. For example, the only two real numbers which are idempotent are 0 and 1.
In user interface design, a button can be called "idempotent" if pressing it more than once will have the same effect as pressing it once. For example, a "Pause" button is not idempotent if it toggles the paused state. On the other hand, if pressing it multiple times keeps the system paused and pressing "Play" resumes, then "Pause" is idempotent. This is useful in interfaces such as infrared remote controls and touch screens where the user may not be sure of having pressed the button successfully and may press it again. Elevator call buttons are also idempotent, though many people think they are not.
Resource:-http://www.allapplabs.com/glossary/idempotent.htm