The properties iterator iterates over a set of enumerated properties. : Properties « Development Class « Java






The properties iterator iterates over a set of enumerated properties.

      
/**
 * 
 * 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;.
   * <P>
   *  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();
  }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Read a set of properties from the received input stream, strip off any excess white space that exists in those property values,
2.Copy a set of properties from one Property to another.
3.Sorts property list and print out each key=value pair prepended with specific indentation.
4.Sorts a property list and turns the sorted list into a string.
5.A utility class for replacing properties in strings.
6.Property Loader
7.Property access utility methods
8.Represents a persistent set of properties
9.Converts specified map to java.util.Properties
10.A java.util.Properties class that will check a file or URL for changes periodically
11.Encapsulates java.util.Properties to add java primitives and some other java classes
12.Load and save properties to files.
13.Adds new properties to an existing set of properties
14.Extracts a specific property key subset from the known properties
15.Observable Properties
16.Merge Properties Into Map
17.XML configuration management
18.JDOM based XML properties
19.XML Properties
20.Converts Unicode into something that can be embedded in a java properties file
21.Create Properties from String array
22.Task to overwrite Properties
23.Gets strong-type-value property from a standard Properties
24.An utility class to ease up using property-file resource bundles.
25.Sorted Properties
26.A subclass of the java.util.Properties class that must be initialized from a file on disk
27.This class contains a collection of static utility methods for creating, retrieving, saving and loading properties.
28.Simplify routine job with properties
29.Property Manager