KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > util > PropertiesMap


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.util;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.LinkedHashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Represents a persistent set of properties.
28  * <p>This class is a replacement for {@link Properties} class.
29  * <p>Please note that {@link #put(String, Object)} has additional semantics.
30  *
31  * @author Fyodor Kupolov
32  * @version 1.0
33  */

34 public class PropertiesMap implements Map JavaDoc<String JavaDoc, Object JavaDoc> {
35     private Map JavaDoc<String JavaDoc, Object JavaDoc> props;
36
37     public PropertiesMap() {
38         props = new LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc>();
39     }
40
41     public PropertiesMap(int initialCapacity) {
42         props = new LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc>(initialCapacity);
43     }
44
45     public PropertiesMap(Map JavaDoc<String JavaDoc, ?> props) {
46         this(props.size());
47         putAll(props);
48     }
49
50     public int size() {
51         return props.size();
52     }
53
54     public boolean isEmpty() {
55         return props.isEmpty();
56     }
57
58     public boolean containsKey(Object JavaDoc key) {
59         return props.containsKey(key);
60     }
61
62     public boolean containsValue(Object JavaDoc value) {
63         return props.containsValue(value);
64     }
65
66     public Object JavaDoc get(Object JavaDoc key) {
67         return props.get(key);
68     }
69
70     /**
71      * Put the property to underlying map.
72      * <p>The properties are immutable, i.e. if the property is already present in the map, the new value is ignored.
73      *
74      * @param key property name
75      * @param value property value
76      * @return value associated with specified key,
77      * or null if there was no mapping for key.
78      */

79     public Object JavaDoc put(String JavaDoc key, Object JavaDoc value) {
80         Object JavaDoc old = props.get(key);
81         if (old==null) {
82             props.put(key, value);
83         }
84         return old;
85     }
86
87     public Object JavaDoc remove(Object JavaDoc key) {
88         return props.remove(key);
89     }
90
91     public void putAll(Map JavaDoc<? extends String JavaDoc, ? extends Object JavaDoc> t) {
92         for (Entry<? extends String JavaDoc, ? extends Object JavaDoc> entry : t.entrySet()) {
93             put(entry.getKey(), entry.getValue());
94         }
95     }
96
97     public void clear() {
98         props.clear();
99     }
100
101     public Set JavaDoc<String JavaDoc> keySet() {
102         return props.keySet();
103     }
104
105     public Collection JavaDoc<Object JavaDoc> values() {
106         return props.values();
107     }
108
109     public Set JavaDoc<Entry<String JavaDoc, Object JavaDoc>> entrySet() {
110         return props.entrySet();
111     }
112
113     public boolean equals(Object JavaDoc o) {
114         return props.equals(o);
115     }
116
117     public int hashCode() {
118         return props.hashCode();
119     }
120
121     /**
122      * Loads properties using {@link Properties#load(java.io.InputStream)}.
123      * <p>Properties order is preserved
124      *
125      * @param is input stream with properties.
126      * @throws IOException if I/O error occurs.
127      */

128     public void load(InputStream JavaDoc is) throws IOException JavaDoc {
129         Properties JavaDoc tmp = new Properties JavaDoc() { //Overrides Properties to preserve insertion order
130

131             public Object JavaDoc put(final Object JavaDoc k, final Object JavaDoc v) {
132                 return PropertiesMap.this.put((String JavaDoc) k, v);
133             }
134         };
135         tmp.load(is);
136     }
137
138
139     public String JavaDoc toString() {
140         return String.valueOf(props);
141     }
142 }
143
Popular Tags