KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > ConfigurationMap


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.configuration;
18
19 import java.util.AbstractMap JavaDoc;
20 import java.util.AbstractSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * <p>The <code>ConfigurationMap</code> wraps a
27  * configuration-collection
28  * {@link org.apache.commons.configuration.Configuration}
29  * instance to provide a <code>Map</code> interface.</p>
30  *
31  * @todo This implementation is incomplete.
32  *
33  * @author <a HREF="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
34  * @version $Revision: 155408 $, $Date: 2005-02-26 13:56:39 +0100 (Sa, 26 Feb 2005) $
35  * @since 1.0
36  */

37 public class ConfigurationMap extends AbstractMap JavaDoc
38 {
39     /**
40      * The <code>Configuration</code> wrapped by this class.
41      */

42     Configuration configuration = null;
43
44     /**
45      * Creates a new instance of a <code>ConfigurationMap</code>
46      * that wraps the specified <code>Configuration</code>
47      * instance.
48      * @param configuration <code>Configuration</code>
49      * instance.
50      */

51     public ConfigurationMap(Configuration configuration)
52     {
53         this.configuration = configuration;
54     }
55
56     /**
57      * @see java.util.Map#entrySet()
58      */

59     public Set JavaDoc entrySet()
60     {
61         return new ConfigurationSet(configuration);
62     }
63
64     /**
65      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
66      */

67     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
68     {
69         String JavaDoc strKey = String.valueOf(key);
70         Object JavaDoc old = configuration.getProperty(strKey);
71         configuration.setProperty(strKey, value);
72         return old;
73     }
74
75     /**
76      * @see java.util.Map#get(java.lang.Object)
77      */

78     public Object JavaDoc get(Object JavaDoc key)
79     {
80         return configuration.getProperty(String.valueOf(key));
81     }
82
83     static class ConfigurationSet extends AbstractSet JavaDoc
84     {
85         private Configuration configuration = null;
86
87         private class Entry implements Map.Entry JavaDoc
88         {
89             private Object JavaDoc key = null;
90             
91             private Entry(Object JavaDoc key)
92             {
93                 this.key = key;
94             }
95             
96             public Object JavaDoc getKey()
97             {
98                 return key;
99             }
100             
101             public Object JavaDoc getValue()
102             {
103                 return configuration.getProperty((String JavaDoc) key);
104             }
105
106             public Object JavaDoc setValue(Object JavaDoc value)
107             {
108                 Object JavaDoc old = getValue();
109                 configuration.setProperty((String JavaDoc) key, value);
110                 return old;
111             }
112
113         }
114
115         private class ConfigurationSetIterator implements Iterator JavaDoc
116         {
117             private Iterator JavaDoc keys;
118
119             private ConfigurationSetIterator()
120             {
121                 keys = configuration.getKeys();
122             }
123
124             public boolean hasNext()
125             {
126                 return keys.hasNext();
127             }
128
129             public Object JavaDoc next()
130             {
131                 return new Entry(keys.next());
132             }
133
134             public void remove()
135             {
136                 keys.remove();
137             }
138
139         }
140
141         ConfigurationSet(Configuration configuration)
142         {
143             this.configuration = configuration;
144         }
145
146         /**
147          * @see java.util.Collection#size()
148          */

149         public int size()
150         {
151             // Ouch. Now _that_ one is expensive...
152
int count = 0;
153             for (Iterator JavaDoc iterator = configuration.getKeys(); iterator.hasNext(); )
154             {
155                 iterator.next();
156                 count++;
157             }
158             return count;
159         }
160
161         /**
162          * @see java.util.Collection#iterator()
163          */

164         public Iterator JavaDoc iterator()
165         {
166             return new ConfigurationSetIterator();
167         }
168     }
169 }
170
Popular Tags