What is a smart way to change a primitive variable from an anonymous Java class?

I would like to write the following code:

boolean found = false; search(new SearchCallback() { @Override void onFound(Object o) { found = true; } }); 

Obviously this is unacceptable since found must be final . I cannot make the found member field for thread safety reasons. What is the best alternative? One way is to define

 final class MutableReference<T> { private T value; MutableReference(T value) { this.value = value; } T get() { return value; } void set(T value) { this.value = value; } } 

but it ends up taking up a lot of space with proper formatting, and I would prefer not to reinvent the wheel, if at all possible. I could use a List<Boolean> with one element (either with mutation of this element, or with emptying the list), or even with Boolean[1] . But it seems that everything seems funny, since none of the options are used, as they were intended.

What is a reasonable way to do this?

+4
source share
4 answers

If you really can't use the field, Michael's answers seem correct.

Anyway. I don’t know what signatures you can touch, but it seems to me that this callback intends to do something (when the search succeeds) with the / found object. Instead, you intend to notify the caller of the search method that he has found something. It would seem much more natural if your seach () method were returned boolean (the method will certainly call s.onFound () somewhere, if the search succeeds, then set the internal found flag there and return it).

0
source

I usually use the boolean [1] method that you mentioned:

 final boolean[] found = {false}; search(new SearchCallback() { @Override void onFound(Object o) { found[0] = true; } }); 

It's a bit hacky, but it’s usually the closest to what you really want

+4
source

You can use all functions:

 Boolean found = search(new SearchCallback<Boolean>() { @Override Boolean onFound(Object o) { return true; } }); 

Usually, if you want to change the built-in variable, you can more clearly express the decision without doing it.

+3
source

All the solutions are really hacky, but the array is a "standard" textbook that can handle it, since even the pre-peaks were typical.

Another option in this situation is to create a private class as follows:

  private class Searcher implements SearchCallback { private boolean found; @Override public void onFound(Object o) { found = true; } public boolean search() { OuterClass.this.search(this); return found; } } 

And then use it like this:

  boolean found = new Searcher().search(); 

Edit: if I understand Tom's comment correctly, he offers this as an alternative

  public void foo() { //This is the method that enclosed the code in your question new SearchCallBack() { private boolean found; @Override public void onFound(Object o) { found = true; } { //The code that was before this in your method search(this); //The code that was after this in your method } }; } 

I think these are more hacks, and I would really find such code unusual, but it is definitely worth knowing that this is an option.

+2
source

Source: https://habr.com/ru/post/1308716/


All Articles