Java Specialists' Java Training Europehome of the java specialists' newsletter

The Java Specialists' Newsletter
Issue 095b2004-10-01 Category: Language Java version: Sun JDK 1.5.0-rc

GitHub Subscribe Free RSS Feed

Follow-up: Self-reloading XML Property Files

by Dr. Heinz M. Kabutz

In my last newsletter, I mentioned that I like generics. To me, they improve the Java language by being more specific as to what is allowed in collections. I find writing code with generics quite easy, because IntelliJ IDEA assists me with it. However, John O'Sullivan mentioned that it took him "MUCH MUCH MUCH longer to read" the code to see what I was doing, and I think that John has a point.

This may be a problem. The cost of maintenance often dwarfs the cost of initially writing the code. Can generics help in maintenance? I cannot recall ever seeing a ClassCastException "in the field" due to incorrect types being put into a collection.

Here is the AbstractConfiguration class written without using generics. By removing generics, I discovered that the getAllProperties() would have been better off returning a copy of a Map, rather than a Set of entries. This "design flaw" was obscured by having so much text as a return code: Set<Map.Entry<String, Object>>.

So, here is your opportunity to have your say. Please think about whether you find that generics will improve maintenance or not. Remember that if the syntax bothers you, that is a matter of time before you are used to it. You can also do clever tricks with syntax highlighting. An IDE could even give you the option of hiding generics in the editor so that they do not bother you. Try thinking of technical reasons, rather than just looks.

  1. Generics make the code easier to maintain
  2. Generics make the code more difficult to maintainlink>

I will send a summary of the results in my next newsletter, and might publish the full results on my webpage. Please indicate if you would like to remain anonymous in the results. If I do reveal your comments, they would only include your name and company, definitely not your email address.

import java.beans.*;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class AbstractConfigurationNoGenerics {
  /** A map of all the properties for the configuration */
  private final Map properties = new HashMap();

  private final Collection listeners = new CopyOnWriteArrayList();

  /** Make a daemon timer to check for changes. */
  private final Timer timer = new Timer(true);

  /**
   * This class has a timer to check periodically if the
   * configuration has changed.  If it has, it reloads the
   * properties.  This may cause the property change events to
   * fire.
   *
   * @param period number of milliseconds between checking for
   *               property changes.
   */
  protected AbstractConfigurationNoGenerics(int period) {
    timer.schedule(new TimerTask() {
      public void run() {
        checkForPropertyChanges();
      }
    }, period, period);
  }

  /**
   * This method should be overridden to check whether the
   * properties could maybe have changed, and if yes, to reload
   * them.
   */
  protected abstract void checkForPropertyChanges();

  public final Object getProperty(String propertyName) {
    synchronized (properties) {
      return properties.get(propertyName);
    }
  }

  public Map getAllProperties() {
    synchronized (properties) {
      return new HashMap(properties);
    }
  }

  /**
   * Each time we set a property, we check whether it has changed
   * and if it has, we let the listeners know.
   */
  protected final void setProperty(String propertyName, Object value) {
    synchronized (properties) {
      Object old = properties.get(propertyName);
      if ((value != null && !value.equals(old))
          || value == null && old != null) {
        properties.put(propertyName, value);
        PropertyChangeEvent event = new PropertyChangeEvent(this,
            propertyName, old, value);
        for (Iterator it = listeners.iterator(); it.hasNext();) {
          PropertyChangeListener listener = (PropertyChangeListener) it.next();
          listener.propertyChange(event);
        }
      }
    }
  }

  public void addPropertyChangeListener(PropertyChangeListener l) {
    listeners.add(l);
  }

  public boolean removePropertyChangeListener(PropertyChangeListener l) {
    return listeners.remove(l);
  }
}
  

I am currently sitting in Citrusdal, about 2 hours drive from my home, and this newsletter will reach you via GPRS :-)

Kind regards

Heinz

Language Articles Related Java Course

Extreme Java - Concurrency and Performance for Java 8
Extreme Java - Advanced Topics for Java 8
Design Patterns
In-House Courses

© 2010-2016 Heinz Kabutz - All Rights Reserved Sitemap
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. JavaSpecialists.eu is not connected to Oracle, Inc. and is not sponsored by Oracle, Inc.