alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * Properties.java
 *
 * Created on November 25, 2002, 7:06 PM
 */

package org.netbeans.test.editor.app.core.properties;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author  eh103527
 */
public class Properties {
    
    Vector entries;
    
    /** Creates a new instance of Properties */
    public Properties() {
	entries=new Vector(30);
    }
    
    public Object getProperty(java.lang.String name) {
	if (name == null)  throw new NullPointerException("Null name property.");
	Entry e;
	for (Iterator it=entries.iterator();it.hasNext();) {
	    e=(Entry)(it.next());
	    if (e.key.compareTo(name) == 0) {
		return e.value;
	    }
	}
	return null;
    }
    
    public Object put(String name, Object value) {
	Object ret=null;
	Entry e=null;
	if (name == null)  throw new NullPointerException("Null name property.");
	if (value == null) throw new NullPointerException("Null property value.");
	for (Iterator it=entries.iterator();it.hasNext();) {
	    e=(Entry)(it.next());
	    if (e.key.compareTo(name) == 0) {
		ret=e.value;
		break;
	    }
	}
	if (ret == null) {
	    entries.add(new Entry(name,value));
	} else {
	    e.value=value;
	}
	return ret;
    }
    
    public Enumeration propertyNames() {
	return new Enumeration() {
	    String[] names=getNames();
	    int i=0;
	    
	    public boolean hasMoreElements() {
		return (i < names.length);
	    }
	    
	    public Object nextElement() {
		return names[i++];
	    }
	};
    }
    
    private String[] getNames() {
	ArrayList ar=new ArrayList();
	Entry e;
	for (Iterator it=entries.iterator();it.hasNext();) {
	    e=(Entry)(it.next());
	    ar.add(e.key);
	}
	return (String[])(ar.toArray(new String[] {}));
    }
    
    /** Removes all mappings from this map (optional operation).
     *
     * @throws UnsupportedOperationException clear is not supported by this
     * 		  map.
     *
     */
    public void clear() {
	entries.removeAllElements();
    }
    
    /** Returns true if this map contains no key-value mappings.
     *
     * @return true if this map contains no key-value mappings.
     *
     */
    public boolean isEmpty() {
	return entries.size() == 0;
    }
    
    /** Removes the mapping for this key from this map if it is present
     * (optional operation).   More formally, if this map contains a mapping
     * from key k to value v such that
     * (key==null ?  k==null : key.equals(k)), that mapping
     * is removed.  (The map can contain at most one such mapping.)
     *
     * 

Returns the value to which the map previously associated the key, or * null if the map contained no mapping for this key. (A * null return can also indicate that the map previously * associated null with the specified key if the implementation * supports null values.) The map will not contain a mapping for * the specified key once the call returns. * * @param key key whose mapping is to be removed from the map. * @return previous value associated with specified key, or null * if there was no mapping for key. * * @throws ClassCastException if the key is of an inappropriate type for * this map (optional). * @throws NullPointerException if the key is null and this map * does not not permit null keys (optional). * @throws UnsupportedOperationException if the remove method is * not supported by this map. * */ public Object remove(String key) { Entry e; for (Iterator it=entries.iterator();it.hasNext();) { e=(Entry)(it.next()); if (e.key.compareTo(key) == 0) { entries.remove(e); return e.value; } } return null; } /** Returns the number of key-value mappings in this map. If the * map contains more than Integer.MAX_VALUE elements, returns * Integer.MAX_VALUE. * * @return the number of key-value mappings in this map. * */ public int size() { return entries.size(); } static class Entry { public String key; public Object value; public Entry(String key,Object value) { this.key=key; this.value=value; } } }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.