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();
            }
        };
    }

Maven integration (m2e) for Eclipse 3.7.x (Indigo)

Sonatype’s Maven integration for Eclipse (m2e) has been migrated to eclipse.org and is now available from the default update site for Indigo.

This means that you no longer have to add any Sonatype update sites as mentioned on the old (now outdated) m2eclipse site.

In particular, this also means that the old “m2e-extras” update site is obsolete for Eclipse 3.7.x and later. Things like WTP integration or Subclipse integration are now available as “m2e connectors” through Window – Preferences – Maven – Discovery.

All of this is very poorly (or not at all) documented on the new m2e home page and some people had problems with these changes.

Programming principles for Java methods

Topics I want to cover in the future about Java methods:

  • Minimalistic coding style [1] [2] [3] [4]
  • “Fail early” (or “fail fast”) principle [1] [2]
  • “Return early” principle [1] [2] [3]
  • Object creation methods (to allow final)
  • Type generic methods (process a T vs create a T)
  • Functional style: stateless static methods vs interfaces
  • Javadoc: On interface not implementation
  • Method naming guidelines
  • Strategy parameters (often anonymous interface implementations)
  • Exception handling methods (try-catch-finally, throwing exceptions)
  • How to avoid setter/getter madness
  • Method chaining API style (static factory method, builder methods)

Jenkins on minimalistic Debian Virtualbox VM (64bit)

Update 14/Aug/2018: I no longer maintain the package mentioned and the download links below are most likely broken.

A Jenkins build server (LTS release) can now be easily installed on the minimalistic Debian VM:

  1. Download and install Virtualbox
  2. Download debian-stable-amd64-minimal.ova and import it into Virtualbox
  3. Start the “debian-stable-amd64-minimal” VM in Virtualbox
  4. If you are outside Nova Scotia, please review debian-stable-amd64-minimal.txt and adjust locale, timezone and Debian mirror based on your location
  5. Start an ssh session to localhost, port 1111 (using PuTTY, for example)
  6. Log in as user (default password is “user”)
  7. Issue “sudo install.sh jenkins” (default root password is “root”)
  8. Press enter for any questions during installation
  9. Open http://localhost:8888/ in a browser on the host OS for Jenkins web ui

You can go to “Manage Jenkins” – “Configure System” and see that JDK, Ant and Maven entries are already configured for you.

Both OpenJDK 6 and OpenJDK 7 are installed automatically for you in the VM. Please note that openjdk-6 is the Debian stable system default, while openjdk-7 is configured as the default for Jenkins build jobs.

Important: Make sure to change root and user passwords to something secure, as mentioned in debian-stable-amd64-minimal.txt.

Lightweight Eclipse package for web development

Update 14/Aug/2018: I no longer maintain the package mentioned and the download links below are most likely broken.

I uploaded a lightweight Eclipse package (based on Helios 3.6.1) for web development (includes Maven, SVN and basic Spring integration, JEE / web tools plugins, but no Mylyn or other non-essential stuff) … This is currently only for Windows. It requires a JDK and is completely free / open source software.

See the txt file for quick installation steps.