KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > table > span > ScopedPropertyMetaSet


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.table.span;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collection JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.TreeSet JavaDoc;
23
24 import com.tonbeller.jpivot.olap.model.Member;
25 import com.tonbeller.jpivot.olap.model.MemberPropertyMeta;
26 import com.tonbeller.jpivot.olap.model.Property;
27 import com.tonbeller.jpivot.olap.navi.MemberProperties;
28
29 /**
30  * A MemberPropertyMeta Set implementation that supports different scopes (Level and Hierarchy).
31  * An Entry is identified by the Scope (either Level or Hierarchy) of the PropertyMeta and
32  * the property name. Example: Property "a" belongs to Level "l1" of Hierarchy "h", another
33  * Property "a" (same name) to Level "l2" (different Level) of the same Hierarchy "h". If
34  * these properties are added to a Level-scoped set, it would contain 2 different entries.
35  * When added to a Hierarchy-scoped set, it would contain one entry only (the first one added).
36  * @author av
37  */

38 public class ScopedPropertyMetaSet {
39   
40   /**
41    * ordered list of MemberPropertyMetas for a scope
42    *
43    * @author av
44    * @since 07.03.2005
45    */

46   class ScopeList {
47     List JavaDoc list = new ArrayList JavaDoc();
48     Map JavaDoc map = new HashMap JavaDoc();
49     void add(MemberPropertyMeta mpm) {
50       String JavaDoc name = mpm.getName();
51       if (!map.containsKey(name)) {
52         map.put(name, mpm);
53         list.add(mpm);
54       }
55     }
56     void remove(MemberPropertyMeta mpm) {
57       mpm = (MemberPropertyMeta) map.get(mpm.getName());
58       if (mpm == null)
59         return;
60       map.remove(mpm.getName());
61       list.remove(mpm);
62     }
63     MemberPropertyMeta lookup(String JavaDoc name) {
64       return (MemberPropertyMeta) map.get(name);
65     }
66     boolean contains(String JavaDoc name) {
67       return map.containsKey(name);
68     }
69     Iterator JavaDoc iterator() {
70       return list.iterator();
71     }
72   }
73
74   private MemberProperties extension;
75   
76   public ScopedPropertyMetaSet(MemberProperties extension) {
77     this.extension = extension;
78   }
79
80   // For every scope (=key) contains a ScopeList (=value) which contains the MemberPropertyMeta instances.
81
private Map JavaDoc scopeMap = new HashMap JavaDoc();
82
83   public void addAll(Collection JavaDoc memberPropertyMetas) {
84     for (Iterator JavaDoc it = memberPropertyMetas.iterator(); it.hasNext();) {
85       MemberPropertyMeta mpm = (MemberPropertyMeta) it.next();
86       add(mpm);
87     }
88   }
89
90   public void removeAll(Collection JavaDoc memberPropertyMetas) {
91     for (Iterator JavaDoc it = memberPropertyMetas.iterator(); it.hasNext();) {
92       MemberPropertyMeta mpm = (MemberPropertyMeta) it.next();
93       remove(mpm);
94     }
95   }
96
97   /**
98    * for every property of member, creates a MemberPropertyMeta and adds it to this set
99    * @param member
100    */

101   public void addMember(Member member) {
102     if (member == null)
103       return;
104     String JavaDoc scope = extension.getPropertyScope(member);
105     Property[] properties = member.getProperties();
106     properties = PropertyUtils.normalize(properties);
107     for (int i = 0; i < properties.length; i++) {
108       String JavaDoc label = properties[i].getLabel();
109       String JavaDoc name = properties[i].getName();
110       MemberPropertyMeta mpm = new MemberPropertyMeta(label, name, scope);
111       this.add(mpm);
112     }
113   }
114
115   public void add(MemberPropertyMeta mpm) {
116     Object JavaDoc scope = mpm.getScope();
117     ScopeList scopeList = (ScopeList) scopeMap.get(scope);
118     if (scopeList == null) {
119       scopeList = new ScopeList();
120       scopeMap.put(scope, scopeList);
121     }
122     scopeList.add(mpm);
123   }
124
125   public void remove(MemberPropertyMeta mpm) {
126     Object JavaDoc scope = mpm.getScope();
127     ScopeList scopeList = (ScopeList) scopeMap.get(scope);
128     if (scopeList == null)
129       return;
130     scopeList.remove(mpm);
131   }
132
133   /**
134    * true, if mpm is contained in the visiblePropertiyMetas collection
135    * @param mpm
136    * @return
137    */

138   public boolean contains(MemberPropertyMeta mpm) {
139     return contains(mpm.getScope(), mpm.getName());
140   }
141   /**
142    * true if the property <code>name</code> is contained in
143    * the <code>scope</code>. Example usage:
144    * <pre>
145    * Member m = ...
146    * Property p = m.getProperty("foo");
147    * MemberProperties extension = (MemberProperties ) model.getExtension(MemberProperties.ID);
148    *
149    * if (set.contains(extension.getScope(m), p.getName())
150    * ...
151    * </pre>
152    * @param scope
153    * @param name
154    * @return
155    */

156   public boolean contains(String JavaDoc scope, String JavaDoc name) {
157     ScopeList scopeList = (ScopeList) scopeMap.get(scope);
158     if (scopeList == null)
159       return false;
160     return scopeList.contains(name);
161   }
162
163   MemberPropertyMeta lookup(String JavaDoc scope, String JavaDoc name) {
164     ScopeList scopeList = (ScopeList) scopeMap.get(scope);
165     if (scopeList == null)
166       return null;
167     return scopeList.lookup(name);
168   }
169   
170   /**
171    * creates a new List that contains all the MemberPropertyMetas that are contained in <code>metas</code>
172    * and are contained in this set (intersection). The order is not changed.
173    * @param metas a list of MemberPropertyMetas
174    * @return
175    */

176   public List JavaDoc intersectList(List JavaDoc metas) {
177     List JavaDoc list = new ArrayList JavaDoc();
178     for (Iterator JavaDoc it = metas.iterator(); it.hasNext();) {
179       MemberPropertyMeta mpm = (MemberPropertyMeta) it.next();
180       mpm = lookup(mpm.getScope(), mpm.getName());
181       if (mpm != null)
182         list.add(mpm);
183     }
184     return list;
185   }
186
187
188   /**
189    * returns a list of Metas that pass the filter
190    */

191   public List JavaDoc metaList(MemberPropertyMetaFilter filter) {
192     List JavaDoc list = new ArrayList JavaDoc();
193     for (Iterator JavaDoc it = scopeMap.values().iterator(); it.hasNext();) {
194       ScopeList scopeList = (ScopeList ) it.next();
195       for (Iterator JavaDoc vt = scopeList.iterator(); vt.hasNext();) {
196         MemberPropertyMeta meta = (MemberPropertyMeta) vt.next();
197         if (filter.accept(meta))
198           list.add(meta);
199       }
200     }
201     return list;
202   }
203
204   /**
205    * returns a subset containing the metas that pass the filter
206    */

207   public ScopedPropertyMetaSet metaSet(MemberPropertyMetaFilter filter) {
208     ScopedPropertyMetaSet set = new ScopedPropertyMetaSet(extension);
209     for (Iterator JavaDoc it = scopeMap.values().iterator(); it.hasNext();) {
210       ScopeList scopeList = (ScopeList) it.next();
211       for (Iterator JavaDoc vt = scopeList.iterator(); vt.hasNext();) {
212         MemberPropertyMeta meta = (MemberPropertyMeta) vt.next();
213         if (filter.accept(meta))
214           set.add(meta);
215       }
216     }
217     return set;
218   }
219
220   /**
221    * @return sorted (!) array of all names - for test use only
222    */

223   String JavaDoc[] getAllNames() {
224     Set JavaDoc allNames = new TreeSet JavaDoc();
225     for (Iterator JavaDoc it = scopeMap.values().iterator(); it.hasNext();) {
226       ScopeList scopeList = (ScopeList) it.next();
227       for (Iterator JavaDoc vt = scopeList.iterator(); vt.hasNext();) {
228         MemberPropertyMeta meta = (MemberPropertyMeta) vt.next();
229         allNames.add(meta.getName());
230       }
231     }
232     return (String JavaDoc[]) allNames.toArray(new String JavaDoc[allNames.size()]);
233   }
234
235   /**
236    * accepts all metas
237    */

238   static class AllFilter implements MemberPropertyMetaFilter {
239     public boolean accept(MemberPropertyMeta meta) {
240       return true;
241     }
242   }
243   public MemberPropertyMetaFilter createAllFilter() {
244     return new AllFilter();
245   }
246
247   /**
248    * accepts metas that belong to a set of scopes.
249    * @see ScopedPropertyMetaSet#getScope
250    */

251   class ScopesFilter implements MemberPropertyMetaFilter {
252     Set JavaDoc scopes;
253     public ScopesFilter(Set JavaDoc scopes) {
254       this.scopes = scopes;
255     }
256     public boolean accept(MemberPropertyMeta meta) {
257       return scopes.contains(meta.getScope());
258     }
259   }
260   public MemberPropertyMetaFilter createScopesFilter(Set JavaDoc scopes) {
261     return new ScopesFilter(scopes);
262   }
263
264   /**
265    * accepts metas that are not rendered inline
266    */

267   static class IgnoreInlineFilter implements MemberPropertyMetaFilter {
268     public boolean accept(MemberPropertyMeta meta) {
269       return !PropertyUtils.isInline(meta.getName());
270     }
271   }
272   public MemberPropertyMetaFilter createIgnoreInlineFilter() {
273     return new IgnoreInlineFilter();
274   }
275
276 }
277
Popular Tags