Archive for ‘Notes’

August 20, 2013

Mutable variable capture in anonymous Java classes

by Jay Jonas
 
 

The Java compiler requires local variables of enclosing contexts referenced in anonymous classes (so-called captured variables) to be final. What if the anonymous class wants to alter the value of the variable, i.e. requires the variable to be mutable? This post shows different ways how to achieve that.

Mutability with an array

The most common way to solve this problem is to put the variable into an array of length one, make the array final and capture the array:

<br /><%%KEEPWHITESPACE%%>   JButton button = new JButton("Press me!");<br /><%%KEEPWHITESPACE%%>   final String[] message = new String[]{"Never been pressed"};<br /><%%KEEPWHITESPACE%%>   button.addActionListener(new ActionListener() {<br /><br /><%%KEEPWHITESPACE%%>      @Override<br /><%%KEEPWHITESPACE%%>      public void actionPerformed(ActionEvent e) {<br /><%%KEEPWHITESPACE%%>         message[0] = "Pressed";<br /><%%KEEPWHITESPACE%%>      }<br /><br /><%%KEEPWHITESPACE%%>   });<br />

The value is stored in the first slot of the array. The reference to the array is final but its element stays mutable.

While this approach works fine, it looks strange and will certainly raise questions for developers in your team which have never seen this construct before.

Mutability with a holder

The better way to make the message mutable is to put it into a holder class

<br /><%%KEEPWHITESPACE%%>    class Holder {<br /><%%KEEPWHITESPACE%%>        private T value;<br /><br /><%%KEEPWHITESPACE%%>        Holder(T value) {<br /><%%KEEPWHITESPACE%%>            setValue(value);<br /><%%KEEPWHITESPACE%%>        }<br /><br /><%%KEEPWHITESPACE%%>        T getValue() {<br /><%%KEEPWHITESPACE%%>            return value;<br /><%%KEEPWHITESPACE%%>        }<br /><br /><%%KEEPWHITESPACE%%>        void setValue(T value) {<br /><%%KEEPWHITESPACE%%>            this.value = value;<br /><%%KEEPWHITESPACE%%>        }<br /><%%KEEPWHITESPACE%%>    }<br />

and pass the holder into the inner class:

<br /><%%KEEPWHITESPACE%%>   JButton button = new JButton("Press me!");<br /><%%KEEPWHITESPACE%%>   final Holder mutableMessage = new Holder("Never been pressed");<br /><%%KEEPWHITESPACE%%>   button.addActionListener(new ActionListener() {<br /><br /><%%KEEPWHITESPACE%%>      @Override<br /><%%KEEPWHITESPACE%%>      public void actionPerformed(ActionEvent e) {<br /><%%KEEPWHITESPACE%%>         mutableMessage.setValue("Pressed");<br /><%%KEEPWHITESPACE%%>      }<br /><br /><%%KEEPWHITESPACE%%>   });<br />

This solution states more clearly why the message has been wrapped. If the holder is implemented as a generic utility class, this solution is not more verbose than the one with the array. In case you don’t want to implement the Holder class yourself, you can also reuse the MutableObject from Apache Commons or the Holder from Google Guava. One could argue that the solution with the array is faster (creating an array is usually faster than instantiating a class), but in most cases the performance loss will be negligible.

// from at eclipsesource.com/blogs

 
September 22, 2012

Eclipse Task Tags Meaning

by Jay Jonas

TODO, along with FIXME and XXX, is known in Eclipse as a task tag, and is indexed by the IDE to let you find the spots marked with those tags easily. You can edit such tags in the Eclipse Preferences -> Java -> Compiler -> Task Tags.

This is the list for default Eclipse Task Tags and their meaning:

TODO

Comments that mark something for later work, later revision or at least later reconsideration. ODO comments should be considered a very useful technique, although like all good things on Earth, there’s certainly potential for abuse.

FIXME

A standard put in comments near a piece of code that is broken and needs work.

 XXX

A marker that attention is needed. Commonly used in program comments to indicate areas that are kluged up or need to be. Some hackers like `XXX’ to the notional heavy-porn movie rating. Use it to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.

ZZZ   /* This tag is mine!  ; ) */

Sometimes I like to use this marker for a stuff that was commented as deprecated just to remind me what happened there. However, when I’m sure, I delete it along with the code.

// TODO Need to optimize this before the end of the world.
// FIXME This won't work if the programmer is missing. 
// XXX This method badly needs refactoring: should be decaffeinated.
// ZZZ This was useful before the Y2K Millennium Bug.

--
From Sun/Oracle's Java code conventions, section 10.5.4:

June 1, 2012

15 client limit for Windows Server 2003 for Small Business Server

by Jay Jonas

That’s a horror history!
Technically, we have a 75 Client Access License (CAL) limit in a Microsoft Windows Small Business Server 2003 R2 (Part Number T75-01355). It comes with a 5 CALs in box.

So, we purchased additional CALs described as Microsoft Windows Server CAL 2008 sngl Open No Level User CAL.

We can perfectly add 2 packets of 5 CAL’s, but when we try to add the third packet of 5 CAL’s we get the following error:

“The specified license code cannot be added because it will exceed the
15 client limit for Windows Server 2003 for Small Business Server”

The client now has 15 licenses in total, but has still the remain CAL’s that
need to be added.

Note, I repeat,  “we have a Windows Small Business Server 2003 R2. Technically, it’s a 75 Client Access License (CAL)”. The error message is about a Windows Server 2003 for Small Business Server.

The Microsoft support says that all this problem is because we cannot dowgrade from Microsoft Windows Server CAL 2008 to a Microsoft Windows Server CAL 2003 for SBS.

Not a surprise when after buy Microsoft Windows Small Business Server Premium CAL Ste 2008 Single Open  20 No Level User CAL (packed of 20 CALs SKU 6VA-02189) and Microsoft Windows Small Business Server Premium CAL Ste 2008 Single Open  5 No Level User CAL (packed of 5 CALs SKU 6VA-01644) we got the same error message:

“The specified license code cannot be added because it will exceed the
15 client limit for Windows Server 2003 for Small Business Server”

We called Microsoft Support and they asked us if we have a support contract. “No, we don’t” was the answer. So, they said if we pay a support they will fix it!

If it were not tragic, it would be comic!

I will tell the rest of the story in the next post. Stay tune!

December 31, 2011

2011 in review

by Jay Jonas

The WordPress.com stats helper monkeys prepared a 2011 annual report for our blog.

Happy New Year and here’s an excerpt:

A San Francisco cable car holds 60 people. This blog was viewed about 3,100 times in 2011. If it were a cable car, it would take about 52 trips to carry that many people.

Click here to see the complete report.

October 4, 2011

JavaBeans PropertyChangeSupport Template

by Jay Jonas

Property-change events occur whenever the value of a bound property changes for a java bean. You can use a Eclipse template to help you to write all the standard setter code

Open Windows | Preferences. Browse to Java | Editor | Templates. Click on New and enter property_change_support as Name, choose Java Statements as Context and mark Automatically Insert option. Write a meaning description as “property change support for setter method” and copy & paste into Pattern the code as follows:

if (this.${enclosing_method_arguments} == null && ${enclosing_method_arguments} == null) {
return;
}
propertyChangeSupport.firePropertyChange("${enclosing_method_arguments}", this.${enclosing_method_arguments}, ${word_selection})

Take care about that there is no new line at the end of the last line.
Take notes and will never forget it.