Iterate in reverse order over a list in Java

Sometimes we want to iterate in reverse order over a list in Java – without (re)sorting the list. Code example:

    for (T item : backwards(list)) {
        // do something
    }

Here is a backwards() method that does this for you (maybe put this code into a util class and use a static import):

    public static <T> Iterable<T> backwards(final List<T> list) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                return backwardsIterator(list);
            }
        };
    }

    private static <T> Iterator<T> backwardsIterator(List<T> list) {
        final ListIterator<T> iter = list.listIterator(list.size());
        return new Iterator<T>() {
            @Override
            public boolean hasNext() {
                return iter.hasPrevious();
            }

            @Override
            public T next() {
                return iter.previous();
            }

            @Override
            public void remove() {
                iter.remove();
            }
        };
    }

Record and share shell activity via shelr.tv

Terminal

Shelr.tv allows Unix/Linux command line users to record something interesting from their terminal and share it to followers.

It is a bit like YouTube for plain text shellcasts. A great feature is that you can copy and paste everything you see.

A nice intro with interesting comments from one of the core developers can be found on linuxaria.com.

A Debian package has been proposed through the Debian “package mentor” system.

World calendar

Facts due to movements of earth and moon:

  • 1 solar year = about 365.25 days
  • 1 lunar month = about 29.5 days

The leap year rules from the Gregorian calendar are good because they keep the calendar year well aligned with the solar year.

But the weird lengths of the months and shifting week day of January 1st are drawbacks in many respects.

It would be so much nicer to have a perennial calendar so that every year begins on the same week day and the week day of any given day in a month is predictable.

For these and many other reasons, I like the proposed World Calendar.

Export HTML table as CSV file using JQuery

Put this code into a script section into a common HTML head include file:

$(document).ready(function() {

  $('table').each(function() {
    var $table = $(this);

    var $button = $("<button type='button'>");
    $button.text("Export to spreadsheet");
    $button.insertAfter($table);

    $button.click(function() {
      var csv = $table.table2CSV({delivery:'value'});
      window.location.href = 'data:text/csv;charset=UTF-8,'
                            + encodeURIComponent(csv);
    });
  });
})

Note:

Consider vending misers to save energy at work …

A power meter is now installed in the upstairs lunch room in Halifax at the company that I currently work for. It measures the power consumption of both vending machines at the same time (using a power bar).

vending-machines-metered

The meter shows both the current consumption in Watts (W) and the accumulated energy use in kilo Watt hours (kWh). I installed it yesterday at 2:45pm and will let it run for 1 week (to get good data).

green-team-metering-in-progress

After that we will do some cost/benefit analysis regarding the potential installation of vending misers.

If you are in our Halifax office you can go and see for yourself how the consumption kind of alternates between roughly 150W (between refrigeration) and roughly 800W (during active refrigeration).

If you want to do something similar with other electric appliances, e.g. at your homes, you can buy those power meters devices at Canadian Tire or similar stores. This is the one that I am currently using.

Backup/restore XFCE desktop icons

Sometimes the XFCE desktop icons get messed up, for example by games that temporarily change the screen resolution to 800×600.

A solution to this problem has been mentioned here. It suggests using “sudo chattr +i” to lock the config file where XFCE stores the icon positions.

Alternatively (and without the repeated need for sudo and chattr) you can also backup and restore the ~/.config/xfce4/desktop/icons* file(s) like this:

Create a script /usr/local/bin/save-xfce-desktop-icons.sh like this:

#! /bin/sh
mkdir -p ~/.config/xfce4/desktop.bak
cp -f ~/.config/xfce4/desktop/icons* ~/.config/xfce4/desktop.bak

Create another script /usr/local/bin/load-xfce-desktop-icons.sh like this:

#! /bin/sh
cp -f ~/.config/xfce4/desktop.bak/icons* ~/.config/xfce4/desktop

Make the scripts executable like this

sudo chmod ugo+x /usr/local/bin/save-xfce-desktop-icons.sh
sudo chmod ugo+x /usr/local/bin/load-xfce-desktop-icons.sh

Then in the XFCE start menu, go to “Settings” – “Keyboard” – “Application Shortcuts” and configure 2 keyboard shortcuts:

Command Shortcut
save-xfce-desktop-icons.sh <Control><ALT>S
load-xfce-desktop-icons.sh <Control><ALT>L

Then you will be able to backup and restore your icons like this:

  • Backup: Press F5 then <Control><ALT>S then F5
  • Restore: Press F5 then <Control><ALT>L then F5

The F5 is necessary to synchronize what you see on the screen with the content of ~/.config/xfce4/desktop/icons*.

Tested old-o.github.io on all major browsers

I just tested my personal “résumé” – British folks would say “CV” – website at old-o.github.io successfully on IE8, IE9, Firefox 3.6 to 9, latest and older Chrome versions, Opera 10 and 11 and Safari 4 and 5.1. It works on all browsers and looks fine.

You might wonder if I have all these browser installed? No, I don’t. I used the amazing browserling.com service that runs all the various browsers in virtual machines “in the cloud”, and embeds the UI in their website. Cool stuff and currently free for everyone to use!

One caveat with browserling.com is a tool called IETester that they use to emulate the ancient IE5.5 and IE6 browsers. Its seems to have bugs related to PNG graphics which prevented reliable testing. So if anyone out there still uses IE5.5 or IE6: Please visit old-o.github.io and let me know if you can see the photo of me on the page with the transparency effect.

On the newer CSS3 capable browsers, my site now sports drop shadows and rounded corners, using border-radius and box-shadow.

I also tested W3C standards compliance (HTML5, CSS3) and all my pages did pass those tests as well, except for some stuff caused by bugs in the CSS3 validator at w3.org. What a nice way to end the computer oriented part of the day …