Pattern of finite array instead of non-finite variable for boolean flag in inner class

I often have a situation in my Java code when I need to set a Boolean flag inside an inner class. It is impossible to use a primitive boolean type for this, because the inner class can only work with final variables from the outside, so I use the template as follows:

// class from gnu.trove is not of big importance, just to have an example private final TIntIntHashMap team = new TIntIntHashMap(); // ....... code ............ final boolean[] flag = new boolean[]{false}; team.forEachValue(new TIntProcedure() { @Override public boolean execute(int score) { if(score >= VICTORY_SCORE) { flag[0] = true; } return true; // to continue iteration over hash map values } }); // ....... code .............. 

A sample finite array instead of a non-final variable works well, except that I don't look pretty enough for me. Does anyone know a better pattern in Java?

+1
java design-patterns
source share
6 answers

There are situations when this is the best template.

The only improvement I can offer is return false when you find a match.

+3
source share

Use AtomicBoolean .

Here is a popular StackOverflow question about this issue: Why are only finite variables available in an anonymous class?

+6
source share

How to get a general holder class that contains an object of any type. In your case, it may contain a Boolean type. Something like:

 class Holder<T> { private T genericObj; public Holder(T genericObj) { this.genericObj = genericObj; } public T getGenericObj() { return genericObj; } public void setGenericObj(T genericObj) { this.genericObj = genericObj; } } 

And use it like:

 public class Test { public static void main(String[] args) throws Exception { final Holder<Boolean> boolHolder = new Holder<Boolean>(Boolean.TRUE); new Runnable() { @Override public void run() { boolHolder.setGenericObj(Boolean.FALSE); } }; } } 

Of course, this has the usual problems arising with mutable objects that are thread-separated, but you get the idea. Plus, for applications where memory requirements are strict, this can be tailored when performing optimization if you have many calls to such methods. In addition, using AtomicReference to exchange / set links should take care of using from multiple threads, although using it across threads will still be a bit dubious.

+3
source share

One of the problems is that TIntIntHashMap does not have a fold / reduce method, so you have to simulate it using foreach. You can try to write your own class that extends TIntIntHashMap by adding a reduction method.

Another solution is to simply extend TIntProcedure to make a difference. Something like:

 abstract class TIntProcedureWithValue<T> implements TIntProcedure { private T accumulator; public T getValue() {return accumulator;} } 

Then you can pass an instance of this class to foreach, install an internal drive instead of an external array of flags and get the resulting value later.

+1
source share

I am not familiar with gnu.trove, but, as a rule, it is better for the "algortihm" function to be more specific, leaving less code here.

 private final IntIntHashMap team = new IntIntHashMap(); boolean found = team.value().containsMatch(new IntPredicate() { public boolean is(int score) { return score >= VICTORY_SCORE; } }); 

(More concise syntax should be available in Java SE 8.)

+1
source share
Maybe something like that? (implements or extends ... I do not know what TIntProcedure is, unfortunately):
  class FlagResult implements TIntProcedure { boolean flag = false; @Override public boolean execute(int score) { flag = score >= VICTORY_SCORE; return !flag; } }; FlagResult result = new FlagResult(); team.forEachValue(result); boolean flag = result.flag; 
0
source share

All Articles