001    /*
002     @license.text@ 
003     */
004    package biz.hammurapi.properties;
005    
006    import java.util.HashMap;
007    import java.util.HashSet;
008    import java.util.Iterator;
009    import java.util.Map;
010    import java.util.Set;
011    import java.util.Map.Entry;
012    
013    /**
014     * Creates property sets backed by java.util.HashMap.
015     * @author Pavel
016     *
017     */
018            
019    public class MapPropertySet implements PropertySet {
020            private Map map = new HashMap();                
021            private PropertySet[] chain = {};
022            
023            public MapPropertySet(PropertySet[] chain) {
024                    if (chain!=null) {
025                            this.chain = chain;
026                    }
027            }
028            
029            public MapPropertySet(Map source, PropertySet[] chain) {
030                    this(chain);
031                    if (source!=null) {
032                            map.putAll(source);
033                    }
034            }
035    
036            public MapPropertySet() {
037    
038            }
039            
040            public MapPropertySet(Map source) {
041                    if (source!=null) {
042                            map.putAll(source);
043                    }
044            }
045            
046            public Set getPropertyNames() {
047                    HashSet ret = new HashSet(map.keySet());
048                    for (int i=0; i<chain.length; ++i) {
049                            ret.addAll(chain[i].getPropertyNames());
050                    }
051                    return ret;
052            }
053    
054            public PropertySet getSubset(String prefix) {
055                    return new PropertySubSet(this, prefix);
056            }
057    
058            public boolean remove(String name) {
059                    return map.remove(name)!=null;
060            }
061    
062            public void set(String name, Object value) {
063                    map.put(name, value);
064            }
065    
066            public Object get(String name) {
067                    Object ret = map.get(name);
068                    if (ret!=null) {
069                            return ret;
070                    }
071                    
072                    Iterator mit = mounts.entrySet().iterator();
073                    while (mit.hasNext()) {
074                            Map.Entry mount= (Entry) mit.next();
075                            if (name.startsWith((String) mount.getKey())) {
076                                    ret = ((PropertySet) mount.getValue()).get(name.substring(((String) mount.getKey()).length()));
077                                    if (ret!=null) {
078                                            return ret;
079                                    }
080                            }
081                    }
082                    
083                    for (int i=0; i<chain.length; ++i)  {
084                            ret = chain[i].get(name);
085                            if (ret!=null) {
086                                    return ret;
087                            }
088                    }
089                    
090                    return ret;
091            }
092    
093            public void clear() {
094                    map.clear();            
095            }
096    
097            private Map mounts = new HashMap();
098    
099            public void mount(String prefix, PropertySet source) {
100                    mounts.put(prefix, source);             
101            }
102            
103            public void setAll(PropertySet source) {
104                    Iterator nit = source.getPropertyNames().iterator();
105                    while (nit.hasNext()) {
106                            String pName = (String) nit.next();
107                            set(pName, source.get(pName));
108                    }               
109            }       
110            
111            /**
112             * @return true if other property set entries are equal to this property set entries. 
113             * Other Property set attribute
114             */
115            public boolean compareProperties(PropertySet otherSet) {
116                    Set myNames = new HashSet(getPropertyNames());
117                    Set otherNames = new HashSet(otherSet.getPropertyNames());
118                    if (!myNames.equals(otherNames)) {
119                            return false;
120                    }
121                    
122                    Iterator mnit = myNames.iterator();
123                    while (mnit.hasNext()) {
124                            String pName = (String) mnit.next();
125                            Object myValue = get(pName);
126                            Object otherValue = otherSet.get(pName);
127                            if (myValue==null) {
128                                    if (otherValue!=null) {
129                                            return false;
130                                    }
131                            } else if (!myValue.equals(otherValue)) {
132                                    return false;
133                            }                       
134                    }
135                    
136                    return true;
137            }
138    
139            public boolean containsAll(PropertySet subSet) {
140                    Set myNames = new HashSet(getPropertyNames());
141                    Set subsetNames = new HashSet(subSet.getPropertyNames());
142                    if (!myNames.containsAll(subsetNames)) {
143                            return false;
144                    }
145                    
146                    Iterator snit = subsetNames.iterator();
147                    while (snit.hasNext()) {
148                            String pName = (String) snit.next();
149                            Object myValue = get(pName);
150                            Object subsetValue = subSet.get(pName);
151                            if (myValue==null) {
152                                    if (subsetValue!=null) {
153                                            return false;
154                                    }
155                            } else if (!myValue.equals(subsetValue)) {
156                                    return false;
157                            }                       
158                    }
159                    
160                    return true;
161            }
162    
163            public Object get(String name, Object defaultValue) {
164                    Object ret = get(name);
165                    return ret == null ? defaultValue : ret;
166            }       
167    }
168