KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > core > properties > Properties


1 /*
2  * Properties.java
3  *
4  * Created on November 25, 2002, 7:06 PM
5  */

6
7 package org.netbeans.test.editor.app.core.properties;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 /**
15  *
16  * @author eh103527
17  */

18 public class Properties {
19     
20     Vector JavaDoc entries;
21     
22     /** Creates a new instance of Properties */
23     public Properties() {
24     entries=new Vector JavaDoc(30);
25     }
26     
27     public Object JavaDoc getProperty(java.lang.String JavaDoc name) {
28     if (name == null) throw new NullPointerException JavaDoc("Null name property.");
29     Entry e;
30     for (Iterator JavaDoc it=entries.iterator();it.hasNext();) {
31         e=(Entry)(it.next());
32         if (e.key.compareTo(name) == 0) {
33         return e.value;
34         }
35     }
36     return null;
37     }
38     
39     public Object JavaDoc put(String JavaDoc name, Object JavaDoc value) {
40     Object JavaDoc ret=null;
41     Entry e=null;
42     if (name == null) throw new NullPointerException JavaDoc("Null name property.");
43     if (value == null) throw new NullPointerException JavaDoc("Null property value.");
44     for (Iterator JavaDoc it=entries.iterator();it.hasNext();) {
45         e=(Entry)(it.next());
46         if (e.key.compareTo(name) == 0) {
47         ret=e.value;
48         break;
49         }
50     }
51     if (ret == null) {
52         entries.add(new Entry(name,value));
53     } else {
54         e.value=value;
55     }
56     return ret;
57     }
58     
59     public Enumeration JavaDoc propertyNames() {
60     return new Enumeration JavaDoc() {
61         String JavaDoc[] names=getNames();
62         int i=0;
63         
64         public boolean hasMoreElements() {
65         return (i < names.length);
66         }
67         
68         public Object JavaDoc nextElement() {
69         return names[i++];
70         }
71     };
72     }
73     
74     private String JavaDoc[] getNames() {
75     ArrayList JavaDoc ar=new ArrayList JavaDoc();
76     Entry e;
77     for (Iterator JavaDoc it=entries.iterator();it.hasNext();) {
78         e=(Entry)(it.next());
79         ar.add(e.key);
80     }
81     return (String JavaDoc[])(ar.toArray(new String JavaDoc[] {}));
82     }
83     
84     /** Removes all mappings from this map (optional operation).
85      *
86      * @throws UnsupportedOperationException clear is not supported by this
87      * map.
88      *
89      */

90     public void clear() {
91     entries.removeAllElements();
92     }
93     
94     /** Returns <tt>true</tt> if this map contains no key-value mappings.
95      *
96      * @return <tt>true</tt> if this map contains no key-value mappings.
97      *
98      */

99     public boolean isEmpty() {
100     return entries.size() == 0;
101     }
102     
103     /** Removes the mapping for this key from this map if it is present
104      * (optional operation). More formally, if this map contains a mapping
105      * from key <tt>k</tt> to value <tt>v</tt> such that
106      * <code>(key==null ? k==null : key.equals(k))</code>, that mapping
107      * is removed. (The map can contain at most one such mapping.)
108      *
109      * <p>Returns the value to which the map previously associated the key, or
110      * <tt>null</tt> if the map contained no mapping for this key. (A
111      * <tt>null</tt> return can also indicate that the map previously
112      * associated <tt>null</tt> with the specified key if the implementation
113      * supports <tt>null</tt> values.) The map will not contain a mapping for
114      * the specified key once the call returns.
115      *
116      * @param key key whose mapping is to be removed from the map.
117      * @return previous value associated with specified key, or <tt>null</tt>
118      * if there was no mapping for key.
119      *
120      * @throws ClassCastException if the key is of an inappropriate type for
121      * this map (optional).
122      * @throws NullPointerException if the key is <tt>null</tt> and this map
123      * does not not permit <tt>null</tt> keys (optional).
124      * @throws UnsupportedOperationException if the <tt>remove</tt> method is
125      * not supported by this map.
126      *
127      */

128     public Object JavaDoc remove(String JavaDoc key) {
129     Entry e;
130     for (Iterator JavaDoc it=entries.iterator();it.hasNext();) {
131         e=(Entry)(it.next());
132         if (e.key.compareTo(key) == 0) {
133         entries.remove(e);
134         return e.value;
135         }
136     }
137     return null;
138     }
139     
140     /** Returns the number of key-value mappings in this map. If the
141      * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
142      * <tt>Integer.MAX_VALUE</tt>.
143      *
144      * @return the number of key-value mappings in this map.
145      *
146      */

147     public int size() {
148     return entries.size();
149     }
150     
151     static class Entry {
152     public String JavaDoc key;
153     public Object JavaDoc value;
154     
155     public Entry(String JavaDoc key,Object JavaDoc value) {
156         this.key=key;
157         this.value=value;
158     }
159     }
160 }
161
Popular Tags