Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

Making Enumerations Iterable

       

2005-04-19 The Java Specialists' Newsletter [Issue 107] - Making Enumerations Iterable

Author: Dr. Heinz M. Kabutz

JDK version: Sun JDK 1.5.0_02

If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page. You can subscribe either via email or RSS.


Welcome to the 107th edition of The Java(tm) Specialists' Newsletter, sent to you from Vienna, Austria. Vienna is a beautiful city with lots of old buildings and culture. Thanks to Thomas Eichberger from AGILA Softwareentwicklung und Schulung for showing me around Vienna. Thomas is the top Java trainer in Austria (except of course when I am in the country ;-).

A special treat was meeting up with family Mahrl, who I last saw in Cape Town over 20 years ago. Franz Mahrl was probably the first real computer programmer that I met, and may have been an inspiration to eventually pursue the career. I certainly do not regret becoming a programmer. We have the best jobs in the world - and we even get paid for having fun! It is great to meet up with the heros of your childhood, and try to peer through the dark curtains of time.

We are running a Java Course in Cape Town in May 2005, so please let me know if you or your developers would like to attend. In addition, 2005 is the year where I intend focusing on Java in Europe, so please write to me if you would benefit from training, consulting or programming, especially in the United Kingdom, Ireland, Germany, Switzerland or Austria.

Next week I will be in Siegen, Germany, so please let me know if you would like to pop over for a coffee and a chat about Java, South Africa, the world, etc., one of the evenings.

Making Enumerations Iterable

There are some parts of Java where we still see Enumeration being used. I performed a search with IntelliJ and was amazed how often it still occurs. Changing the interface of existing classes is not always feasible, so we sometimes need to iterate through an Enumeration. For an example, look at newsletter #106 where we enumerate through the columns. Unfortunately, we then cannot use the nice for/in construct to iterate through the enumeration - or can we?

It is possible to get an Enumeration over a Collection by calling the function java.util.Collections.enumeration(Collection). That does not help us though, since it is going in the wrong direction. We want to iterate through an Enumeration.

We could also convert the Enumeration to an ArrayList using the method java.util.Collections.list(Enumeration) and then use that as the expression in the for/in construct. This is also not ideal though, since we then have to construct an ArrayList each time we want to iterate. This can be expensive, especially with a large Collection.

I therefore propose to rather build an adapter for the Enumeration, and then make an IterableEnumeration that satisfies the Iterable interface, and is Generics enabled:

import java.util.*;

public class IterableEnumeration<T> implements Iterable<T> {
  private final Enumeration<T> en;
  public IterableEnumeration(Enumeration<T> en) {
    this.en = en;
  }
  // return an adaptor for the Enumeration
  public Iterator<T> iterator() {
    return new Iterator<T>() {
      public boolean hasNext() {
        return en.hasMoreElements();
      }
      public T next() {
        return en.nextElement();
      }
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }
  public static <T> Iterable<T> make(Enumeration<T> en) {
    return new IterableEnumeration<T>(en);
  }
}
  

Ignore the static make function for now. We can use it by constructing an instance of IterableEnumeration giving it the generic type, like so:

import java.sql.*;
import java.util.*;

public class IterableTest {
  public static void main(String[] args) {
    Vector<String> sv = new Vector<String>();
    sv.addElement("Maximilian");
    sv.addElement("Francis");
    sv.addElement("Kabutz");

    // using the generics makes it look a bit clumsy
    IterableEnumeration<String> ie =
        new IterableEnumeration<String>(sv.elements());
    for (String s : ie) {
      System.out.println(s);
    }

    // Without generics, we cannot automatically cast to String
    IterableEnumeration ie2 =
        new IterableEnumeration(sv.elements());
    for (Object s : ie2) { // here we now have to use Object type
      System.out.println(s);
    }

    // Again, generics makes the code look clumsy
    // here you should load your own driver, if applicable
    new sun.jdbc.odbc.JdbcOdbcDriver();
    IterableEnumeration<Driver> drivers =
        new IterableEnumeration<Driver>(
            DriverManager.getDrivers());
    for (Driver driver : drivers) {
      System.out.println("driver = " + driver.getClass());
    }

    // or we could build up the list using Collections.list()
    // and iterate through that - this is ineffient.
    for (Driver driver : Collections.list(
        DriverManager.getDrivers())) {
      System.out.println("driver = " + driver.getClass());
    }
  }
}
  

If possible, I want to use the compile-time checking of generics without having to see the ugly syntax. That is why I wrote the make method, which returns the correct type without me having to specify it anywhere:

import java.sql.*;
import java.util.Vector;

public class IterableTestStatic {
  public static void main(String[] args) {
    Vector<String> sv = new Vector<String>();
    sv.addElement("Maximilian");
    sv.addElement("Francis");
    sv.addElement("Kabutz");

    // Use a static "factory method" to reduces generics clutter
    for (String s : IterableEnumeration.make(sv.elements())) {
      System.out.println(s);
    }

    // This also looks slightly more readable
    new sun.jdbc.odbc.JdbcOdbcDriver();
    for (Driver driver : IterableEnumeration.make(
        DriverManager.getDrivers())) {
      System.out.println("driver = " + driver.getClass());
    }
  }
}
  

I personally find that more readable. It seems to me, though this is just an observation, that static methods are slightly more powerful when it comes to generics.

Kind regards

Heinz

This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions for more information.

       

Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.