The properties iterator iterates over a set of enumerated properties. : Properties « Collections « Java Tutorial






/**
 * 
 * JFreeReport : a free Java reporting library
 * 
 *
 * Project Info:  http://reporting.pentaho.org/
 *
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * ------------
 * PropertiesIterator.java
 * ------------
 * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
 */

import java.util.Iterator;
import java.util.Properties;

/**
 * The properties iterator iterates over a set of enumerated properties. The
 * properties are named by an optional prefix plus a number, which is counted up
 * on each iteration: <p/>
 * <ul>
 * <li>prefix_0 </li>
 * <li>prefix_1 </li>
 * <li>prefix_2 </li>
 * <li>... </li>
 * </ul>
 * <p/> The iterator iterates over all subsequent numbered proprties until the
 * number-sequence is finished.
 * 
 * @author Thomas Morgner
 * 
 */
public class PropertiesIterator implements Iterator {
  /**
   * The underlying properties collection.
   */
  private Properties properties;

  /**
   * The property name prefix.
   */
  private String prefix;

  /**
   * An incremental counter.
   */
  private int count;

  /**
   * Creates a new properties iterator without an prefix.
   * 
   * @param properties
   *          the underlying properties collection.
   */
  public PropertiesIterator(final Properties properties) {
    this(properties, null);
  }

  /**
   * Creates a new properties iterator with the given prefix.
   * 
   * @param properties
   *          the underlying properties collection.
   * @param prefix
   *          a prefix for generating property names (null permitted).
   */
  public PropertiesIterator(final Properties properties, final String prefix) {
    if (properties == null) {
      throw new NullPointerException();
    }
    this.properties = properties;
    this.prefix = prefix;
    this.count = 0;
  }

  /**
   * Returns true if there is a property in the underlying collection with a
   * name that matches the name returned by the getNextKey() method.
   * 
   * @return true if there is another property with a name in the correct form.
   */
  public boolean hasNext() {
    return properties.containsKey(getNextKey());
  }

  /**
   * Generates a property name in the form <prefix>
   * &lt;count&gt;.
   * 
   *  The &lt;count&gt; begins at 0,
   *  and is automatically incremented with each call to the next() method.
   * 
   *  @return the next key in the sequence
   * 
   */
  private String getNextKey() {
    if (prefix == null) {
      return String.valueOf(count);
    }
    return prefix + String.valueOf(count);
  }

  /**
   * Returns the property with a name the same as the name generated by the
   * getNextKey() method, or null if there is no such property (that is, then
   * end of the sequence has been reached).
   * 
   * @return the property or null.
   */
  public Object next() {
    final String value = properties.getProperty(getNextKey());
    count++;
    return value;
  }

  /**
   * Always throws UnsupportedOperationException as remove is not implemented
   * for this iterator.
   * 
   * @throws UnsupportedOperationException
   *           as remove is not supported.
   */
  public void remove() {
    throw new UnsupportedOperationException();
  }

}








9.34.Properties
9.34.1.Setting and Getting Elements
9.34.2.using properties
9.34.3.Getting property by String key value
9.34.4.Getting a key List from Properties
9.34.5.Loading and Saving properties
9.34.6.Use store() to save the properties
9.34.7.List Properties to a print stream or print writer
9.34.8.Using Enumeration to loop through Properties
9.34.9.Put value to a Property list.
9.34.10.Sort Properties when saving
9.34.11.Sorts a property list and turns the sorted list into a string.
9.34.12.Sorts property list and print out each key=value pair prepended with specific indentation.
9.34.13.Load a properties file in the classpath
9.34.14.A Properties file stored in a JAR can be loaded this way
9.34.15.Load a properties file in the startup directory
9.34.16.Have a multi-line value in a properties file
9.34.17.Use XML with Properties
9.34.18.Store properties as XML file
9.34.19.Getting and Setting Properties
9.34.20.Convert a Properties list into a map.
9.34.21.To read a Properties file via an Applet
9.34.22.Read system property as an integer
9.34.23.Read a set of properties from the received input stream, strip off any excess white space that exists in those property values,
9.34.24.Property access utility methods
9.34.25.An utility class to ease up using property-file resource bundles.
9.34.26.Copy a set of properties from one Property to another.
9.34.27.Create Properties from String array
9.34.28.Gets strong-type-value property from a standard Properties
9.34.29.Merge Properties Into Map
9.34.30.Property Loader
9.34.31.Returns a Properties object matching the given node
9.34.32.The properties iterator iterates over a set of enumerated properties.
9.34.33.Use a default property list.