MapPropertySet.java

biz/hammurapi/properties/MapPropertySet.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 049 Use a Collection instead of arrays Object[] 2 40:9
Java Inspector 089 Undocumented constructor 2 42:9
Java Inspector 089 Undocumented constructor 2 48:9
Java Inspector 089 Undocumented constructor 2 55:9
Java Inspector 089 Undocumented constructor 2 59:9
Java Inspector 089 Undocumented method 2 65:9
Java Inspector 089 Undocumented method 2 73:9
Java Inspector 089 Undocumented method 2 77:9
Java Inspector 089 Undocumented method 2 81:9
Java Inspector 089 Undocumented method 2 85:9
Java Inspector 089 Undocumented method 2 112:9
Java Inspector 089 Undocumented method 2 118:9
Java Inspector 089 Undocumented method 2 122:9
Java Inspector 089 Method is not properly documented 2 134:9
Java Inspector 089 Undocumented parameter otherSet 2 134:9
Java Inspector 089 Undocumented method 2 158:9
Java Inspector 089 Undocumented method 2 182:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 42:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 48:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 55:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 59:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.properties;
24
25import java.util.HashMap;
26import java.util.HashSet;
27import java.util.Iterator;
28import java.util.Map;
29import java.util.Set;
30import java.util.Map.Entry;
31
32/**
33 * Creates property sets backed by java.util.HashMap.
34 * @author Pavel
35 *
36 */
37
38public class MapPropertySet implements PropertySet {
39 private Map map = new HashMap();
40 private PropertySet[] chain = {};
41
42 public MapPropertySet(PropertySet[] chain) {
43 if (chain!=null) {
44 this.chain = chain;
45 }
46 }
47
48 public MapPropertySet(Map source, PropertySet[] chain) {
49 this(chain);
50 if (source!=null) {
51 map.putAll(source);
52 }
53 }
54
55 public MapPropertySet() {
56
57 }
58
59 public MapPropertySet(Map source) {
60 if (source!=null) {
61 map.putAll(source);
62 }
63 }
64
65 public Set getPropertyNames() {
66 HashSet ret = new HashSet(map.keySet());
67 for (int i=0; i<chain.length; ++i) {
68 ret.addAll(chain[i].getPropertyNames());
69 }
70 return ret;
71 }
72
73 public PropertySet getSubset(String prefix) {
74 return new PropertySubSet(this, prefix);
75 }
76
77 public boolean remove(String name) {
78 return map.remove(name)!=null;
79 }
80
81 public void set(String name, Object value) {
82 map.put(name, value);
83 }
84
85 public Object get(String name) {
86 Object ret = map.get(name);
87 if (ret!=null) {
88 return ret;
89 }
90
91 Iterator mit = mounts.entrySet().iterator();
92 while (mit.hasNext()) {
93 Map.Entry mount= (Entry) mit.next();
94 if (name.startsWith((String) mount.getKey())) {
95 ret = ((PropertySet) mount.getValue()).get(name.substring(((String) mount.getKey()).length()));
96 if (ret!=null) {
97 return ret;
98 }
99 }
100 }
101
102 for (int i=0; i<chain.length; ++i) {
103 ret = chain[i].get(name);
104 if (ret!=null) {
105 return ret;
106 }
107 }
108
109 return ret;
110 }
111
112 public void clear() {
113 map.clear();
114 }
115
116 private Map mounts = new HashMap();
117
118 public void mount(String prefix, PropertySet source) {
119 mounts.put(prefix, source);
120 }
121
122 public void setAll(PropertySet source) {
123 Iterator nit = source.getPropertyNames().iterator();
124 while (nit.hasNext()) {
125 String pName = (String) nit.next();
126 set(pName, source.get(pName));
127 }
128 }
129
130 /**
131 * @return true if other property set entries are equal to this property set entries.
132 * Other Property set attribute
133 */
134 public boolean compareProperties(PropertySet otherSet) {
135 Set myNames = new HashSet(getPropertyNames());
136 Set otherNames = new HashSet(otherSet.getPropertyNames());
137 if (!myNames.equals(otherNames)) {
138 return false;
139 }
140
141 Iterator mnit = myNames.iterator();
142 while (mnit.hasNext()) {
143 String pName = (String) mnit.next();
144 Object myValue = get(pName);
145 Object otherValue = otherSet.get(pName);
146 if (myValue==null) {
147 if (otherValue!=null) {
148 return false;
149 }
150 } else if (!myValue.equals(otherValue)) {
151 return false;
152 }
153 }
154
155 return true;
156 }
157
158 public boolean containsAll(PropertySet subSet) {
159 Set myNames = new HashSet(getPropertyNames());
160 Set subsetNames = new HashSet(subSet.getPropertyNames());
161 if (!myNames.containsAll(subsetNames)) {
162 return false;
163 }
164
165 Iterator snit = subsetNames.iterator();
166 while (snit.hasNext()) {
167 String pName = (String) snit.next();
168 Object myValue = get(pName);
169 Object subsetValue = subSet.get(pName);
170 if (myValue==null) {
171 if (subsetValue!=null) {
172 return false;
173 }
174 } else if (!myValue.equals(subsetValue)) {
175 return false;
176 }
177 }
178
179 return true;
180 }
181
182 public Object get(String name, Object defaultValue) {
183 Object ret = get(name);
184 return ret == null ? defaultValue : ret;
185 }
186}
187
188