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     * Wrapper around a property set to get access to a sub-set of properties based on prefix.
015     * @author Pavel
016     */
017    public class PropertySubSet implements PropertySet {
018            
019            private PropertySet master;
020            private String prefix;
021    
022            public PropertySubSet(PropertySet master, String prefix) {
023                    this.master = master;
024                    this.prefix = prefix;
025            }
026    
027            public Set getPropertyNames() {
028                    Set ret = new HashSet();
029                    
030                    Iterator nit = master.getPropertyNames().iterator();
031                    while (nit.hasNext()) {
032                            String name = (String) nit.next(); 
033                            if (name.startsWith(prefix)) {
034                                    ret.add(name.substring(prefix.length()));
035                            }
036                    }
037                    return ret;
038            }
039    
040            public PropertySet getSubset(String prefix) {           
041                    return new PropertySubSet(this, prefix);
042            }
043    
044            public boolean remove(String name) {
045                    return master.remove(prefix+name);
046            }
047    
048            public void set(String name, Object value) {
049                    master.set(prefix+name, value);
050            }
051    
052            public Object get(String name) {
053                    Object ret = master.get(prefix+name);
054                    if (ret==null) {
055                            Iterator esit = mounts.entrySet().iterator();
056                            while (esit.hasNext()) {
057                                    Entry entry = (Entry) esit.next();                              
058                                    if (name.startsWith((String) entry.getKey())) {
059                                            ret = ((PropertySet) entry.getValue()).get(name.substring(((String) entry.getKey()).length()));
060                                            if (ret!=null) {
061                                                    return ret;
062                                            }
063                                    }
064                            }
065                    }
066                    return ret;
067            }
068    
069            public void clear() {
070                    Iterator nit = master.getPropertyNames().iterator();
071                    while (nit.hasNext()) {
072                            String name = (String) nit.next();
073                            if (name.startsWith(prefix)) {
074                                    master.remove(name);
075                            }
076                    }               
077            }
078    
079            private Map mounts = new HashMap();
080    
081            public void mount(String prefix, PropertySet source) {
082                    mounts.put(prefix, source);             
083            }
084    
085            public void setAll(PropertySet source) {
086                    Iterator pnit = source.getPropertyNames().iterator();
087                    while (pnit.hasNext()) {
088                            String pName = (String) pnit.next();
089                            set(pName, source.get(pName));
090                    }               
091            }               
092            
093            /**
094             * @return true if other property set entries are equal to this property set entries. 
095             * Other Property set attribute
096             */
097            public boolean compareProperties(PropertySet otherSet) {
098                    Set myNames = new HashSet(getPropertyNames());
099                    Set otherNames = new HashSet(otherSet.getPropertyNames());
100                    if (!myNames.equals(otherNames)) {
101                            return false;
102                    }
103                    
104                    Iterator mnit = myNames.iterator();
105                    while (mnit.hasNext()) {
106                            String pName =  (String) mnit.next();
107                            Object myValue = get(pName);
108                            Object otherValue = otherSet.get(pName);
109                            if (myValue==null) {
110                                    if (otherValue!=null) {
111                                            return false;
112                                    }
113                            } else if (!myValue.equals(otherValue)) {
114                                    return false;
115                            }                       
116                    }
117                    
118                    return true;
119            }
120    
121            public boolean containsAll(PropertySet subSet) {
122                    Set myNames = new HashSet(getPropertyNames());
123                    Set subsetNames = new HashSet(subSet.getPropertyNames());
124                    if (!myNames.containsAll(subsetNames)) {
125                            return false;
126                    }
127                    
128                    Iterator snit = subsetNames.iterator();
129                    while (snit.hasNext()) {
130                            String pName = (String) snit.next();
131                            Object myValue = get(pName);
132                            Object subsetValue = subSet.get(pName);
133                            if (myValue==null) {
134                                    if (subsetValue!=null) {
135                                            return false;
136                                    }
137                            } else if (!myValue.equals(subsetValue)) {
138                                    return false;
139                            }                       
140                    }
141                    
142                    return true;
143            }
144    
145            public Object get(String name, Object defaultValue) {
146                    Object ret = get(name);
147                    return ret == null ? defaultValue : ret;
148            }       
149    }