What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?

31,330

Solution 1

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.

Solution 2

Explanation of Concept Without Code

Einsteins definition of indempotency

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. e.g. I make a get request to view my bank balance and it's the same. everytime. Unlike the game monopoly, I've never had an extra $100 magically added to my bank balance.
  • An operation that is not idempotent, for example, would be making a delete request to destroy a resource - every time you do this you will be changing state of the application. For example, the IRS keeps making delete requests to my bank account - and every-time they do so the "state" of my bank account depletes and is halved. It is an idempotent operation.

Solution 3

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

Share:
31,330
Aniket Thakur
Author by

Aniket Thakur

Hi there, I love to code and learn new things! I firmly believe that "It is your attitude rather than your aptitude that determines your altitude" and "Nothing is impossible if you give it your sincere try". My Blog - Open Source For Geeks You can also view - My Playstore Apps and My Git repositories / Gists Youtube channel Chrome Plugin All the Best! Stay in touch :) (LinkedIn Profile)

Updated on December 13, 2021

Comments

  • Aniket Thakur
    Aniket Thakur over 2 years

    Java docs of close() method of java.lang.AutoCloseable says

    Note that unlike the close() method of Closeable, this close() method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlike Closeable#close() which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent.


    What do they mean by idempotent method and what are the side effects of calling this close() method twice?

    And since interface Closeable extends AutoCloseable why are the side effects not to be seen in the close of Closeable interface?

  • Pacerier
    Pacerier about 9 years
    Also note that this is but one interpretation of the term "idempotent". "idempotent" has other meanings in different contexts.
  • Edwin Buck
    Edwin Buck over 8 years
    @Pacerier It is also the nearly word-for-word compatible description of the first sentence of your correction. If you use the mathematical theory approach to analyzing computation, it's still this definition, albeit with minor rewording f(f(x)) = f(x), which if you speak math means "if you preform the operation f on x twice, it's the same as performing the operation f on x once.
  • endo64
    endo64 over 2 years
    Nice example :)