PropertySubSet.java

biz/hammurapi/properties/PropertySubSet.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Undocumented constructor 2 41:9
Java Inspector 089 Undocumented method 2 46:9
Java Inspector 089 Undocumented method 2 59:9
Java Inspector 089 Undocumented method 2 63:9
Java Inspector 089 Undocumented method 2 67:9
Java Inspector 089 Undocumented method 2 71:9
Java Inspector 089 Undocumented method 2 88:9
Java Inspector 089 Undocumented method 2 100:9
Java Inspector 089 Undocumented method 2 104:9
Java Inspector 089 Method is not properly documented 2 116:9
Java Inspector 089 Undocumented parameter otherSet 2 116:9
Java Inspector 089 Undocumented method 2 140:9
Java Inspector 089 Undocumented method 2 164:9
Java Inspector 040 Parameter name prefix clashes with field name in PropertySubSet 3 59:38
Java Inspector 040 Parameter name prefix clashes with field name in PropertySubSet 3 100:27
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 41: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 * Wrapper around a property set to get access to a sub-set of properties based on prefix.
34 * @author Pavel
35 */
36public class PropertySubSet implements PropertySet {
37
38 private PropertySet master;
39 private String prefix;
40
41 public PropertySubSet(PropertySet master, String prefix) {
42 this.master = master;
43 this.prefix = prefix;
44 }
45
46 public Set getPropertyNames() {
47 Set ret = new HashSet();
48
49 Iterator nit = master.getPropertyNames().iterator();
50 while (nit.hasNext()) {
51 String name = (String) nit.next();
52 if (name.startsWith(prefix)) {
53 ret.add(name.substring(prefix.length()));
54 }
55 }
56 return ret;
57 }
58
59 public PropertySet getSubset(String prefix) {
60 return new PropertySubSet(this, prefix);
61 }
62
63 public boolean remove(String name) {
64 return master.remove(prefix+name);
65 }
66
67 public void set(String name, Object value) {
68 master.set(prefix+name, value);
69 }
70
71 public Object get(String name) {
72 Object ret = master.get(prefix+name);
73 if (ret==null) {
74 Iterator esit = mounts.entrySet().iterator();
75 while (esit.hasNext()) {
76 Entry entry = (Entry) esit.next();
77 if (name.startsWith((String) entry.getKey())) {
78 ret = ((PropertySet) entry.getValue()).get(name.substring(((String) entry.getKey()).length()));
79 if (ret!=null) {
80 return ret;
81 }
82 }
83 }
84 }
85 return ret;
86 }
87
88 public void clear() {
89 Iterator nit = master.getPropertyNames().iterator();
90 while (nit.hasNext()) {
91 String name = (String) nit.next();
92 if (name.startsWith(prefix)) {
93 master.remove(name);
94 }
95 }
96 }
97
98 private Map mounts = new HashMap();
99
100 public void mount(String prefix, PropertySet source) {
101 mounts.put(prefix, source);
102 }
103
104 public void setAll(PropertySet source) {
105 Iterator pnit = source.getPropertyNames().iterator();
106 while (pnit.hasNext()) {
107 String pName = (String) pnit.next();
108 set(pName, source.get(pName));
109 }
110 }
111
112 /**
113 * @return true if other property set entries are equal to this property set entries.
114 * Other Property set attribute
115 */
116 public boolean compareProperties(PropertySet otherSet) {
117 Set myNames = new HashSet(getPropertyNames());
118 Set otherNames = new HashSet(otherSet.getPropertyNames());
119 if (!myNames.equals(otherNames)) {
120 return false;
121 }
122
123 Iterator mnit = myNames.iterator();
124 while (mnit.hasNext()) {
125 String pName = (String) mnit.next();
126 Object myValue = get(pName);
127 Object otherValue = otherSet.get(pName);
128 if (myValue==null) {
129 if (otherValue!=null) {
130 return false;
131 }
132 } else if (!myValue.equals(otherValue)) {
133 return false;
134 }
135 }
136
137 return true;
138 }
139
140 public boolean containsAll(PropertySet subSet) {
141 Set myNames = new HashSet(getPropertyNames());
142 Set subsetNames = new HashSet(subSet.getPropertyNames());
143 if (!myNames.containsAll(subsetNames)) {
144 return false;
145 }
146
147 Iterator snit = subsetNames.iterator();
148 while (snit.hasNext()) {
149 String pName = (String) snit.next();
150 Object myValue = get(pName);
151 Object subsetValue = subSet.get(pName);
152 if (myValue==null) {
153 if (subsetValue!=null) {
154 return false;
155 }
156 } else if (!myValue.equals(subsetValue)) {
157 return false;
158 }
159 }
160
161 return true;
162 }
163
164 public Object get(String name, Object defaultValue) {
165 Object ret = get(name);
166 return ret == null ? defaultValue : ret;
167 }
168}
169